Professional Documents
Culture Documents
Lab Cat Crypto Final
Lab Cat Crypto Final
import java.util.Scanner;
scanner.close();
}
}
Verman Cipher
import java.util.Scanner;
public class Main {
// Function to encrypt plaintext using Vigenère cipher
public static String encrypt(String plaintext, String key) {
StringBuilder encryptedText = new StringBuilder();
plaintext = plaintext.toUpperCase();
key = key.toUpperCase();
for (int i = 0, j = 0; i < plaintext.length(); i++) {
char currentChar = plaintext.charAt(i);
if (Character.isLetter(currentChar)) {
int shift = key.charAt(j) - 'A';
char encryptedChar = (char) ((currentChar + shift - 'A') % 26 + 'A');
encryptedText.append(encryptedChar);
j = (j + 1) % key.length();
} else {
encryptedText.append(currentChar);
}
}
return encryptedText.toString();
}
// Function to decrypt ciphertext using Vigenère cipher
public static String decrypt(String ciphertext, String key) {
StringBuilder decryptedText = new StringBuilder();
ciphertext = ciphertext.toUpperCase();
key = key.toUpperCase();
for (int i = 0, j = 0; i < ciphertext.length(); i++) {
char currentChar = ciphertext.charAt(i);
if (Character.isLetter(currentChar)) {
int shift = key.charAt(j) - 'A';
char decryptedChar = (char) ((currentChar - shift - 'A' + 26) % 26 + 'A');
decryptedText.append(decryptedChar);
j = (j + 1) % key.length();
} else {
decryptedText.append(currentChar);
}
}
return decryptedText.toString();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input plaintext from user
System.out.print("Enter the plaintext: ");
String plaintext = scanner.nextLine();
// Input key from user
System.out.print("Enter the key: ");
String key = scanner.nextLine();
// Encrypt the plaintext
String encryptedText = encrypt(plaintext, key);
System.out.println("Encrypted text: " + encryptedText);
// Decrypt the ciphertext
String decryptedText = decrypt(encryptedText, key);
System.out.println("Decrypted text: " + decryptedText);
scanner.close();
}
}
#include<iostream>
#include<math.h>
using namespace std;
float encrypt[3][1], decrypt[3][1], a[3][3], b[3][3], mes[3][1], c[3][3];
void encryption(); //encrypts the message
void decryption(); //decrypts the message
void getKeyMessage(); //gets key and message from user
void inverse(); //finds inverse of key matrix
int main() {
getKeyMessage();
encryption();
decryption();
}
void encryption() {
int i, j, k;
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
encrypt[i][j] = encrypt[i][j] + a[i][k] * mes[k][j];
cout<<"\nEncrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(encrypt[i][0], 26) + 97);
}
void decryption() {
int i, j, k;
inverse();
for(i = 0; i < 3; i++)
for(j = 0; j < 1; j++)
for(k = 0; k < 3; k++)
decrypt[i][j] = decrypt[i][j] + b[i][k] * encrypt[k][j];
cout<<"\nDecrypted string is: ";
for(i = 0; i < 3; i++)
cout<<(char)(fmod(decrypt[i][0], 26) + 97); cout<<"\n";
}
void getKeyMessage() {
int i, j;
char msg[3];
void inverse() {
int i, j, k;
float p, q;
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++) {
if(i == j) b[i][j]=1;
else b[i][j]=0;
}
for(k = 0; k < 3; k++) {
for(i = 0; i < 3; i++) {
p = c[i][k]; q = c[k][k];
for(j = 0; j < 3; j++) {
if(i != k) {
c[i][j] = c[i][j]*q - p*c[k][j];
b[i][j] = b[i][j]*q - p*b[k][j];
}
}
}
}
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
b[i][j] = b[i][j] / c[i][i];
cout<<"\n\nInverse Matrix is:\n";
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++)
cout<<b[i][j]<<" ";
cout<<"\n";
}
}
// Encryption
String ciphertext = encrypt(plaintext, key);
System.out.println("Encrypted text: " + ciphertext);
// Decryption
String decryptedText = decrypt(ciphertext, key);
System.out.println("Decrypted text: " + decryptedText);
scanner.close();
}
}
return result.toString();
}
row = 0;
col = 0;
for (int i = 0; i < cipher.length(); i++) {
// check the direction of flow
if (row == 0)
dirDown = true;
if (row == key - 1)
dirDown = false;
scanner.close();
}
}
class AES {
// Class private variables
private static final String SALT = "ssshhhhhhhhhhh!!!!";
// driver code
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
scanner.close();
}
}
#include <iostream>
#include <iomanip>
#include <vector>
if (i % Nk == 0) {
// Rotate the bytes and apply S-box to the first byte
temp = {Sbox[temp[1]], Sbox[temp[2]], Sbox[temp[3]], Sbox[temp[0]]};
// XOR with the round constant
temp[0] ^= Rcon[i / Nk - 1];
}
// XOR with the Nk-th previous word
for (int j = 0; j < 4; ++j) {
roundKeys[i][j] = roundKeys[i - Nk][j] ^ temp[j];
}
}
}
int main() {
// User input for the key (4x4 matrix)
std::vector<std::vector<byte>> keyMatrix(4, std::vector<byte>(4));
std::cout << "Enter the 16-byte key in matrix format (4x4) in hexadecimal:" <<
std::endl;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
std::string hexInput;
std::cout << "Row " << i + 1 << ", Column " << j + 1 << ": ";
std::cin >> hexInput;
keyMatrix[i][j] = std::stoi(hexInput, nullptr, 16);
} }
// Key expansion
std::vector<std::vector<byte>> roundKeys;
KeyExpansion(reinterpret_cast<byte*>(keyMatrix.data()), roundKeys);
return 0;
}
#include <iostream>
using namespace std;
//Functions
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
}
//S0
int rowS0bin[2] = {a[0], a[3]}, colS0bin[2] = {a[1], a[2]};
int rowS0dec = bin2dec(rowS0bin, 2), colS0dec = bin2dec(colS0bin, 2);
int opS0dec = S0[rowS0dec][colS0dec];
int opS0bin[2] = {};
dec2bin(opS0dec, opS0bin);
//S1
int rowS1bin[2] = {b[0], b[3]}, colS1bin[2] = {b[1], b[2]};
int rowS1dec = bin2dec(rowS1bin, 2), colS1dec = bin2dec(colS1bin, 2);
int opS1dec = S1[rowS1dec][colS1dec];
int opS1bin[2] = {};
dec2bin(opS1dec, opS1bin);
class KeyGeneration {
private:
int P10_rule[10] = {3, 5, 2, 7, 4, 10, 1, 9, 8, 6};
int P8_rule[8] = {6, 3, 7, 4, 8, 5, 10, 9};
int temp_left[5] = {}, temp_right[5] = {};
public:
KeyGeneration() {
cout << endl;
cout << "KEY GENERATION.." << endl;
cout << endl;
}
void key(int master_key[], int *k1, int *k2) {
Permutation(master_key, P10_rule, 10);
cout << "After P10 Permutation: ";
printArray(master_key, 10);
cout << endl;
Split(master_key, 10, temp_left, temp_right);
cout << "After split, " << endl;
cout << "l = ";
printArray(temp_left, 5);
cout << "r = ";
printArray(temp_right, 5);
cout << endl;
leftRotate(temp_left, 1, 5);
leftRotate(temp_right, 1, 5);
cout << "After LeftShift-1, " << endl;
cout << "l = ";
printArray(temp_left, 5);
cout << "r = ";
printArray(temp_right, 5);
cout << endl;
combine(temp_left, temp_right, master_key, 10);
Permutation(master_key, P8_rule, 10);
cout << "After P8, Key-1: ";
for (int i = 0; i < 8; i++)
k1[i] = master_key[i];
printArray(k1, 8);
cout << endl;
leftRotate(temp_left, 2, 5);
leftRotate(temp_right, 2, 5);
cout << "After LeftShift-2, " << endl;
cout << "l = ";
printArray(temp_left, 5);
cout << "r = ";
printArray(temp_right, 5);
cout << endl;
combine(temp_left, temp_right, master_key, 10);
Permutation(master_key, P8_rule, 10);
cout << "After P8, Key-2: ";
for (int i = 0; i < 8; i++)
k2[i] = master_key[i];
printArray(k2, 8);
}
};
class Roundfunction {
private:
int Expanrule[8] = {4, 1, 2, 3, 2, 3, 4, 1};
int P4_rule[4] = {2, 4, 3, 1};
int r_arr2[8] = {}, a[4] = {}, b[4] = {};
int opS0S1[4] = {};
public:
void roundfun(int *k1, int *l_arr, int *r_arr, int *fk1) {
ExPermutation(r_arr, Expanrule, r_arr2, 8);
cout << "After EP: ";
printArray(r_arr2, 8);
cout << endl;
XOR(k1, r_arr2, 8);
cout << "XOR with key" << endl;
printArray(r_arr2, 8);
cout << endl;
Split(r_arr2, 8, a, b);
cout << "After Split" << endl;
cout << "l = ";
printArray(a, 4);
cout << "r = ";
printArray(b, 4);
cout << endl;
S_box(a, b, opS0S1);
Permutation(opS0S1, P4_rule, 4);
cout << "After P4" << endl;
printArray(opS0S1, 4);
cout << endl;
XOR(opS0S1, l_arr, 4);
cout << "XOR with leftarray" << endl;
printArray(l_arr, 4);
cout << endl;
combine(l_arr, r_arr, fk1, 8);
cout << "After combine" << endl;
printArray(fk1, 8);
cout << endl;
}
};
public:
encrypt() {
cout << endl;
cout << "ENCRYPTING.." << endl;
cout << endl;
}
public:
decrypt() {}
int main() {
char input;
int arr[8] = {}, master_key[10] = {}, k1[8] = {}, k2[8] = {}, fk1[8] = {};
//Key
cout << "Enter 10-bit Master Key (using space)" << endl;
for (int i = 0; i < 10; i++) {
cin >> master_key[i];
}
return 0;
}
System.out.println("Enter a binary string (64 bits) for DES initial and final
permutation:");
String inputBlockString = scanner.nextLine();
scanner.close();
}
RSA (numbers)
#include <iostream>
#include <cmath>
// Function to calculate the greatest common divisor (GCD) using Euclid's algorithm
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
if (m == 1) {
return 0;
}
while (a > 1) {
int q = a / m;
int t = m;
m = a % m, a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0) {
x += m0;
}
return x;
}
int main() {
// Select two large prime numbers p and q
int p, q;
std::cout << "Enter first prime number (p): ";
std::cin >> p;
std::cout << "Enter second prime number (q): ";
std::cin >> q;
// Calculate n, e, and d
int e, d, n;
generate_keys(p, q, e, d, n);
// Enter plaintext
int plaintext;
std::cout << "Enter plaintext: ";
std::cin >> plaintext;
// Encrypt plaintext
int ciphertext = encrypt(plaintext, e, n);
std::cout << "Encrypted ciphertext: " << ciphertext << std::endl;
// Decrypt ciphertext
int decrypted_text = decrypt(ciphertext, d, n);
std::cout << "Decrypted plaintext: " << decrypted_text << std::endl;
return 0;
}
RSA (text)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
long int p,q,n,t,flag,e[100],d[100],temp[100],j,m[100],en[100],i;
char msg[100];
int prime(long int);
void ce();
long int cd(long int);
void encrypt();
void decrypt();
void main()
{
printf("\nENTER FIRST PRIME NUMBER\n");
scanf("%d",&p);
flag=prime(p);
if(flag==0) {
printf("\nWRONG INPUT\n");
getchar();
exit(1);
}
printf("\nENTER ANOTHER PRIME NUMBER\n");
scanf("%d",&q);
flag=prime(q);
if(flag==0||p==q) {
printf("\nWRONG INPUT\n");
getchar();
exit(1);
}
printf("\nENTER MESSAGE\n");
fflush(stdin);
scanf("%s",msg);
for (i=0;msg[i]!=NULL;i++)
m[i]=msg[i];
n=p*q;
t=(p-1)*(q-1);
ce();
printf("\nPOSSIBLE VALUES OF e AND d ARE\n");
for (i=0;i<j-1;i++)
printf("\n%ld\t%ld",e[i],d[i]);
encrypt();
decrypt();
getchar();
}
int prime(long int pr) {
int i;
j=sqrt(pr);
for (i=2;i<=j;i++) {
if(pr%i==0)
return 0;
}
return 1;
}
void ce() {
int k=0;
for (i=2;i<t;i++) {
if(t%i==0)
continue;
flag=prime(i);
if(flag==1&&i!=p&&i!=q) {
e[k]=i;
flag=cd(e[k]);
if(flag>0) {
d[k]=flag;
k++;
}
if(k==99)
break;
}
}
}
long int cd(long int x) {
long int k=1;
while(1) {
k=k+t;
if(k%x==0)
return(k/x);
}
}
void encrypt() {
long int pt,ct,key=e[0],k,len;
i=0;
len=strlen(msg);
while(i!=len) {
pt=m[i];
pt=pt-96;
k=1;
for (j=0;j<key;j++) {
k=k*pt;
k=k%n;
}
temp[i]=k;
ct=k+96;
en[i]=ct;
i++;
}
en[i]=-1;
printf("\nTHE ENCRYPTED MESSAGE IS\n");
for (i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}
void decrypt() {
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1) {
ct=temp[i];
k=1;
for (j=0;j<key;j++) {
k=k*ct;
k=k%n;
}
pt=k+96;
m[i]=pt;
i++;
}
m[i]=-1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for (i=0;m[i]!=-1;i++)
printf("%c",m[i]);
}