c) Hill Cipher
import [Link];
import [Link];
public class hillcipher {
public static int[][] keymat = new int[][] {
{ 1, 2, 1 },
{ 2, 3, 2 },
{ 2, 2, 1 }
};
public static int[][] invkeymat = new int[][] {
{ -1, 0, 1 },
{ 2, -1, 0 },
{ -2, 2, -1 }
};
public static String key = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static void main(String[] args) {
String text, outtext = "", outtext1 = "";
int ch, n;
Scanner sc = new Scanner([Link]);
[Link]("Enter the Plain text for Encryption: ");
text = [Link]();
text = [Link]();
text = [Link]("\\s+", "");
n = [Link]() % 3;
if (n != 0) {
for (int i = 1; i <= (3 - n); i++)
text += "X";
}
[Link]("Padded Text:" + text);
char[] ptextchars1 = [Link]();
for (int i = 0; i < [Link](); i += 3) {
outtext += encrypt(ptextchars1[i], ptextchars1[i + 1], ptextchars1[i + 2]);
}
[Link]("Encrypted Message: " + outtext);
char[] ptextchars2 = [Link]();
for (int i = 0; i < [Link](); i += 3) {
outtext1 += decrypt(ptextchars2[i], ptextchars2[i + 1], ptextchars2[i + 2]);
}
[Link]("Decrypted Message: " + outtext1);
}
private static String encrypt(char a, char b, char c) {
String ret = "";
int x, y, z;
int posa = (int) a - 65;
int posb = (int) b - 65;
int posc = (int) c - 65;
x = posa * keymat[0][0] + posb * keymat[0][1] + posc * keymat[0][2];
y = posa * keymat[1][0] + posb * keymat[1][1] + posc * keymat[1][2];
z = posa * keymat[2][0] + posb * keymat[2][1] + posc * keymat[2][2];
a = [Link]((x % 26 + 26) % 26);
b = [Link]((y % 26 + 26) % 26);
c = [Link]((z % 26 + 26) % 26);
ret = "" + a + b + c;
return ret;
}
private static String decrypt(char a, char b, char c) {
String ret = "";
int x, y, z;
int posa = (int) a - 65;
int posb = (int) b - 65;
int posc = (int) c - 65;
x = posa * invkeymat[0][0] + posb * invkeymat[0][1] + posc * invkeymat[0][2];
y = posa * invkeymat[1][0] + posb * invkeymat[1][1] + posc * invkeymat[1][2];
z = posa * invkeymat[2][0] + posb * invkeymat[2][1] + posc * invkeymat[2][2];
a = [Link]((x % 26 + 26) % 26);
b = [Link]((y % 26 + 26) % 26);
c = [Link]((z % 26 + 26) % 26);
ret = "" + a + b + c;
return ret;
}
}
a) DES
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].Base64;
public class DES {
public static void main(String[] args) {
try {
String keyString = "SecretKey";
SecretKey secretKey = generateDESKey(keyString);
String plaintext = "Hello, DES!";
String ciphertext = encrypt(plaintext, secretKey);
String decryptedText = decrypt(ciphertext, secretKey);
[Link]("Plaintext: " + plaintext);
[Link]("Ciphertext: " + ciphertext);
[Link]("Decrypted Text: " + decryptedText);
} catch (Exception e) {
[Link]();
}
}
public static SecretKey generateDESKey(String keyString) throws Exception {
DESKeySpec desKeySpec = new
DESKeySpec([Link](StandardCharsets.UTF_8));
SecretKeyFactory keyFactory = [Link]("DES");
return [Link](desKeySpec);
}
public static String encrypt(String plaintext, SecretKeysecretKey) throws Exception
{
Cipher cipher = [Link]("DES");
[Link](Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes =
[Link]([Link](StandardCharsets.UTF_8));
return [Link]().encodeToString(encryptedBytes);
}
public static String decrypt(String ciphertext, SecretKeysecretKey) throws
Exception {
Cipher cipher = [Link]("DES");
[Link](Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes =
[Link]([Link]().decode(ciphertext));
return new String(decryptedBytes, StandardCharsets.UTF_8);
}
}
b) RSA – Algorithm
import [Link];
import [Link];
import [Link];
public class RSA {
private BigInteger p;
private BigInteger q;
private BigInteger N;
private BigInteger phi;
private BigInteger e;
private BigInteger d;
private int bitlength = 1024;
private Random r;
public RSA() {
r = new SecureRandom();
p = [Link](bitlength, r);
q = [Link](bitlength, r);
N = [Link](q);
phi = [Link]([Link]).multiply([Link]([Link]));
e = [Link](bitlength / 2, r);
while ([Link](e).compareTo([Link]) > 0 && [Link](phi) < 0) {
e = [Link]([Link]);
}
d = [Link](phi);
}
public BigInteger encrypt(BigInteger message) {
return [Link](e, N);
}
public BigInteger decrypt(BigInteger encrypted) {
return [Link](d, N);
}
public static void main(String[] args) {
RSA rsa = new RSA();
BigInteger message = new BigInteger("42");
BigInteger encrypted_msg = [Link](message);
BigInteger decrypted_msg = [Link](encrypted_msg);
[Link]("Message: " + message);
[Link]("Encrypted Message: " + encrypted_msg);
[Link]("Decrypted Message: " + decrypted_msg);
}
}