document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4. void Decryption(char *P, char *Temp, char * Key, int Count);
  5. void Encryption(char *P, char *Temp, char * key, int Count);
  6. void Change_key(char * p);
  7. void Vig_Cipher(); // 비제네르 암호 표 출력.
  8. int main()
  9. {
  10.     int select, count;
  11.     printf("=====================Vigenere Cipher ==========================\\n");
  12.     Vig_Cipher();
  13.     printf("===============================================================\\n");
  14.     while (1)
  15.     {
  16.         char * p = (char *)malloc(sizeof(char)* 20);
  17.         char * key = (char *)malloc(sizeof(char)* 20);
  18.         printf("========== Enter Plain Text or Cryptogram : ");
  19.         scanf("%s", p);
  20.         printf("========== Enter Key Value : ");
  21.         scanf("%s", key);
  22.         Change_key(key); // key값이 대문자일 경우, 소문자로 변환시켜준다...
  23.  
  24.         count = strlen(p); // 평문 or 암호문의 문자길이 입력 (문자길이 만큼 동적메모리 할당하기 위해서)
  25.        
  26.         char *Tmp = (char *)malloc(sizeof(char)* (count + 1)); // count+1 하는이유는 문자열 출력 할 때 NULL바이트까지 넣기위해서..
  27.  
  28.         Tmp[count] = \'\\0\'; // 임시저장할 문자에 NULL바이트 삽입
  29.  
  30.         printf("========== Enter Encryption[1] Decryption[2] End[3]  :  \\b");
  31.         scanf("%d", &select);
  32.         if (select == 1)
  33.             Encryption(p, Tmp, key, count);
  34.         else if (select == 2)
  35.             Decryption(p, Tmp, key, count);
  36.         else if (select == 3)
  37.             break;
  38.         else
  39.         {
  40.             printf("\\n Error!!\\n");
  41.             exit(-1);
  42.         }
  43.  
  44.         free(p);
  45.         free(Tmp);
  46.         free(key);
  47.     }
  48.     return 0;
  49. }
  50.  
  51. void Decryption(char * P, char * Temp, char * Key, int Count)
  52. {
  53.     int i, j = 0;
  54.     int key_len = strlen(Key); // key값의 문자열 길이
  55.     printf("\\n=== Decryption ===\\n");
  56.     for (i = 0; i < Count; i++)
  57.     {
  58.        
  59.         if (P[i] >= 65 && P[i] <= 90)
  60.         {
  61.             Temp[i] = 90 - (90 - P[i] + (Key[j++] - 97)) % 26;
  62.             printf("%c -> %c\\n", P[i], Temp[i]);
  63.         }
  64.         else if (P[i] >= 97 && P[i] <= 122)
  65.         {
  66.             Temp[i] = 122 - (122 - P[i] + (Key[j++] - 97)) % 26;
  67.             printf("%c -> %c\\n", P[i], Temp[i]);
  68.         }
  69.         else
  70.         {
  71.             printf("Decryption Error!!\\n");
  72.             exit(-1);
  73.         }
  74.         if (j == key_len)
  75.             j = 0;
  76.     }
  77.     printf("<<  Cryptogram : %s  >>\\n", P);
  78.     printf("<<  Plain Text : %s  >>\\n\\n", Temp);
  79. }
  80. void Encryption(char * P, char *Temp, char * Key, int Count)
  81. {
  82.     int j = 0;
  83.     int key_len = strlen(Key); // key값의 문자열 길이
  84.     printf("\\n=== Encryption ===\\n");
  85.     int i; // 반복문, 문자갯수count변수 선언
  86.     for (i = 0; i < Count; i++)
  87.     {
  88.         if (P[i] >= 65 && P[i] <= 90)
  89.         {// 평문이 값이 대문자일경우!
  90.             Temp[i] = (P[i] + (Key[j++]-97) - 65) % 26 + 65; // 암호화 공식 알파벳 갯수 26 ...               
  91.             printf("%c -> %c\\n", P[i], Temp[i]);
  92.         }
  93.         else if (P[i] >= 97 && P[i] <= 122) //평문값이 소문자일 경우!
  94.         {
  95.             Temp[i] = (P[i] + (Key[j++]-97) - 97) % 26 + 97;
  96.             printf("%c -> %c\\n", P[i], Temp[i]);
  97.         }
  98.         else
  99.         {
  100.             printf("Plain Text Error!\\n");
  101.             exit(-1);
  102.         }
  103.         if (j == key_len)
  104.             j = 0;
  105.     }
  106.     printf("<<  Plain Text : %s  >>\\n", P);
  107.     printf("<<  Cryptogram : %s  >>\\n\\n", Temp);
  108. }
  109. void Change_key(char * p) // 키값이 대문자인경우 ->  소문자로 바꿔줌
  110. {
  111.     int i;
  112.     int count = strlen(p); // count =>7 이묜 0~ 6
  113.     for (i = 0; i < count; i++)
  114.     {
  115.         if (p[i] >= 65 && p[i] <= 90) // 키값이 소문자일때,
  116.             p[i] = p[i] - 65 + 97;
  117.         else if (!(p[i] >= 97 && p[i] <= 122)) // 키값이 대문자일때,
  118.         {
  119.             printf("키값 오류!!!\\n");
  120.             exit(-1);
  121.         }
  122.     }
  123. }
  124. void Vig_Cipher(){
  125.     int i, j;
  126.     printf("    ");
  127.     for (i = 0; i < 26; i++)
  128.     {
  129.         printf("%2c", 65 + i);
  130.     }
  131.     printf("\\n    ____________________________________________________\\n");
  132.     for (i = 0; i < 26; i++)
  133.     {
  134.         printf("%c | ", 65 + i);
  135.         for (j = 0; j < 26; j++)
  136.         {
  137.             printf("%2c", (i + j) % 26 + 65); // 비제네르 암호 표 소문자로 표시
  138.         }
  139.         printf("\\n");
  140.     }
  141. }
');