Exercise 1- Caesar Cipher:
public class CaesarCipher {
// Encryption method
for (char ch : plaintext.toCharArray()) {
char base
= Character.
isUpperCase(ch
) ? 'A' : 'a';
encryptedText=encryptedText +((char) ((ch - base + shift) % 26
+ base));
} else {
encryptedText+=ch;
}
}
return encryptedText;
}
// Decryption method
public static String decrypt
(String ciphertext,
int shift
) {
// Decryption is the same as encryption with a negative shift
return encrypt(ciphertext, -shift);
}
public static void main
(String[] args
) {
// Example Usage
String plaintext
= "Hello, World!";
int shift = 3;
// Encryption
String encryptedText
= encrypt
(plaintext, shift
);
System.
out.
println("Encrypted: " + encryptedText
);
// Decryption
String decryptedText
= decrypt
(encryptedText, shift
);
System.
out.
println("Decrypted: " + decryptedText
);
}
}
Exercise 2- Affine Cipher:
import java.util.Random;
public class AffineCipherExample {
public static void main
(String[] args
) {
// Example Usage
String plaintext
= "Hello, World!"; // Replace with your desired
plaintext
// Generate random values for a and b (ensuring a is coprime with
26)
int a = generateCoprimeWith26();
int b
= new Random().
nextInt(26); // b can be any random integer
// Encryption
String ciphertext
= encrypt
(plaintext, a, b
);
System.
out.
println("Plaintext: " + plaintext
);
System.
out.
println("Encrypted: " + ciphertext
);
// Decryption
String decryptedText
= decrypt
(ciphertext, a, b
);
System.
out.
println("Decrypted: " + decryptedText
);
}
// Method to check if two numbers are coprime
private static boolean areCoprime(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a == 1;
}
// Method to generate a random integer coprime with 26
private static int generateCoprimeWith26() {
int candidate;
do {
candidate = random.nextInt(25) + 1; // Generate a random
integer between 1 and 25
} while (!areCoprime(candidate, 26));
return candidate;
}
// Affine Cipher Encryption
private static String encrypt
(String plaintext,
int a,
int b
) {
for (char ch : plaintext.toCharArray()) {
char base
= Character.
isUpperCase(ch
) ? 'A' : 'a';
encryptedText+=((char) ((a * (ch - base) + b) % 26 + base));
} else {
encryptedText+=ch;
}
}
return encryptedText;
}
// Affine Cipher Decryption
private static String decrypt
(String ciphertext,
int a,
int b
) {
// Calculate modular multiplicative inverse of a modulo 26
int aInverse = 0;
for (int i = 1; i < 26; i++) {
if ((a * i) % 26 == 1) {
System.
out.
println("aInverse= "+ i
);
aInverse = i;
break;
}
}
for (char ch : ciphertext.toCharArray()) {
char base
= Character.
isUpperCase(ch
) ? 'A' : 'a';
decryptedText+=((char) ((aInverse * (ch - base - b + 26) % 26) +
base));
} else {
decryptedText+=ch;
}
}
return decryptedText;
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}