Cyrkcyrk

decipher

Jan 5th, 2021
822
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Cyrille KASYC - cyrille.kasyc@edu.ece.fr */
  2. /* Fanny MARCUCCINI - fanny.marcuccini@edu.ece.fr */
  3. /* 05/01/2020 - ECE Paris Lyon */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. void sort
  8. void decipher(char *str)
  9. {
  10.     char    abc_freq_ordered[27] = {'E','T','A','O','I','N','S','R','H','D','L','U','C','M','F','Y','W','G','P','B','V','K','X','Q','J','Z','\0'};
  11.     char    freq_str[26] = {0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0};
  12.     char    abc_translation[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
  13.     char    abc_real_order[27] = {'M','L','V','D','R','X','O','P','W','Z', 'H','Q','Y','K','J','S','F','U','I','B','N','E','C','A','T','G','\0'};
  14.     char    abc_real_translation[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
  15.     int     i, tmp;
  16.     char    bool, buf;
  17.    
  18.     i = -1;
  19.     while (str[++i])
  20.     {
  21.         if (str[i] >= 'a' && str[i] <= 'z' )
  22.             freq_str[(str[i] - 'a')]++;
  23.         if (str[i] >= 'A' && str[i] <= 'Z' )
  24.             freq_str[(str[i] - 'A')]++;
  25.     }
  26.     bool = 1;
  27.     while (bool)
  28.     {
  29.         bool = 0;
  30.         i = -1;
  31.         while (++i < 24)
  32.         {
  33.             if (freq_str[i] < freq_str[i + 1])
  34.             {
  35.                 tmp = freq_str[i];
  36.                 freq_str[i] = freq_str[i + 1];
  37.                 freq_str[i + 1] = tmp;
  38.                
  39.                 buf = abc_translation[i];
  40.                 abc_translation[i] = abc_translation[i + 1];
  41.                 abc_translation[i + 1] = buf;
  42.                
  43.                 bool = 1;
  44.             }
  45.         }
  46.     }
  47.     bool = 1;
  48.     while (bool)
  49.     {
  50.         bool = 0;
  51.         i = -1;
  52.         while (++i < 25)
  53.         {
  54.             if (abc_freq_ordered[i] > abc_freq_ordered[i + 1])
  55.             {
  56.                 buf = abc_freq_ordered[i];
  57.                 abc_freq_ordered[i] = abc_freq_ordered[i + 1];
  58.                 abc_freq_ordered[i + 1] = buf;
  59.  
  60.                 buf = abc_translation[i];
  61.                 abc_translation[i] = abc_translation[i + 1];
  62.                 abc_translation[i + 1] = buf;
  63.  
  64.                 bool = 1;
  65.             }
  66.         }
  67.     }
  68.     bool = 1;
  69.     while (bool)
  70.     {
  71.         bool = 0;
  72.         i = -1;
  73.         while (++i < 25)
  74.         {
  75.             if (abc_real_order[i] > abc_real_order[i + 1])
  76.             {
  77.                 buf = abc_real_order[i];
  78.                 abc_real_order[i] = abc_real_order[i + 1];
  79.                 abc_real_order[i + 1] = buf;
  80.  
  81.                 buf = abc_real_translation[i];
  82.                 abc_real_translation[i] = abc_real_translation[i + 1];
  83.                 abc_real_translation[i + 1] = buf;
  84.  
  85.                 bool = 1;
  86.             }
  87.         }
  88.     }
  89.     printf("Alphabet:\t%s\nNaive table:\t%s\nReal table:\t%s\n\n", abc_freq_ordered, abc_translation, abc_real_translation);
  90.     printf("\tNAIVE TRANSLATION:\n");
  91.     i = -1;
  92.     while (str[++i])
  93.     {
  94.         if (str[i] >= 'a' && str[i] <= 'z' )
  95.             printf("%c",  abc_translation[str[i] - 'a']);
  96.         else if (str[i] >= 'A' && str[i] <= 'Z' )
  97.             printf("%c", abc_translation[str[i] - 'A']);
  98.         else
  99.             printf("%c", str[i]);
  100.     }
  101.     printf("\n\n\tREAL TRANSLATION:\n");
  102.     i = -1;
  103.     while (str[++i])
  104.     {
  105.         if (str[i] >= 'a' && str[i] <= 'z' )
  106.             printf("%c",  abc_real_translation[str[i] - 'a']);
  107.         else if (str[i] >= 'A' && str[i] <= 'Z' )
  108.             printf("%c", abc_real_translation[str[i] - 'A']);
  109.         else
  110.             printf("%c", str[i]);
  111.     }
  112. }
  113.  
  114. int main(int ac, char **av)
  115. {
  116.     char *str = "LRVMNIR BPR SUMVBWVR JX BPR LMIWV YJERYRKBI JX QMBM WI BPR XJVNI MKD YMIBRUT JX IRHX WI BPR RIIRKVR JX YMBINLMTMIPW UTN QMUMBR DJ W IPMHH BUT BJ RHNVWDMBR BPR YJERYRKBI JX BPR QMBM MVVJUDWKO BJ YT WKBRUSURBMBWJK LMIRD JK XJUBT TRMUI JX IBNDT WB WI KJB MK RMIT BMIQ BJ RASHMWK RMVP YJERYRKB MKD WBI IWOKWXWVMKVR MKD IJYR YNIB URYMWK NKRASHMWKRD BJ OWER M VJYSHRBR RASHMKMBWJK JKR CJNHD PMER BJ LR FNMHWXWRD MKD WKISWURD BJ INVP MK RABRKB BPMB PR VJNHD URMVP BPR IBMBR JX RKHWOPBRKRD YWKD VMSMLHR JX URVJOKWGWKO IJNKDHRII IJNKD MKD IPMSRHRII IPMSR W DJ KJB DRRY YTIRHX BPR XWKMH MNBPJUWBT LNB YT RASRUWRKVR CWBP QMBM PMI HRXB KJ DJNLB BPMB BPR XJHHJCWKO WI BPR SUJSRU MSSHWVMBWJK MKD WKBRUSURBMWJK W JXXRU YT BPRJUWRI WK BPR PJSR BPMB BPR RIIRKVR JX JQWKMCMK QMUMBR CWHH URYMWK WKBMVBz";
  117.     if (ac == 2)
  118.         str = av[1];
  119.     printf("%s\n%lu\n---\n",str, strlen(str));
  120.     decipher(str);
  121.     return (1);
  122. }
RAW Paste Data