Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SUBSTITUTION CIPHER
- #include <stdio.h>
- int main()
- {
- int key, choice;
- char message[100];
- char ch;
- int i;
- printf("Enter the Message :: ");
- gets(message);
- printf("Enter the value of key :: ");
- scanf("%d", &key);
- printf("1. To Encrypt \n2. To Decrypt\nEnter choice :: ");
- scanf("%d", &choice);
- switch (choice)
- {
- case 1:
- 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;
- }
- }
- if (ch >= 'A' && ch <= 'Z')
- {
- ch = ch + key;
- if (ch > 'Z')
- {
- ch = ch - 'Z' + 'A' - 1;
- }
- }
- message[i] = ch;
- }
- printf("Encrypted Message :: %s", message);
- break;
- case 2:
- 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;
- }
- }
- if (ch >= 'A' && ch <= 'Z')
- {
- ch = ch - key;
- if (ch < 'A')
- {
- ch = ch + 'Z' - 'A' + 1;
- }
- }
- message[i] = ch;
- }
- printf("Encrypted Message :: %s", message);
- break;
- }
- return 0;
- }
- =============================================================================================================
- RSA
- #include <stdlib.h>
- #include <stdio.h>
- int gcd(int a, int b)
- {
- int c;
- while (a != b)
- {
- if (a < b)
- {
- c = a;
- a = b;
- b = c;
- }
- a -= b;
- }
- return a;
- }
- int mod(int m, int e, int n)
- {
- int a = 1;
- while (e)
- {
- a = (a * m) % n;
- e--;
- }
- return a;
- }
- int main()
- {
- int p, q, z, n, m, c, d, x, e;
- int en[100], de[100], j = 0;
- printf("\nEnter the value of P & Q\n");
- scanf("%d %d", &p, &q);
- n = p * q;
- z = (p - 1) * (q - 1);
- for (e = 1; e < n; e++)
- {
- if (gcd(e, z) == 1)
- {
- en[j] = e;
- printf("%d ", en[j++]);
- }
- }
- printf("\nChoose e \n");
- scanf("%d", &e);
- if (gcd(e, z) != 1)
- {
- printf("\nThe value not from list\n");
- exit(0);
- }
- printf("Enter the message(integer value) to be encrypted \n");
- scanf("%d", &m);
- printf("Before Encryption : %d\n", m);
- c = mod(m, e, n);
- printf("After encryption : %d\n", c);
- printf("The possible Decryption keys are : ");
- for (d = 0; d < n; d++)
- {
- if ((d * e) % z == 1)
- {
- de[j] = d;
- printf("%d ", de[j++]);
- }
- }
- printf("Choose d \n");
- scanf("%d", &d);
- x = mod(c, d, n);
- printf("After DEcrypiton : %d", x);
- return 0;
- }
- ==================================================================================================================
- PASSWORD STRENGTH
- #include<stdio.h>
- #include<stdlib.h>
- int main(){
- char pwd[100];
- int n,i,d,a,s;
- s =0;
- d = 0;
- a= 0;
- printf("Enter your password : ");
- gets(pwd);
- n = strlen(pwd);
- for(i = 0;i<n;i++){
- if(isalpha(pwd[i])){
- a++;
- }
- else if(isdigit(pwd[i])){
- d++;
- }else{
- s++;
- }
- }
- if(a >=1 && d >= 1 && s >=1 ){
- printf("Strong Password\n");
- }else if((a>=1 && s >= 1) || (a>=1&& d>=1) || (s>=1 && d>=1)){
- printf("Moderate PAssword \n");
- }else{
- printf("Weak Password\n");
- }
- return 0;
- }
- =================================================================================================================
- RAILFENCE
- #include <stdlib.h>
- #include <stdio.h>
- void encrypt(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] = '*';
- }
- }
- for (i = 0; i < msglen; i++)
- {
- railmatrix[row][col++] = msg[i];
- if (row == 0 || row == key - 1)
- {
- k = k * (-1);
- }
- row = row + k;
- }
- printf("Encrypted Message :: ");
- char encrypt[100];
- for (i = 0; i < key; i++)
- {
- for (j = 0; j < msglen; j++)
- {
- if (railmatrix[i][j] != '*')
- {
- printf("%c", railmatrix[i][j]);
- strncat(encrypt, &railmatrix[i][j], 1);
- }
- }
- }
- printf("Decryption \n");
- printf("Input :: %s\n", encrypt);
- printf("Key :: %d\n", key);
- decrypt(encrypt,key);
- }
- void decrypt(char encrypt[], int key)
- {
- int msglen = strlen(encrypt), 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] = '*';
- }
- }
- 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] = encrypt[m++];
- }
- }
- }
- row = col = 0;
- k = -1;
- printf("Decrypted :: ");
- 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 message[100];
- int key;
- printf("Enter the Message :: ");
- gets(message);
- printf("Enter the value of Key :: ");
- scanf("%d", &key);
- encrypt(message, key);
- return 0;
- }
- =====================================================================================================================
- DEFFIEHELLMAN
- #include<stdio.h>
- #include<math.h>
- int power(int a,int b,int p){
- if(b==1){
- return a;
- }else{
- return ((int)pow(a,b)%p);
- }
- }
- int main(){
- int q,alpha,x,a,y,b;
- printf("Enter the prime number and prime root :: ");
- scanf("%d %d",&q,&alpha);
- printf("Enter private key of A : ");
- scanf("%d",&x);
- a = power(alpha,x,q);
- printf("Enter the private key of B : ");
- scanf("%d",&y);
- b = power(alpha,y,q);
- printf("A computes key K : %d \n",power(b,x,q));
- printf("B computes key K : %d \n",power(a,y,q));
- return 0;
- }
- ===================================================================================================================
- VIGENERE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- char message[100], key[100];
- int numstr[100], numkey[100], numenc[100];
- int i, j;
- printf("Enter the Plain Text : ");
- gets(message);
- printf("Enter the value of Key : ");
- gets(key);
- for (i = 0; i < strlen(message); i++)
- {
- message[i] = toupper(message[i]);
- numstr[i] = message[i] - 'A';
- }
- for (i = 0; i < strlen(key); i++)
- {
- key[i] = toupper(key[i]);
- }
- for (i = 0; i < strlen(message);)
- {
- for (j = 0; j < strlen(key) && i <strlen(message); j++)
- {
- numkey[i] = key[j] - 'A';
- i++;
- }
- }
- for (i = 0; i < strlen(message); i++)
- {
- numenc[i] = numstr[i] + numkey[i];
- if (numenc[i] > 25)
- {
- numenc[i] -= 26;
- }
- }
- for(i = 0;i<strlen(message);i++){
- printf("%c",numenc[i] + 'A');
- }
- // puts(message);
- // puts(key);
- return 0;
- }
- ====================================================================================================================================
- DES
- #include <stdio.h>
- int main()
- {
- int i, cnt = 0, p8[8] = {6, 7, 8, 9, 1, 2, 3, 4};
- int p10[10] = {6, 7, 8, 9, 10, 1, 2, 3, 4, 5};
- char input[11], k1[10], k2[10], temp[11];
- char LS1[5], LS2[5];
- // k1, k2 are for storing interim keys
- // p8 and p10 are for storing permutation key
- // Read 10 bits from user...
- printf("Enter 10 bits input:");
- scanf("%s", input);
- input[10] = '\0';
- // Applying p10...
- for (i = 0; i < 10; i++)
- {
- cnt = p10[i];
- temp[i] = input[cnt - 1];
- }
- temp[i] = '\0';
- printf("\nYour p10 key is :");
- for (i = 0; i < 10; i++)
- {
- printf("%d,", p10[i]);
- }
- printf("\nBits after p10 :");
- puts(temp);
- // Performing LS-1 on first half of temp
- for (i = 0; i < 5; i++)
- {
- if (i == 4)
- temp[i] = temp[0];
- else
- temp[i] = temp[i + 1];
- }
- // Performing LS-1 on second half of temp
- for (i = 5; i < 10; i++)
- {
- if (i == 9)
- temp[i] = temp[5];
- else
- temp[i] = temp[i + 1];
- }
- printf("Output after LS-1 :");
- puts(temp);
- printf("\nYour p8 key is :");
- for (i = 0; i < 8; i++)
- {
- printf("%d,", p8[i]);
- }
- // Applying p8...
- for (i = 0; i < 8; i++)
- {
- cnt = p8[i];
- k1[i] = temp[cnt - 1];
- }
- printf("\nYour key k1 is :");
- puts(k1);
- // This program can be extended to generate k2 as per DES algorithm.
- }
- ===================================================================================================================================================
- HILL CIPHER
- #include <stdio.h>
- #include <string.h>
- // Following function generates the
- // key matrix for the key string
- void getKeyMatrix(char key[6], int keyMatrix[][3])
- {
- int k = 0;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- keyMatrix[i][j] = (key[k]) % 65;
- k++;
- }
- }
- }
- // Following function encrypts the message
- void encrypt(int cipherMatrix[][1],
- int keyMatrix[][3],
- int messageVector[][1])
- {
- int x, i, j;
- for (i = 0; i < 3; i++)
- {
- for (j = 0; j < 1; j++)
- {
- cipherMatrix[i][j] = 0;
- for (x = 0; x < 3; x++)
- {
- cipherMatrix[i][j] +=
- keyMatrix[i][x] * messageVector[x][j];
- }
- cipherMatrix[i][j] = cipherMatrix[i][j] % 26;
- }
- }
- }
- // Function to implement Hill Cipher
- void HillCipher(char message[3], char key[9])
- {
- // Get key matrix from the key string
- int keyMatrix[3][3];
- getKeyMatrix(key, keyMatrix);
- int messageVector[3][1];
- // Generate vector for the message
- for (int i = 0; i < 3; i++)
- messageVector[i][0] = (message[i]) % 65;
- int cipherMatrix[3][1];
- // Following function generates
- // the encrypted vector
- encrypt(cipherMatrix, keyMatrix, messageVector);
- char CipherText[3];
- // Generate the encrypted text from
- // the encrypted vector
- for (int i = 0; i < 3; i++)
- CipherText[i] = cipherMatrix[i][0] + 65;
- // Finally print the ciphertext
- printf("%s", CipherText);
- }
- // Driver function for above code
- int main()
- {
- // Get the message to be encrypted
- char message[3], key[9];
- printf("Enter the plaintext :");
- scanf("%s", message);
- // Get the key
- printf("Enter the key :");
- scanf("%s", key);
- HillCipher(message, key);
- return 0;
- }
- ============================================================================================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement