Advertisement
jbn6972

cnslab

Feb 13th, 2023
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.08 KB | None | 0 0
  1. SUBSTITUTION CIPHER
  2.  
  3. #include <stdio.h>
  4. int main()
  5. {
  6.     int key, choice;
  7.     char message[100];
  8.     char ch;
  9.     int i;
  10.  
  11.     printf("Enter the Message :: ");
  12.     gets(message);
  13.     printf("Enter the value of key :: ");
  14.     scanf("%d", &key);
  15.  
  16.     printf("1. To Encrypt \n2. To Decrypt\nEnter choice :: ");
  17.     scanf("%d", &choice);
  18.  
  19.     switch (choice)
  20.     {
  21.     case 1:
  22.         for (i = 0; message[i] != '\0'; ++i)
  23.         {
  24.             ch = message[i];
  25.  
  26.             if (ch >= 'a' && ch <= 'z')
  27.             {
  28.                 ch = ch + key;
  29.                 if (ch > 'z')
  30.                 {
  31.                     ch = ch - 'z' + 'a' - 1;
  32.                 }
  33.             }
  34.             if (ch >= 'A' && ch <= 'Z')
  35.             {
  36.                 ch = ch + key;
  37.                 if (ch > 'Z')
  38.                 {
  39.                     ch = ch - 'Z' + 'A' - 1;
  40.                 }
  41.             }
  42.             message[i] = ch;
  43.         }
  44.  
  45.         printf("Encrypted Message :: %s", message);
  46.  
  47.         break;
  48.     case 2:
  49.  
  50.         for (i = 0; message[i] != '\0'; i++)
  51.         {
  52.             ch = message[i];
  53.  
  54.             if (ch >= 'a' && ch <= 'z')
  55.             {
  56.                 ch = ch - key;
  57.                 if (ch < 'a')
  58.                 {
  59.                     ch = ch + 'z' - 'a' + 1;
  60.                 }
  61.             }
  62.             if (ch >= 'A' && ch <= 'Z')
  63.             {
  64.                 ch = ch - key;
  65.                 if (ch < 'A')
  66.                 {
  67.                     ch = ch + 'Z' - 'A' + 1;
  68.                 }
  69.             }
  70.             message[i] = ch;
  71.         }
  72.         printf("Encrypted Message :: %s", message);
  73.  
  74.         break;
  75.     }
  76.  
  77.     return 0;
  78. }
  79.  
  80. =============================================================================================================
  81. RSA
  82. #include <stdlib.h>
  83. #include <stdio.h>
  84.  
  85. int gcd(int a, int b)
  86. {
  87.     int c;
  88.     while (a != b)
  89.     {
  90.         if (a < b)
  91.         {
  92.             c = a;
  93.             a = b;
  94.             b = c;
  95.         }
  96.         a -= b;
  97.     }
  98.     return a;
  99. }
  100.  
  101. int mod(int m, int e, int n)
  102. {
  103.     int a = 1;
  104.     while (e)
  105.     {
  106.         a = (a * m) % n;
  107.         e--;
  108.     }
  109.     return a;
  110. }
  111.  
  112. int main()
  113. {
  114.     int p, q, z, n, m, c, d, x, e;
  115.     int en[100], de[100], j = 0;
  116.     printf("\nEnter the value of P & Q\n");
  117.     scanf("%d %d", &p, &q);
  118.     n = p * q;
  119.     z = (p - 1) * (q - 1);
  120.     for (e = 1; e < n; e++)
  121.     {
  122.         if (gcd(e, z) == 1)
  123.         {
  124.             en[j] = e;
  125.             printf("%d ", en[j++]);
  126.         }
  127.     }
  128.     printf("\nChoose e \n");
  129.     scanf("%d", &e);
  130.  
  131.     if (gcd(e, z) != 1)
  132.     {
  133.         printf("\nThe value not from list\n");
  134.         exit(0);
  135.     }
  136.  
  137.     printf("Enter the message(integer value) to be encrypted \n");
  138.     scanf("%d", &m);
  139.     printf("Before Encryption : %d\n", m);
  140.     c = mod(m, e, n);
  141.     printf("After encryption : %d\n", c);
  142.  
  143.     printf("The possible Decryption keys are : ");
  144.     for (d = 0; d < n; d++)
  145.     {
  146.         if ((d * e) % z == 1)
  147.         {
  148.             de[j] = d;
  149.             printf("%d ", de[j++]);
  150.         }
  151.     }
  152.     printf("Choose d \n");
  153.     scanf("%d", &d);
  154.     x = mod(c, d, n);
  155.     printf("After DEcrypiton : %d", x);
  156.     return 0;
  157. }
  158. ==================================================================================================================
  159. PASSWORD STRENGTH
  160. #include<stdio.h>
  161. #include<stdlib.h>
  162.  
  163. int main(){
  164.     char pwd[100];
  165.     int n,i,d,a,s;
  166.     s =0;
  167.     d = 0;
  168.     a= 0;
  169.     printf("Enter your password : ");
  170.     gets(pwd);
  171.  
  172.     n = strlen(pwd);
  173.  
  174.     for(i = 0;i<n;i++){
  175.         if(isalpha(pwd[i])){
  176.             a++;
  177.         }
  178.         else if(isdigit(pwd[i])){
  179.             d++;
  180.         }else{
  181.             s++;
  182.         }
  183.     }
  184.  
  185.     if(a >=1 && d >= 1 && s >=1 ){
  186.         printf("Strong Password\n");
  187.     }else if((a>=1 && s >= 1) || (a>=1&& d>=1) || (s>=1 && d>=1)){
  188.         printf("Moderate PAssword \n");
  189.  
  190.     }else{
  191.         printf("Weak Password\n");
  192.     }
  193.     return 0;
  194. }
  195. =================================================================================================================
  196. RAILFENCE
  197. #include <stdlib.h>
  198. #include <stdio.h>
  199.  
  200. void encrypt(char msg[], int key)
  201. {
  202.     int msglen = strlen(msg), i, j, k = -1, row = 0, col = 0;
  203.     char railmatrix[key][msglen];
  204.  
  205.     for (i = 0; i < key; i++)
  206.     {
  207.         for (j = 0; j < msglen; j++)
  208.         {
  209.             railmatrix[i][j] = '*';
  210.         }
  211.     }
  212.  
  213.     for (i = 0; i < msglen; i++)
  214.     {
  215.         railmatrix[row][col++] = msg[i];
  216.  
  217.         if (row == 0 || row == key - 1)
  218.         {
  219.             k = k * (-1);
  220.         }
  221.  
  222.         row = row + k;
  223.     }
  224.  
  225.     printf("Encrypted Message :: ");
  226.     char encrypt[100];
  227.     for (i = 0; i < key; i++)
  228.     {
  229.         for (j = 0; j < msglen; j++)
  230.         {
  231.             if (railmatrix[i][j] != '*')
  232.             {
  233.                 printf("%c", railmatrix[i][j]);
  234.                 strncat(encrypt, &railmatrix[i][j], 1);
  235.             }
  236.         }
  237.     }
  238.  
  239.     printf("Decryption \n");
  240.     printf("Input :: %s\n", encrypt);
  241.     printf("Key :: %d\n", key);
  242.     decrypt(encrypt,key);
  243. }
  244.  
  245. void decrypt(char encrypt[], int key)
  246. {
  247.     int msglen = strlen(encrypt), i, j, k = -1, row = 0, col = 0, m = 0;
  248.     char railmatrix[key][msglen];
  249.  
  250.     for (i = 0; i < key; i++)
  251.     {
  252.         for (j = 0; j < msglen; j++)
  253.         {
  254.             railmatrix[i][j] = '*';
  255.         }
  256.     }
  257.     for (i = 0; i < msglen; i++)
  258.     {
  259.         railmatrix[row][col++] = '#';
  260.  
  261.         if (row == 0 || row == key - 1)
  262.         {
  263.             k = k * (-1);
  264.         }
  265.  
  266.         row = row + k;
  267.     }
  268.  
  269.     for (i = 0; i < key; i++)
  270.     {
  271.         for (j = 0; j < msglen; j++)
  272.         {
  273.             if (railmatrix[i][j] == '#')
  274.             {
  275.                 railmatrix[i][j] = encrypt[m++];
  276.             }
  277.         }
  278.     }
  279.  
  280.     row = col = 0;
  281.     k = -1;
  282.     printf("Decrypted :: ");
  283.    for (i = 0; i < msglen; i++)
  284.     {
  285.        printf("%c ", railmatrix[row][col++]);
  286.  
  287.         if (row == 0 || row == key - 1)
  288.         {
  289.             k = k * (-1);
  290.         }
  291.  
  292.         row = row + k;
  293.     }
  294. }
  295.  
  296. int main()
  297. {
  298.     char message[100];
  299.     int key;
  300.  
  301.     printf("Enter the Message :: ");
  302.     gets(message);
  303.     printf("Enter the value of Key :: ");
  304.     scanf("%d", &key);
  305.     encrypt(message, key);
  306.     return 0;
  307. }
  308. =====================================================================================================================
  309. DEFFIEHELLMAN
  310. #include<stdio.h>
  311. #include<math.h>
  312.  
  313. int power(int a,int b,int p){
  314.     if(b==1){
  315.         return a;
  316.  
  317.     }else{
  318.         return ((int)pow(a,b)%p);
  319.     }
  320. }
  321.  
  322. int main(){
  323.     int q,alpha,x,a,y,b;
  324.     printf("Enter the prime number and prime root :: ");
  325.     scanf("%d %d",&q,&alpha);
  326.  
  327.     printf("Enter private key of A : ");
  328.     scanf("%d",&x);
  329.     a = power(alpha,x,q);
  330.     printf("Enter the private key of B : ");
  331.     scanf("%d",&y);
  332.     b = power(alpha,y,q);
  333.  
  334.     printf("A computes key K : %d \n",power(b,x,q));
  335.     printf("B computes key K : %d \n",power(a,y,q));
  336.  
  337.     return 0;
  338. }
  339. ===================================================================================================================
  340. VIGENERE
  341. #include <stdio.h>
  342. #include <stdlib.h>
  343. #include <string.h>
  344.  
  345. int main()
  346. {
  347.     char message[100], key[100];
  348.     int numstr[100], numkey[100], numenc[100];
  349.  
  350.     int i, j;
  351.     printf("Enter the Plain Text : ");
  352.     gets(message);
  353.     printf("Enter the value of Key : ");
  354.     gets(key);
  355.  
  356.     for (i = 0; i < strlen(message); i++)
  357.     {
  358.         message[i] = toupper(message[i]);
  359.         numstr[i] = message[i] - 'A';
  360.     }
  361.     for (i = 0; i < strlen(key); i++)
  362.     {
  363.         key[i] = toupper(key[i]);
  364.     }
  365.  
  366.     for (i = 0; i < strlen(message);)
  367.     {
  368.         for (j = 0; j < strlen(key) && i <strlen(message);  j++)
  369.         {
  370.             numkey[i] = key[j] - 'A';
  371.             i++;
  372.         }
  373.     }
  374.  
  375.     for (i = 0; i < strlen(message); i++)
  376.     {
  377.         numenc[i] = numstr[i] + numkey[i];
  378.         if (numenc[i] > 25)
  379.         {
  380.             numenc[i] -= 26;
  381.         }
  382.     }
  383.     for(i = 0;i<strlen(message);i++){
  384.         printf("%c",numenc[i] + 'A');
  385.     }
  386.  
  387.     // puts(message);
  388.     // puts(key);
  389.     return 0;
  390. }
  391. ====================================================================================================================================
  392. DES
  393. #include <stdio.h>
  394. int main()
  395. {
  396.     int i, cnt = 0, p8[8] = {6, 7, 8, 9, 1, 2, 3, 4};
  397.     int p10[10] = {6, 7, 8, 9, 10, 1, 2, 3, 4, 5};
  398.  
  399.     char input[11], k1[10], k2[10], temp[11];
  400.     char LS1[5], LS2[5];
  401.     // k1, k2 are for storing interim keys
  402.     // p8 and p10 are for storing permutation key
  403.  
  404.     // Read 10 bits from user...
  405.     printf("Enter 10 bits input:");
  406.     scanf("%s", input);
  407.     input[10] = '\0';
  408.  
  409.     // Applying p10...
  410.     for (i = 0; i < 10; i++)
  411.     {
  412.         cnt = p10[i];
  413.         temp[i] = input[cnt - 1];
  414.     }
  415.     temp[i] = '\0';
  416.     printf("\nYour p10 key is    :");
  417.     for (i = 0; i < 10; i++)
  418.     {
  419.         printf("%d,", p10[i]);
  420.     }
  421.  
  422.     printf("\nBits after p10     :");
  423.     puts(temp);
  424.     // Performing LS-1 on first half of temp
  425.     for (i = 0; i < 5; i++)
  426.     {
  427.         if (i == 4)
  428.             temp[i] = temp[0];
  429.         else
  430.             temp[i] = temp[i + 1];
  431.     }
  432.     // Performing LS-1 on second half of temp
  433.     for (i = 5; i < 10; i++)
  434.     {
  435.         if (i == 9)
  436.             temp[i] = temp[5];
  437.         else
  438.             temp[i] = temp[i + 1];
  439.     }
  440.     printf("Output after LS-1  :");
  441.     puts(temp);
  442.  
  443.     printf("\nYour p8 key is     :");
  444.     for (i = 0; i < 8; i++)
  445.     {
  446.         printf("%d,", p8[i]);
  447.     }
  448.  
  449.     // Applying p8...
  450.     for (i = 0; i < 8; i++)
  451.     {
  452.         cnt = p8[i];
  453.         k1[i] = temp[cnt - 1];
  454.     }
  455.     printf("\nYour key k1 is     :");
  456.     puts(k1);
  457.     // This program can be extended to generate k2 as per DES algorithm.
  458. }
  459. ===================================================================================================================================================
  460. HILL CIPHER
  461. #include <stdio.h>
  462. #include <string.h>
  463.  
  464. // Following function generates the
  465. //  key matrix for the key string
  466. void getKeyMatrix(char key[6], int keyMatrix[][3])
  467. {
  468.     int k = 0;
  469.     for (int i = 0; i < 3; i++)
  470.     {
  471.         for (int j = 0; j < 3; j++)
  472.         {
  473.             keyMatrix[i][j] = (key[k]) % 65;
  474.             k++;
  475.         }
  476.     }
  477. }
  478.  
  479. // Following function encrypts the message
  480. void encrypt(int cipherMatrix[][1],
  481.              int keyMatrix[][3],
  482.              int messageVector[][1])
  483. {
  484.     int x, i, j;
  485.     for (i = 0; i < 3; i++)
  486.     {
  487.         for (j = 0; j < 1; j++)
  488.         {
  489.             cipherMatrix[i][j] = 0;
  490.  
  491.             for (x = 0; x < 3; x++)
  492.             {
  493.                 cipherMatrix[i][j] +=
  494.                     keyMatrix[i][x] * messageVector[x][j];
  495.             }
  496.  
  497.             cipherMatrix[i][j] = cipherMatrix[i][j] % 26;
  498.         }
  499.     }
  500. }
  501.  
  502. // Function to implement Hill Cipher
  503. void HillCipher(char message[3], char key[9])
  504. {
  505.     // Get key matrix from the key string
  506.     int keyMatrix[3][3];
  507.     getKeyMatrix(key, keyMatrix);
  508.  
  509.     int messageVector[3][1];
  510.  
  511.     // Generate vector for the message
  512.     for (int i = 0; i < 3; i++)
  513.         messageVector[i][0] = (message[i]) % 65;
  514.  
  515.     int cipherMatrix[3][1];
  516.  
  517.     // Following function generates
  518.     // the encrypted vector
  519.     encrypt(cipherMatrix, keyMatrix, messageVector);
  520.  
  521.     char CipherText[3];
  522.  
  523.     // Generate the encrypted text from
  524.     // the encrypted vector
  525.     for (int i = 0; i < 3; i++)
  526.         CipherText[i] = cipherMatrix[i][0] + 65;
  527.  
  528.     // Finally print the ciphertext
  529.     printf("%s", CipherText);
  530. }
  531.  
  532. // Driver function for above code
  533. int main()
  534. {
  535.     // Get the message to be encrypted
  536.     char message[3], key[9];
  537.     printf("Enter the plaintext :");
  538.     scanf("%s", message);
  539.  
  540.     // Get the key
  541.     printf("Enter the key :");
  542.     scanf("%s", key);
  543.  
  544.     HillCipher(message, key);
  545.  
  546.     return 0;
  547. }
  548. ============================================================================================================================================
  549.  
  550.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement