Untitled
unknown
plain_text
2 years ago
16 kB
1
Indexable
Never
//ecludean algo #include<stdio.h> #include<conio.h> int main() { int a, b, q, r, t; printf("Enter number a: "); scanf("%d", &a); printf("Enter number b: "); scanf("%d", &b); // large case if (b>a) { t = a; a = b; b = t; } printf("q\ta\tb\tr\n"); do { q = a/b; r = a%b; printf("%d\t%d\t%d\t%d\n", q, a, b, r); a = b; b = r; if(b==0) { printf("%d\t%d\t%d\t%d\n", q, a, b, r); } }while(b!=0); printf("GCD = %d", a); getch(); return 0; } //extended ecludean #include<stdio.h> #include<conio.h> int main() { int a, b, q, r, temp, s1=1, s2=0, s, t1=0, t2=1, t, N; printf("Enter number a: "); scanf("%d", &a); printf("Enter number b: "); scanf("%d", &b); // large case if (b>a) { temp = a; a = b; b = temp; } N = a; printf("q\ta\tb\tr\ts1\ts2\ts\tt1\tt2\tt\n"); do { q = a/b; r = a%b; s = s1 - q*s2; t = t1 - q*t2; printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", q, a, b, r, s1, s2, s, t1, t2, t); a = b; b = r; s1 = s2; s2 = s; t1 = t2; t2 = t; if(b==0) { printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", q, a, b, r, s1, s2, s, t1, t2, t); } }while(b!=0); printf("GCD = %d\n", a); //multiplicative inverse (t1) t1>0 ? printf("Multiplicative inverse: %d", t1): printf("Multiplicative inverse: %d", t1+N); getch(); return 0; } //diffe hellman #include<stdio.h> #include<conio.h> #include<math.h> // global parameter int p = 11, g = 2; int a, b; int main() { int x, y, ssk1, ssk2; printf("Alice Choice: "); scanf("%d", &x); printf("Bob Choice: "); scanf("%d", &y); // Alice a = (long int) pow(g, x) % p; // Bob b = (long int) pow(g, y) % p; // SSK for ALICE ssk1 = (long int) pow(b, x) % p; // SSK for BOB ssk2 = (long int) pow(a, y) % p; printf("SSK1 = %d and SSK2 = %d", ssk1, ssk2); getch(); return 0; } //rsa #include<stdio.h> #include<conio.h> #include<math.h> // 1. select p and q // 2. n = p*q // 3. phi = (p-1)*(q-1) // 4. choose public key e such that 1<e<phi, gcd(e, phi)=1 // 5. calculate privat key: e*d = 1 mod phi // 6. Encryption: c = m^e mod n -- m message, c cipher // 7. Decryption: m = c^d mod n long long int p = 7, q = 11; long long int gcd(long long int, long long int); //long int mypow(long int, long int); int main() { long long int n, phi, e, d; long long int m = 2, c; //2 n = p*q; // 77 //3 Euler toitent function phi = (p-1)*(q-1); // 60 //4. choose public key p: printf("Enter public component: "); scanf("%lld", &e); // 13 // range 1<e<phi, gcd(e,phi)==1 // printf("GCD = %ld", gcd(phi, e)); // printf("GCD = %ld\n", gcd(phi, e)); if(!(e>1 && e<phi && gcd(phi,e)==1)) { printf("Public component is not selected properly.\n"); goto p; } //printf("Okay!"); // 5 find private key e * d = 1 mod phi // Simple searching strategy // In real implementation use proper algorithm. d = 2; while(e*d % phi != 1) { // printf("checking = %ld\n", d); d++; } printf("\nPrivate Key = %ld\n", d); // 6. Encryption //printf("\nm^e = %lld\n", (long long int) pow(m, e)); c = (long long int) pow(m, e) % n; printf("\nCipher: %lld\n", c); // 7. Decryption //printf("\nc^d = %lld\n", (long int)pow(c,d)); m = (long long int) pow(c, d) % n; printf("\nPlain: %lld\n", m); getch(); return 0; } long long int gcd(long long int a, long long int b) { long long int q, r, tmp; /* use if required*/ if (b>a) { tmp = a; a = b; b = tmp; } while (b!=0) { q = a/b; r = a%b; a = b; b = r; } return a; } /* long int mypow(long int a, long int b) { long int p=1, i; for(i=1;i<b;i++) { p *= a; } return p; } */ //millerrabin #include <stdio.h> #include <string.h> #include <stdlib.h> /* * calculates (a * b) % c taking into account that a * b might overflow */ long long mulmod(long long a, long long b, long long mod) { long long x = 0,y = a % mod; while (b > 0) { if (b % 2 == 1) { x = (x + y) % mod; } y = (y * 2) % mod; b /= 2; } return x % mod; } /* * modular exponentiation */ long long modulo(long long base, long long exponent, long long mod) { long long x = 1; long long y = base; while (exponent > 0) { if (exponent % 2 == 1) x = (x * y) % mod; y = (y * y) % mod; exponent = exponent / 2; } return x % mod; } /* * Miller-Rabin Primality test, iteration signifies the accuracy */ int Miller(long long p,int iteration) { int i; long long s; if (p < 2) { return 0; } if (p != 2 && p % 2==0) { return 0; } s = p - 1; while (s % 2 == 0) { s /= 2; } for (i = 0; i < iteration; i++) { long long a = rand() % (p - 1) + 1, temp = s; long long mod = modulo(a, temp, p); while (temp != p - 1 && mod != 1 && mod != p - 1) { mod = mulmod(mod, mod, p); temp *= 2; } if (mod != p - 1 && temp % 2 == 0) { return 0; } } return 1; } //Main int main() { int iteration = 5; long long num; printf("Enter integer to test primality: "); scanf("%lld", &num); if ( Miller( num, iteration)) printf("\n%lld is prime\n", num); else printf("\n%lld is not prime\n", num); return 0; } //elgamal #include <stdio.h> #include <stdlib.h> #include <math.h> int is_prime(int num) { int i; for (i = 2; i <= sqrt(num); i++) { if (num % i == 0) { return 0; } } return 1; } int generate_prime(int min, int max) { int num = min + rand() % (max - min); while (!is_prime(num)) { num++; } return num; } int power(int a, int b, int p) { int result = 1; while (b > 0) { if (b % 2 == 1) { result = (result * a) % p; } a = (a * a) % p; b /= 2; } return result; } int main() { int p, g, x, y, k, m, r, e, d; printf("Enter a prime number p: "); scanf("%d", &p); while (!is_prime(p)) { printf("p is not prime. Enter a prime number p: "); scanf("%d", &p); } printf("Enter a generator g: "); scanf("%d", &g); // while (g <= 1 || g >= p - 1 || power(g, (p - 1) / 2, p) != 1) { // printf("g is not a generator. Enter a generator g: "); // scanf("%d", &g); // } printf("Enter a private key x: "); scanf("%d", &x); printf("Enter a message m: "); scanf("%d", &m); y = power(g,x,p); k = generate_prime(2, p - 2); r = power(g, k, p); e = (m * power(y, k, p)) % p; d = (e * power(r, p - 1 - x, p)) % p; printf("Public key (p, g, y) = (%d, %d, %d)\n", p, g, y); printf("Private key x = %d\n", x); printf("Message m = %d\n", m); printf("Random integer k = %d\n", k); printf("Encrypted message (r, e) = (%d, %d)\n", r, e); printf("Decrypted message d = %d\n", d); return 0; } //hill cipher #include<stdio.h> #include<string.h> int main() { unsigned int a[3][3] = { { 6, 24, 1 }, { 13, 16, 10 }, { 20, 17, 15 } }; unsigned int b[3][3] = { { 8, 5, 10 }, { 21, 8, 21 }, { 21, 12, 8 } }; int i, j; unsigned int c[20], d[20]; char msg[20]; int determinant = 0, t = 0; ; printf("Enter plain text\n "); scanf("%s", msg); for (i = 0; i < 3; i++) { c[i] = msg[i] - 65; printf("%d ", c[i]); } for (i = 0; i < 3; i++) { t = 0; for (j = 0; j < 3; j++) { t = t + (a[i][j] * c[j]); } d[i] = t % 26; } printf("\nEncrypted Cipher Text :"); for (i = 0; i < 3; i++) printf(" %c", d[i] + 65); for (i = 0; i < 3; i++) { t = 0; for (j = 0; j < 3; j++) { t = t + (b[i][j] * d[j]); } c[i] = t % 26; } printf("\nDecrypted Cipher Text :"); for (i = 0; i < 3; i++) printf(" %c", c[i] + 65); return 0; } //vignere cipher #include<stdio.h> #include<string.h> int main(){ char msg[] = "THECRAZYPROGRAMMER"; char key[] = "HELLO"; int msgLen = strlen(msg), keyLen = strlen(key), i, j; char newKey[msgLen], encryptedMsg[msgLen], decryptedMsg[msgLen]; //generating new key for(i = 0, j = 0; i < msgLen; ++i, ++j){ if(j == keyLen) j = 0; newKey[i] = key[j]; } newKey[i] = '\0'; //encryption for(i = 0; i < msgLen; ++i) encryptedMsg[i] = ((msg[i] + newKey[i]) % 26) + 'A'; encryptedMsg[i] = '\0'; //decryption for(i = 0; i < msgLen; ++i) decryptedMsg[i] = (((encryptedMsg[i] - newKey[i]) + 26) % 26) + 'A'; decryptedMsg[i] = '\0'; printf("Original Message: %s", msg); printf("\nKey: %s", key); printf("\nNew Generated Key: %s", newKey); printf("\nEncrypted Message: %s", encryptedMsg); printf("\nDecrypted Message: %s", decryptedMsg); return 0; } //caesar cipher #include<stdio.h> int main() { char message[100], ch; int i, key; printf("Enter a message to encrypt: "); gets(message); printf("Enter key: "); scanf("%d", &key); for(i = 0; message[i] != '\0'; ++i){ ch = message[i]; if(ch >= 'a' && ch <= 'z'){ ch = ch + key; if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch + key; if(ch > 'Z'){ ch = ch - 'Z' + 'A' - 1; } message[i] = ch; } } printf("Encrypted message: %s", message); return 0; } //decrypt #include<stdio.h> int main() { char message[100], ch; int i, key; printf("Enter a message to decrypt: "); gets(message); printf("Enter key: "); scanf("%d", &key); for(i = 0; message[i] != '\0'; ++i){ ch = message[i]; if(ch >= 'a' && ch <= 'z'){ ch = ch - key; if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch - key; if(ch < 'A'){ ch = ch + 'Z' - 'A' + 1; } message[i] = ch; } } printf("Decrypted message: %s", message); return 0; } //playfair cipher // C program to implement Playfair Cipher #include <stdio.h> #include <stdlib.h> #include <string.h> #define SIZE 30 // Function to convert the string to lowercase void toLowerCase(char plain[], int ps) { int i; for (i = 0; i < ps; i++) { if (plain[i] > 64 && plain[i] < 91) plain[i] += 32; } } // Function to remove all spaces in a string int removeSpaces(char* plain, int ps) { int i, count = 0; for (i = 0; i < ps; i++) if (plain[i] != ' ') plain[count++] = plain[i]; plain[count] = '\0'; return count; } // Function to generate the 5x5 key square void generateKeyTable(char key[], int ks, char keyT[5][5]) { int i, j, k, flag = 0, *dicty; // a 26 character hashmap // to store count of the alphabet dicty = (int*)calloc(26, sizeof(int)); for (i = 0; i < ks; i++) { if (key[i] != 'j') dicty[key[i] - 97] = 2; } dicty['j' - 97] = 1; i = 0; j = 0; for (k = 0; k < ks; k++) { if (dicty[key[k] - 97] == 2) { dicty[key[k] - 97] -= 1; keyT[i][j] = key[k]; j++; if (j == 5) { i++; j = 0; } } } for (k = 0; k < 26; k++) { if (dicty[k] == 0) { keyT[i][j] = (char)(k + 97); j++; if (j == 5) { i++; j = 0; } } } } // Function to search for the characters of a digraph // in the key square and return their position void search(char keyT[5][5], char a, char b, int arr[]) { int i, j; if (a == 'j') a = 'i'; else if (b == 'j') b = 'i'; for (i = 0; i < 5; i++) { for (j = 0; j < 5; j++) { if (keyT[i][j] == a) { arr[0] = i; arr[1] = j; } else if (keyT[i][j] == b) { arr[2] = i; arr[3] = j; } } } } // Function to find the modulus with 5 int mod5(int a) { return (a % 5); } // Function to make the plain text length to be even int prepare(char str[], int ptrs) { if (ptrs % 2 != 0) { str[ptrs++] = 'z'; str[ptrs] = '\0'; } return ptrs; } // Function for performing the encryption void encrypt(char str[], char keyT[5][5], int ps) { int i, a[4]; for (i = 0; i < ps; i += 2) { search(keyT, str[i], str[i + 1], a); if (a[0] == a[2]) { str[i] = keyT[a[0]][mod5(a[1] + 1)]; str[i + 1] = keyT[a[0]][mod5(a[3] + 1)]; } else if (a[1] == a[3]) { str[i] = keyT[mod5(a[0] + 1)][a[1]]; str[i + 1] = keyT[mod5(a[2] + 1)][a[1]]; } else { str[i] = keyT[a[0]][a[3]]; str[i + 1] = keyT[a[2]][a[1]]; } } } // Function to encrypt using Playfair Cipher void encryptByPlayfairCipher(char str[], char key[]) { char ps, ks, keyT[5][5]; // Key ks = strlen(key); ks = removeSpaces(key, ks); toLowerCase(key, ks); // Plaintext ps = strlen(str); toLowerCase(str, ps); ps = removeSpaces(str, ps); ps = prepare(str, ps); generateKeyTable(key, ks, keyT); encrypt(str, keyT, ps); } // Driver code int main() { char str[SIZE], key[SIZE]; // Key to be encrypted strcpy(key, "Monarchy"); printf("Key text: %s\n", key); // Plaintext to be encrypted strcpy(str, "instruments"); printf("Plain text: %s\n", str); // encrypt using Playfair Cipher encryptByPlayfairCipher(str, key); printf("Cipher text: %s\n", str); return 0; } //rail fence cipher #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) if(railMatrix[i][j] != '\n') printf("%c", railMatrix[i][j]); } void decryptMsg(char enMsg[], int key){ int msgLen = strlen(enMsg), i, j, k = -1, row = 0, col = 0, m = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = '*'; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) if(railMatrix[i][j] == '*') railMatrix[i][j] = enMsg[m++]; row = col = 0; k = -1; printf("\nDecrypted Message: "); for(i = 0; i < msgLen; ++i){ printf("%c", railMatrix[row][col++]); if(row == 0 || row == key-1) k= k * (-1); row = row + k; } } int main(){ char msg[] = "Hello World"; char enMsg[] = "Horel ollWd"; int key = 3; printf("Original Message: %s", msg); encryptMsg(msg, key); decryptMsg(enMsg, key); return 0; }