NS LAB Xerox
NS LAB Xerox
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.util.Base64;
public class Aes { public static void main(String[] args) throws Exception {
String message = "Hello, AES!";
SecretKey key = KeyGenerator.getInstance("AES").generateKey();
byte[] iv = generateIV();
byte[] ciphertext = aesEncrypt(message, key, iv);
String decryptedMessage = aesDecrypt(ciphertext, key, iv);
System.out.println("Original Message: " + message);
System.out.println("Encrypted Message: " +
Base64.getEncoder().encodeToString(ciphertext));
System.out.println("Decrypted Message: " + decryptedMessage);
}
public static byte[] aesEncrypt(String message, SecretKey key, byte[] iv) throws Exception
{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv));
byte[] ciphertext = cipher.doFinal(message.getBytes("UTF-8"));
return ciphertext;
}
public static String aesDecrypt(byte[] ciphertext, SecretKey key, byte[] iv) throws
Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
byte[] decryptedBytes = cipher.doFinal(ciphertext);
return new String(decryptedBytes, "UTF-8");
}
public static byte[] generateIV() {
byte[] iv = new byte[16];
SecureRandom random = new SecureRandom();
random.nextBytes(iv); return iv;
}
}
OUTPUT:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class Des{ public static void main(String[] args) throws Exception {
String message = "Hello, DES!";
SecretKey key = KeyGenerator.getInstance("DES").generateKey();
byte[] ciphertext = desEncrypt(message, key);
String decryptedMessage = desDecrypt(ciphertext, key);
System.out.println("Original Message: " + message);
2
lOMoARcPSD|37165109
System.out.println("Encrypted Message: " + encodeBase64(ciphertext));
System.out.println("Decrypted Message: " + decryptedMessage);
}
public static byte[] desEncrypt(String message, SecretKey key) throws Exception {
Cipher
cipher
=
cipher.init(Cipher.ENCRYPT_MODE, key);
Cipher.getInstance("DES/ECB/PKCS5Padding");
byte[] ciphertext = cipher.doFinal(message.getBytes());
return ciphertext;
}
public static String desDecrypt(byte[] ciphertext, SecretKey key) throws Exception {
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedBytes = cipher.doFinal(ciphertext);
return new String(decryptedBytes);
}
public static String encodeBase64(byte[] bytes) {
return javax.xml.bind.DatatypeConverter.prin
OUTPUT:
import java.security.Key;
import java.util.Scanner;
public class RC4 { private final int[] S = new int[256];
private final int[] T = new int[256];
private final int keyLength;
private byte[] key;
public RC4(byte[] key) {
if (key.length < 1 || key.length > 256) {
throw new IllegalArgumentException("RC4 key must be between 1 and 256 bytes");
}
this.key = key;
this.keyLength = key.length;
for (int i = 0; i < 256; i++) {
S[i] = i;
T[i] = key[i % keyLength];
}
int j = 0;
for (int i = 0; i < 256; i++) {
j = (j + S[i] + T[i]) % 256;
int temp = S[i];
S[i] = S[j];
S[j] = temp;
}
}
public byte[] encrypt(byte[] plaintext) {
byte[] ciphertext = new byte[plaintext.length];
int i = 0, j = 0;
for (int k = 0;
k < plaintext.length; k++) {
i = (i + 1) % 256;
j = (j + S[i]) % 256;
int temp = S[i];
S[i] = S[j];
S[j] = temp;
int t = (S[i] + S[j]) % 256;
int K = S[t];
ciphertext[k] = (byte) (plaintext[k] ^ K);
}
return ciphertext;
}
public byte[] decrypt(byte[] ciphertext) {
return encrypt(ciphertext); // Since RC4 is a symmetric cipher, decryption is the same
as encryption
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a plaintext message: ");
String plaintext = scanner.nextLine();
System.out.print("Enter a key (1 to 256 bytes): ");
byte[] key = scanner.nextLine().getBytes();
RC4 rc4 = new RC4(key);
byte[] encrypted = rc4.encrypt(plaintext.getBytes());
byte[] decrypted = rc4.decrypt(encrypted);
System.out.println("Encrypted
message(Base64):"+java.util.Base64.getEncoder().encodeToString(encrypted));
System.out.println("Decrypted message: " + new String(decrypted));
}
}
OUTPUT:
import java.math.BigInteger;
import java.security.SecureRandom;
public class RSA { private BigInteger
privateKey;
private BigInteger publicKey;
private BigInteger modulus;
public RSA(int bitLength) {
SecureRandom random = new SecureRandom();
BigInteger p = BigInteger.probablePrime(bitLength / 2, random);
BigInteger q = BigInteger.probablePrime(bitLength / 2, random);
modulus = p.multiply(q);
BigInteger phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
publicKey
= BigInteger.probablePrime(bitLength / 4, random);
while (phi.gcd(publicKey).compareTo(BigInteger.ONE) > 0 && publicKey.compareTo(phi)
<
0) { publicKey = publicKey.add(BigInteger.ONE);
}
privateKey = publicKey.modInverse(phi);
}
public BigInteger encrypt(BigInteger message) { return
message.modPow(publicKey, modulus);
}
public BigInteger decrypt(BigInteger ciphertext) { return
ciphertext.modPow(privateKey, modulus); }
public static void main(String[] args) {
RSA rsa = new RSA(1024);
BigInteger plaintext = BigInteger.valueOf(123456);
BigInteger ciphertext = rsa.encrypt(plaintext);
BigInteger decryptedText = rsa.decrypt(ciphertext);
System.out.println("Original: " + plaintext);
System.out.println("Encrypted: " + ciphertext);
System.out.println("Decrypted: " + decryptedText);
}
}
OUTPUT:
import java.math.BigInteger;
import java.security.SecureRandom;
public class DiffieHellman { private static final BigInteger ONE =
BigInteger.ONE;
private static final BigInteger TWO = BigInteger.valueOf(2);
public static void main(String[] args) {
BigInteger p = BigInteger.probablePrime(512, new SecureRandom());
BigInteger g = BigInteger.probablePrime(128, new SecureRandom());
BigInteger a = new BigInteger(512, new SecureRandom());
BigInteger b = new BigInteger(512, new SecureRandom());
BigInteger A = g.modPow(a, p);
BigInteger B = g.modPow(b, p);
BigInteger secretA = B.modPow(a, p);
BigInteger secretB = A.modPow(b, p);
System.out.println("Alice's secret: " + secretA);
System.out.println("Bob's secret: " + secretB);
}
}
OUTPUT:
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Random;
import java.io.IOException;
public class ElGamal {
public static void main(String[] args) throws IOException {
BigInteger p, b, c, secretKey;
Random sc = new SecureRandom();
secretKey = new BigInteger("12345678901234567890");
// public key calculation
System.out.println("secretKey = " + secretKey);
p = BigInteger.probablePrime(64, sc);
b = new BigInteger("3");
c = b.modPow(secretKey, p);
System.out.println("p = " + p);
System.out.println("b = " + b);
System.out.println("c = " + c);
// Encryption
System.out.print("Enter your Big Number message --> ");
String s = tools.getString(); // Assuming Tools.getString() is a method available elsewhere
BigInteger X = new BigInteger(s);
BigInteger r = new BigInteger(64, sc);
BigInteger EC = X.multiply(c.modPow(r, p)).mod(p);
BigInteger brmodp = b.modPow(r, p);
System.out.println("Plaintext = " + X);
System.out.println("r = " + r);
System.out.println("EC = " + EC);
System.out.println("b^r mod p = " + brmodp);
// Decryption
BigInteger crmodp = brmodp.modPow(secretKey, p);
BigInteger d = crmodp.modInverse(p);
BigInteger ad = d.multiply(EC).mod(p);
System.out.println("\n\nc^r mod p = " + crmodp);
System.out.println("d = " + d);
System.out.println("Alice decodes: " + ad);
}
}
OUTPUT:
import java.security.*;
public class DigitalSignatureExample {
public static void main(String[] args) {
try {
keyPairGenerator.initialize(2048);
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
String data = "Hello, this is a message to be signed.";
Signature.initSign(privateKey);
Signature.update(data.getBytes());
byte[] digitalSignature = signature.sign();
Signature.initVerify(publicKey);
Signature.update(data.getBytes());
boolean verified = signature.verify(digitalSignature);
if (verified) {
System.out.println("Signature verified successfully.");
} else {
System.out.println("Signature verification failed.");
}
} catch (Exception e) {
System.err.println("Exception: " + e);
}
}
}
OUTPUT: