Crypto
Crypto
import java.util.Scanner;
class one {
inputStr = inputStr.toLowerCase();
encryptStr += encryptChar;
return encryptStr;
inputStr = inputStr.toLowerCase();
if (decryptPos < 0) {
return decryptStr;
shifted:");
System.out.println( encryptData(inputStr,
shiftKey));System.out.println(
sc.close();
PLAYFAIR
import java.util.Scanner;
int count = 0;
if (plain[i] != '\u0000') {
plain[count++] = plain[i];
return count;
if (key[i] != 'j') {
dicty[key[i] - 97] = 2;
dicty['j' - 97] = 1;
int i = 0, j = 0;
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
if (dicty[k] == 0) {
j++;
if (j == 5) {
i++;
j = 0;
if (a == 'j') a = 'i';
if (b == 'j') b = 'i';
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
} else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
return (a % 5);
}
str[i + 1] = 'x';
ptrs++;
if (ptrs % 2 != 0) {
str[ptrs++] = 'x';
return ptrs;
if (a[0] == a[2]) {
} else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
}
}
ks = key.length;
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = str.length;
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
arr[i] = s.charAt(i);
System.out.println("SUJJAL MINHAS");
System.out.println("22BCB0092");
Scanner scanner = new Scanner(System.in);
strcpy(str, input1);
strcpy(key, input2);
int ps = input1.length();
int ks = input2.length();
strcpy(originalStr, input1);
encryptByPlayfairCipher(str, key);
System.out.println(result);
if (modifiedString.charAt(modifiedString.length() - 1) != 'x') {
modifiedString += "x";
System.out.println(modifiedString.toLowerCase());
}
}
Vigenere
import java.util.*;
String pt = sc.nextLine().toUpperCase();
System.out.println(encryptedText);
System.out.println(pt);
if (Character.isAlphabetic(currentChar)) {
cipher.append(encryptedChar);
j++;
} else {
cipher.append(currentChar);
}}
return cipher.toString();
AES
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
import java.util.Scanner;
Base64.getEncoder().encodeToString(roundKeys[i]));
}
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes =
cipher.doFinal(Base64.getDecoder().decode(encryptedText));
roundKeys[0] = key;
return roundKeys;
}
DES
import java.util.Base64;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.util.Scanner;
System.out.println("SUJJAL MINHAS");
System.out.println("22BCB0092");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
scanner.close();
HILL CIPHER
import java.util.Scanner;
sc.close();
System.out.println(encryptedText.toString());
System.out.println(plaintext);
if (plaintext.length() % keySize != 0) {
plaintext += "x".repeat(padding);
return plaintext;
int k = 0;
for (int i = 0; i < keySize; i++) {
return matrix;
return result;
RSA
import java.math.BigInteger;
import java.util.Scanner;
int p = scanner.nextInt();
int q = scanner.nextInt();
scanner.nextLine();
int n = p * q;
int e = findE(phi);
System.out.println(e);
System.out.println(d);
System.out.println(encrypted);
System.out.println(decrypted);
if (gcd(e, phi) == 1) {
return e;
return 2;
int d = 1;
d++;
return d;
if (b == 0) return a;
BigInteger.valueOf(n));
encrypted.append((char) encryptedChar.intValue()); // Convert back to char
return encrypted.toString();
BigInteger.valueOf(n));
decrypted.append((char) decryptedChar.intValue());
return decrypted.toString();
DIFFIE HELLMAN
import java.math.BigInteger;
import java.util.Scanner;
System.out.println("SUJJAL MINHAS");
System.out.println("22BCB0092");
System.out.println(publicA);
System.out.println(publicB);
System.out.println(secretA);
System.out.println(secretB);
if(secretA == secretB){
securely");
}else{
return BigInteger.valueOf(base).modPow(BigInteger.valueOf(exp),
BigInteger.valueOf(mod)).intValue();
ELGAMAL
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;
BigInteger key;
do {
return key;
}
BigInteger k = generateKey(q);
encryptedMessage[0] = p;
encryptedMessage[i + 1] = s.multiply(BigInteger.valueOf((int)
message.charAt(i))).mod(q);
return encryptedMessage;
BigInteger p = encryptedMessage[0];
encryptedMessage[i].multiply(sInverse).mod(q).intValue());
return decryptedMessage.toString();
BigInteger q = generatePrime(64);
System.out.println();
ELLIPTIC CURVE
import java.math.BigInteger;
import java.util.Scanner;
static int a, b, p;
static int k;
System.out.print("Enter a, b, p: ");
a = sc.nextInt();
b = sc.nextInt();
p = sc.nextInt();
System.out.print("Enter Generator point G (x y): ");
G[0] = sc.nextInt();
G[1] = sc.nextInt();
Pm[0] = sc.nextInt();
Pm[1] = sc.nextInt();
Pb[0] = sc.nextInt();
Pb[1] = sc.nextInt();
k = sc.nextInt();
sc.close();
System.out.println("Ciphertext:");
int[] Q = P;
while (k > 0) {
Q = pointDoubling(Q);
k >>= 1;
return R;
a = (a % p + p) % p;
b = (b % p + p) % p;
return (a * inv) % p;
}
CHINESE REMAINDER THEOREM
import java.util.*;
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1) return 0;
while (a > 1) {
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
return x1;
}
public static int findX(int[] num, int[] rem, int k) {
int prod = 1;
prod *= num[i];
int result = 0;
while (scanner.hasNextInt()) {
int r = scanner.nextInt();
int n = scanner.nextInt();
num.add(n);
rem.add(r);
}
scanner.close();
System.out.println(x);
import java.util.*;
int m0 = m, t, q;
int x0 = 0, x1 = 1;
if (m == 1) return 0;
while (a > 1) {
q = a / m;
t = m;
m = a % m;
a = t;
t = x0;
x0 = x1 - q * x0;
x1 = t;
return x1;
System.out.print("Enter a: ");
int a = scanner.nextInt();
System.out.print("Enter m: ");
int m = scanner.nextInt();
scanner.close();
}
RC4
import java.util.Scanner;
S[i] = (byte) i;
int j = 0;
swap(S, i, j);
array[i] = array[j];
array[j] = temp;
int i = 0, j = 0;
i = (i + 1) & 0xFF;
swap(S, i, j);
keystream[k] = (byte) ((S[(S[i] + S[j]) & 0xFF]) & 0x07); // Ensure 3-bit values
return keystream;
return output;
line = scanner.nextLine().trim();
if (scanner.hasNextInt()) {
scanner.nextLine();
rc4.initializeKey(key);
System.out.println(to3BitBinaryString(plainText));
System.out.println(to3BitBinaryString(key));
System.out.println(toDecimalString(keystream));
System.out.println(to3BitBinaryString(encryptedText));
System.out.println(to3BitBinaryString(decryptedText));
scanner.close();
return binaryString.toString();
return decimalString.toString().trim();
}
FERMETS THEOREM
import java.util.Scanner;
int result = 1;
return result;
Scanner sc = newScanner(System.in);
int a = sc.nextInt();
int m= sc.nextInt();
System.out.println("Modular Inverse of " + a + " mod" + m + " is: " + modInverse(a, m));