Cns Record
Cns Record
DEPARTMENT OF MCA
CERTIFICATE
External Examiner
CRYPTOGRAPHY AND NETWORK SECURITY
1. Write a Java program to perform encryption and decryption using the following
algorithms:
6. Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
8. Compute common secret key between client and server using Diffie-Hellman key
exchange technique. Perform encryption and decryption of message using the shared
secret key (Use simple XOR operation to encrypt and decrypt the message).
9. Implement DSS algorithm for signing and verification of messages between two
parties (obtain H (M) using simple XOR method of hash computation on M).
. Write a Java program to perform encryption and decryption using the following algorithms:
a) Ceaser Cipher
package Crypto;
import javax.swing.*;
class Caesar {
int key;
Caesar(int k)
key=k;
String encrypt(String s)
int i=0;
s=s.toUpperCase();
for(i=0;i<s.length();i++)
if(Character.isLetter(s.charAt(i)))ct.append((char)((((s.charAt(i)-'A')+key)%26)+'A'));
else
ct.append(s.charAt(i));
return ct.toString();
String decrypt(String s)
OUTPUT
int i=0;
s=s.toUpperCase();
for(i=0;i<s.length();i++)
if(Character.isLetter(s.charAt(i)))pt.append((char)((((s.charAt(i)-'A')-
key+26)%26)+'A'));
else
pt.append(s.charAt(i));
return pt.toString();
String ct = c.encrypt(pt);
JOptionPane.showMessageDialog(null,ct.toString(),"Ciphertext",JOptionPane.PLAIN_ME
SSAGE);
JOptionPane.showMessageDialog(null,pt1.toString(),"decryptedtext",JOptionPane.PLAIN
_MESSAGE);
OUTPUT
b) Substitution Cipher
package Crypto;
import javax.swing.*;
class SubstitutionCipher {
String key;
SubstitutionCipher(String k) {
key = k.toUpperCase();
plaintext = plaintext.toUpperCase();
if (Character.isLetter(c)) {
ciphertext.append(key.charAt(index));
} else {
ciphertext.append(c);
return ciphertext.toString();
ciphertext = ciphertext.toUpperCase();
if (Character.isLetter(c)) {
plaintext.append(ALPHABET.charAt(index));
} else {
plaintext.append(c);
return plaintext.toString();
String ct = sc.encrypt(pt);
OUTPUT
c) Hill Cipher
package Crypto;
import javax.swing.*;
public class HillCipher {
private static final int MATRIX_SIZE = 2; // Using a 2x2 matrix for encryption
// Method to multiply key matrix with the message vector
public static int[] multiplyMatrix(int[][] keyMatrix, int[] messageVector) {
int[] result = new int[MATRIX_SIZE]; // Resultant vector after multiplication
for (int i = 0; i < MATRIX_SIZE; i++) {
result[i] = 0;
for (int j = 0; j < MATRIX_SIZE; j++) {
result[i] += keyMatrix[i][j] * messageVector[j];
}
result[i] %= 26; // Modulo 26 ensures result stays within A-Z
}
return result;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error: " + e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
}
}
}
OUTPUT
package Crypto;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.*;
try {
return;
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
} catch (Exception e) {
if (hex.length() == 1) {
hexString.append('0');
hexString.append(hex);
return hexString.toString();
} }
OUTPUT
import javax.swing.*;
import java.math.BigInteger;
import java.security.SecureRandom;
class RSA {
private BigInteger n, d, e;
public RSA() {
n = p.multiply(q);
d = e.modInverse(phi);
// Encrypt a message
formattedText.append(text, i, end).append("\n");
return formattedText.toString();
try {
} catch (Exception e) {
} } }
OUTPUT
import javax.swing.*;
class RailFenceCipher {
int key;
RailFenceCipher(int k) {
key = k;
rail[i][j] = '\n';
} }
dirDown = !dirDown;
rail[row][col++] = plaintext.charAt(i);
if (rail[i][j] != '\n') {
ciphertext.append(rail[i][j]);
} }
return ciphertext.toString();
rail[i][j] = '\n';
dirDown = !dirDown;
rail[row][col++] = '*';
int index = 0;
rail[i][j] = ciphertext.charAt(index++);
} }
row = 0;
col = 0;
dirDown = !dirDown;
if (rail[row][col] != '\n') {
plaintext.append(rail[row][col++]);
return plaintext.toString();
String ct = rf.encrypt(pt);
} }
OUTPUT
package Crypto;
import javax.swing.*;
class VernamCipher {
ciphertext.append(encryptedChar);
return ciphertext.toString();
plaintext.append(decryptedChar);
return plaintext.toString();
if (key.length() != pt.length()) {
return;
OUTPUT
Calculate the message digest of a text using the SHA-1 algorithm in java
package Crypto;
import javax.swing.*;
import java.security.MessageDigest;
class SHA1Hash {
hexString.append(String.format("%02x", b));
return hexString.toString();
try {
} catch (Exception e) {
OUTPUT
import javax.swing.*;
import java.security.*;
import java.util.Base64;
class DigitalSignatureExample {
signature.initSign(privateKey);
signature.update(message.getBytes());
return Base64.getEncoder().encodeToString(signatureBytes);
signature.initVerify(publicKey);
signature.update(message.getBytes());
return signature.verify(signatureBytes);
int chunkSize = 50; // Set chunk size for splitting the signature
formattedSignature.append(signature, i, end).append("\n");
return formattedSignature.toString();
try {
keyGen.initialize(2048);
} catch (Exception e) {
} }
OUTPUT
Compute common secret key between client and server using Diffie-Hellman
key exchange technique. Perform encryption and decryption of message using
the shared secret key (Use simple XOR operation to encrypt and decrypt the
message).
package Crypto;
import javax.swing.*;
import java.math.BigInteger;
import java.security.SecureRandom;
class DiffieHellman {
return result.toString();
try {
// Both Client and Server now have the same shared key (they can encrypt and
decrypt using this key)
} catch (Exception e) {
OUTPUT
Implement DSS algorithm for signing and verification of messages between two
parties (obtain H (M) using simple XOR method of hash computation on M).
package Crypto;
import javax.swing.*;
import java.security.*;
class DSS {
int hash = 0;
hash ^= c;
return Integer.toHexString(hash);
try {
} catch (Exception e) {