2.
write a java program to perform encryption and decryption using the
following algorithm
(ii) playfair cipher
import [Link].*;
public class PlayfairCipher {
private char[][] keySquare = new char[5][5];
private String key;
public PlayfairCipher(String key) {
[Link] = key;
generateKeySquare();
}
private void generateKeySquare() {
String alphabet = "ABCDEFGHIKLMNOPQRSTUVWXYZ"; // J is omitted
String keyString = [Link]().replaceAll("J", "I") + alphabet;
Set<Character> used = new LinkedHashSet<>();
for (char c : [Link]()) {
if ([Link](c)) {
[Link](c);
}
}
Iterator<Character> it = [Link]();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
keySquare[i][j] = [Link]();
}
}
}
private String prepareText(String text) {
text = [Link]().replaceAll("J", "I").replaceAll("[^A-Z]", "");
StringBuilder prepared = new StringBuilder();
for (int i = 0; i < [Link](); i++) {
[Link]([Link](i));
if (i < [Link]() - 1 && [Link](i) == [Link](i + 1)) {
[Link]('X');
}
}
if ([Link]() % 2 != 0) {
[Link]('X');
}
return [Link]();
}
private String processDigraphs(String text, boolean encrypt) {
StringBuilder result = new StringBuilder();
text = prepareText(text);
for (int i = 0; i < [Link](); i += 2) {
char a = [Link](i);
char b = [Link](i + 1);
int[] posA = findPosition(a);
int[] posB = findPosition(b);
if (posA[0] == posB[0]) { // Same row
[Link](keySquare[posA[0]][(posA[1] + (encrypt ? 1 : 4)) % 5]);
[Link](keySquare[posB[0]][(posB[1] + (encrypt ? 1 : 4)) % 5]);
} else if (posA[1] == posB[1]) { // Same column
[Link](keySquare[(posA[0] + (encrypt ? 1 : 4)) % 5][posA[1]]);
[Link](keySquare[(posB[0] + (encrypt ? 1 : 4)) % 5][posB[1]]);
} else { // Rectangle swap
[Link](keySquare[posA[0]][posB[1]]);
[Link](keySquare[posB[0]][posA[1]]);
}
}
return [Link]();
}
private int[] findPosition(char c) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (keySquare[i][j] == c) {
return new int[]{i, j};
}
}
}
return null;
}
public String encrypt(String text) {
return processDigraphs(text, true);
}
public String decrypt(String text) {
return processDigraphs(text, false);
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter key: ");
String key = [Link]();
PlayfairCipher cipher = new PlayfairCipher(key);
[Link]("Enter message: ");
String message = [Link]();
String encrypted = [Link](message);
[Link]("Encrypted: " + encrypted);
String decrypted = [Link](encrypted);
[Link]("Decrypted: " + decrypted);
[Link]();
}
}
3. Write a program to implement RSA algorithm.
import [Link];
import [Link];
import [Link];
public class RSAAlgorithm {
private BigInteger p, q, n, phi, e, d;
private int bitLength = 1024;
private Random rand;
public RSAAlgorithm() {
rand = new Random();
p = [Link](bitLength / 2, rand);
q = [Link](bitLength / 2, rand);
n = [Link](q);
phi = [Link]([Link]).multiply([Link]([Link]));
e = [Link](bitLength / 2, rand);
while ([Link](e).compareTo([Link]) > 0 && [Link](phi) < 0)
{
e = [Link](bitLength / 2, rand);
}
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) {
Scanner scanner = new Scanner([Link]);
RSAAlgorithm rsa = new RSAAlgorithm();
[Link]("Public Key: (" + rsa.e + ", " + rsa.n + ")");
[Link]("Private Key: (" + rsa.d + ", " + rsa.n + ")");
[Link]("Enter message (numeric): ");
BigInteger message = [Link]();
BigInteger encrypted = [Link](message);
[Link]("Encrypted: " + encrypted);
BigInteger decrypted = [Link](encrypted);
[Link]("Decrypted: " + decrypted);
[Link]();
}
}
4. calculate message digest of a text using SHA -1 algorithm in JAVA
import [Link];
import [Link];
import [Link];
public class SHA1Digest {
public static String calculateSHA1(String input) {
try {
MessageDigest md = [Link]("SHA-1");
byte[] messageDigest = [Link]([Link]());
StringBuilder hexString = new StringBuilder();
for (byte b : messageDigest) {
[Link]([Link]("%02x", b));
}
return [Link]();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter text: ");
String text = [Link]();
String sha1Hash = calculateSHA1(text);
[Link]("SHA-1 Digest: " + sha1Hash);
[Link]();
}
}
5. Calculate message digest of a text using MD5 algorithm in JAVA
import [Link];
import [Link];
import [Link];
public class MD5Digest {
public static String calculateMD5(String input) {
try {
MessageDigest md = [Link]("MD5");
byte[] messageDigest = [Link]([Link]());
StringBuilder hexString = new StringBuilder();
for (byte b : messageDigest) {
[Link]([Link]("%02x", b));
}
return [Link]();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter text: ");
String text = [Link]();
String md5Hash = calculateMD5(text);
[Link]("MD5 Digest: " + md5Hash);
[Link]();
}
}
8. Implement RC4 algorithm and check the following input using java
Enter plaintext : hello
Enter key of same length : yello
plaintext in bytes : 104 101 108 108 111
keystream is : 248 30 202 165 16
Cipher text is : 144 123 166 201 127
Plaintext after decryption : 104 101 108 108 111
import [Link];
public class RC4Cipher {
private byte[] S = new byte[256];
private int i, j;
public RC4Cipher(String key) {
byte[] keyBytes = [Link]();
initializeSBox(keyBytes);
}
private void initializeSBox(byte[] key) {
for (i = 0; i < 256; i++) {
S[i] = (byte) i;
}
j = 0;
for (i = 0; i < 256; i++) {
j = (j + S[i] + key[i % [Link]]) & 0xFF;
swap(i, j);
}
i = j = 0;
}
private void swap(int a, int b) {
byte temp = S[a];
S[a] = S[b];
S[b] = temp;
}
private byte getKeystreamByte() {
i = (i + 1) & 0xFF;
j = (j + S[i]) & 0xFF;
swap(i, j);
return S[(S[i] + S[j]) & 0xFF];
}
public byte[] encryptDecrypt(byte[] input) {
byte[] output = new byte[[Link]];
for (int k = 0; k < [Link]; k++) {
output[k] = (byte) (input[k] ^ getKeystreamByte());
}
return output;
}
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter plaintext: ");
String plaintext = [Link]();
[Link]("Enter key of same length: ");
String key = [Link]();
if ([Link]() != [Link]()) {
[Link]("Error: Key length must be the same as plaintext
length.");
[Link]();
return;
}
RC4Cipher rc4 = new RC4Cipher(key);
byte[] plaintextBytes = [Link]();
[Link]("Plaintext in bytes: ");
for (byte b : plaintextBytes) {
[Link]((b & 0xFF) + " ");
}
[Link]();
byte[] cipherBytes = [Link](plaintextBytes);
[Link]("Cipher text is: ");
for (byte b : cipherBytes) {
[Link]((b & 0xFF) + " ");
}
[Link]();
RC4Cipher rc4Decrypt = new RC4Cipher(key);
byte[] decryptedBytes = [Link](cipherBytes);
[Link]("Plaintext after decryption: ");
for (byte b : decryptedBytes) {
[Link]((b & 0xFF) + " ");
}
[Link]();
[Link]();
}
}
9. Implement the SIGNATURE SCHEME - Digital Signature Standard.
import [Link].*;
import [Link].Base64;
import [Link];
public class DigitalSignatureDSS {
public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGen = [Link]("DSA");
[Link](1024);
return [Link]();
}
public static byte[] signData(String data, PrivateKey privateKey) throws
Exception {
Signature signature = [Link]("SHA1withDSA");
[Link](privateKey);
[Link]([Link]());
return [Link]();
}
public static boolean verifySignature(String data, byte[] signatureBytes,
PublicKey publicKey) throws Exception {
Signature signature = [Link]("SHA1withDSA");
[Link](publicKey);
[Link]([Link]());
return [Link](signatureBytes);
}
public static void main(String[] args) {
try {
Scanner scanner = new Scanner([Link]);
[Link]("Enter message: ");
String message = [Link]();
KeyPair keyPair = generateKeyPair();
byte[] signatureBytes = signData(message, [Link]());
[Link]("Generated Signature: " +
[Link]().encodeToString(signatureBytes));
boolean isVerified = verifySignature(message, signatureBytes,
[Link]());
[Link]("Signature Verification: " + (isVerified ? "Valid" :
"Invalid"));
[Link]();
} catch (Exception e) {
[Link]();
}
}
}
10. Generate first five keys for the initial key : ThisIsA128BitKey in AES
algorithm using java.
sample output:
Round 0: 54684973497341313238624B657921
Round 1: 96392E2B8BDE6E8A3C5C36E5A7D441D3
Round 2: 0FEF946C7D6DBD592C019D928E72E5E1
Round 3: 27341A879D8389E63F65F2B041D4D0B3
Round 4: D0BCE5E63C05A9D2B2A5E4D73A2DFB6F
Round 5: 92E87DF5A1D457FA12F5D4BC8A374D5A
import [Link];
import [Link];
import [Link];
import [Link];
public class AESKeyExpansion {
private static final int KEY_SIZE = 16; // 128-bit key
private static final int NUM_ROUNDS = 5;
private static final byte[] RCON = {
(byte) 0x01, (byte) 0x02, (byte) 0x04, (byte) 0x08, (byte) 0x10
};
public static void main(String[] args) {
byte[] key = "ThisIsA128BitKey".getBytes();
byte[][] expandedKeys = keyExpansion(key);
for (int i = 0; i <= NUM_ROUNDS; i++) {
[Link]("Round " + i + ": ");
for (byte b : expandedKeys[i]) {
[Link]("%02X", b);
}
[Link]();
}
}
private static byte[][] keyExpansion(byte[] key) {
byte[][] expandedKeys = new byte[NUM_ROUNDS + 1][KEY_SIZE];
[Link](key, 0, expandedKeys[0], 0, KEY_SIZE);
for (int i = 1; i <= NUM_ROUNDS; i++) {
byte[] temp = [Link](expandedKeys[i - 1], KEY_SIZE);
keyScheduleCore(temp, i - 1);
for (int j = 0; j < KEY_SIZE; j++) {
expandedKeys[i][j] = (byte) (expandedKeys[i - 1][j] ^ temp[j]);
}
}
return expandedKeys;
}
private static void keyScheduleCore(byte[] key, int round) {
byte temp = key[0];
[Link](key, 1, key, 0, 3);
key[3] = temp;
for (int i = 0; i < 4; i++) {
key[i] = sBox(key[i]);
}
key[0] ^= RCON[round];
}
private static byte sBox(byte input) {
int[] sbox = {
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67,
0x2b, 0xfe, 0xd7, 0xab, 0x76
};
return (byte) sbox[input & 0x0F];
}
}