import java.util.Scanner;
public class CaesarCipher {
// Moves every letter k places forward in the alphabet, wrapping Z back to A
static String shift(String str, int k) {
String result = "";
for (char c : str.toCharArray()) {
if (Character.isUpperCase(c)) c = (char) ('A' + (c - 'A' + k) % 26);
else if (Character.isLowerCase(c)) c = (char) ('a' + (c - 'a' + k) % 26);
result += c;
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any String: ");
String str = sc.nextLine();
System.out.print("\nEnter the Key: ");
int key = sc.nextInt() % 26;
String encrypted = shift(str, key);
System.out.println("\nEncrypted String is: " + encrypted);
System.out.println("\nDecrypted String is: " + shift(encrypted, 26 - key));
System.out.println();
}
}
_______3.b
import java.util.Scanner;
public class SubstitutionCipher {
public static void main(String[] args) {
String a = "abcdefghijklmnopqrstuvwxyzH";
String b = "zyxwvutsrqponmlkjihgfedcbaS";
System.out.print("Enter any string: ");
String str = new Scanner(System.in).nextLine();
String encrypted = "";
for (char c : str.toCharArray())
encrypted += b.charAt(a.indexOf(c)); // partner letter from b
System.out.println("The encrypted data is: " + encrypted);
}
}
_________des algo
import java.util.Base64;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
public class DES {
public static void main(String[] args) throws Exception {
System.out.print("Enter the string: ");
String text = new Scanner(System.in).nextLine();
byte[] keyBytes = "ThisIsSecretEncryptionKey".getBytes("UTF8");
SecretKeyFactory factory = SecretKeyFactory.getInstance("DESede");
SecretKey key = factory.generateSecret(new DESedeKeySpec(keyBytes));
Cipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptedBytes = cipher.doFinal(text.getBytes("UTF8"));
String encrypted = Base64.getEncoder().encodeToString(encryptedBytes);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encrypted));
String decrypted = new String(decryptedBytes, "UTF8");
System.out.println("\nString To Encrypt : " + text);
System.out.println("Encrypted Value : " + encrypted);
System.out.println("Decrypted Value : " + decrypted);
}
}
_______ blowfish
import java.util.Base64;
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class BlowFish {
public static void main(String[] args) throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");
keyGenerator.init(128);
Cipher cipher = Cipher.getInstance("Blowfish/CFB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, keyGenerator.generateKey());
System.out.println("Initialization Vector of the Cipher: "
+ Base64.getEncoder().encodeToString(cipher.getIV()));
System.out.print("Enter text to encrypt: ");
String text = new Scanner(System.in).nextLine();
byte[] encrypted = cipher.doFinal(text.getBytes());
System.out.println("Encrypted Text: "
+ Base64.getEncoder().encodeToString(encrypted));
}
}
_______rij aglo
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class AES {
static String asHex(byte[] bytes) {
String hex = "";
for (byte b : bytes) hex += String.format("%02x", b);
return hex;
}
public static void main(String[] args) throws Exception {
System.out.print("Enter the data to be encrypted: ");
String message = new Scanner(System.in).nextLine();
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey key = kgen.generateKey();
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(message.getBytes("UTF-8"));
System.out.println("Encrypted string: " + asHex(encrypted));
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] original = cipher.doFinal(encrypted);
System.out.println("Original string: " + new String(original, "UTF-8"));
System.out.println("Original data in hexadecimal: " + asHex(original));
}
}
_____rc4
import java.util.Scanner;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class BlowFishCipher {
public static void main(String[] args) throws Exception {
SecretKey key = KeyGenerator.getInstance("Blowfish").generateKey();
Cipher cipher = Cipher.getInstance("Blowfish");
System.out.print("Input your message: ");
String text = new Scanner(System.in).nextLine();
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encrypted = cipher.doFinal(text.getBytes("UTF-8"));
String hex = "";
for (byte b : encrypted) hex += String.format("%02X", b);
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decrypted = cipher.doFinal(encrypted);
System.out.println("Encrypted text: " + hex);
System.out.println("Decrypted text: " + new String(decrypted, "UTF-8"));
}
}
________rsa
import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;
public class RSA {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Prime number: ");
BigInteger p = sc.nextBigInteger();
System.out.print("Enter another prime number: ");
BigInteger q = sc.nextBigInteger();
BigInteger n = p.multiply(q);
BigInteger one = BigInteger.ONE;
BigInteger phi = p.subtract(one).multiply(q.subtract(one));
// Pick a random e (greater than 2) with gcd(e, phi) = 1
Random r = new Random();
BigInteger e;
do {
e = BigInteger.valueOf(r.nextInt(phi.intValue() - 1));
} while (e.intValue() <= 2 || !phi.gcd(e).equals(BigInteger.ONE));
BigInteger d = e.modInverse(phi); // d x e = 1 (mod phi)
System.out.println("Encryption keys are: " + e + ", " + n);
System.out.println("Decryption keys are: " + d + ", " + n);
}
}
____hellman
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import javax.crypto.spec.DHParameterSpec;
import javax.crypto.spec.DHPublicKeySpec;
public class DiffeHellman {
// Generates a key pair and prints its public key
static void printPublicKey(KeyPairGenerator kpg) throws Exception {
DHPublicKeySpec kspec = KeyFactory.getInstance("DiffieHellman")
.getKeySpec(kpg.generateKeyPair().getPublic(), DHPublicKeySpec.class);
System.out.println("Public key is: " + kspec);
}
public static void main(String[] args) throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DiffieHellman");
kpg.initialize(512);
printPublicKey(kpg);
SecureRandom rnd = new SecureRandom();
BigInteger p = BigInteger.probablePrime(512, rnd);
BigInteger g = BigInteger.probablePrime(512, rnd);
kpg.initialize(new DHParameterSpec(p, g));
System.out.println();
printPublicKey(kpg);
}
}
_____
Program
import java.security.MessageDigest;
public class SHA1 {
public static void main(String[] args) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA1");
System.out.println("Message digest object info: ");
System.out.println(" Algorithm = " + md.getAlgorithm());
System.out.println(" Provider = " + md.getProvider());
System.out.println(" ToString = " + md);
String[] inputs = { "", "abc", "abcdefghijklmnopqrstuvwxyz" };
for (String input : inputs) {
String hex = "";
for (byte b : md.digest(input.getBytes()))
hex += String.format("%02X", b);
System.out.println("\nSHA1(\"" + input + "\") = " + hex);
}
System.out.println();
}
}
____md5
import java.security.MessageDigest;
public class MD5 {
public static void main(String[] args) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
System.out.println("Message digest object info: ");
System.out.println(" Algorithm = " + md.getAlgorithm());
System.out.println(" Provider = " + md.getProvider());
System.out.println(" ToString = " + md);
String[] inputs = { "", "abc", "abcdefghijklmnopqrstuvwxyz" };
for (String input : inputs) {
String hex = "";
for (byte b : md.digest(input.getBytes()))
hex += String.format("%02X", b);
System.out.println("\nMD5(\"" + input + "\") = " + hex);
}
System.out.println();
}
}
_______