fork download
  1. /*
  2.   **** Tabuk University ****
  3.  * College Of Computer And Information Technology
  4.  * Prepaired By:
  5.  * Fahd Dayim Alanzi
  6.  * Omar Ibrahim Almayhubi
  7.  * Ibrahim Salah Alanzi
  8.  * Nasser Awad Alqadi
  9.  * Khalid Abdul Karim Alanzi
  10.  * Ali Abdullah alBelawi
  11.  **** Under supervision****
  12. * * Dr. Ibrahim Alssadi * *
  13.  
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <ctype.h>
  19.  
  20. #define WIDTH 26
  21.  
  22. int gcd(int a, int b);
  23. char shift(int *key, char ch);
  24. char unshift(int *key, char ch);
  25. int modInverse(int a, int m);
  26.  
  27.  
  28. char * encrypt(int* key, char* src, char* dest)
  29. {
  30. char *pSrc = src;
  31. char *pDest = dest;
  32.  
  33. if (gcd(key[0], WIDTH) != 1)
  34. {
  35. printf("Error: a 与 m 不互素");
  36. exit(-1);
  37. }
  38.  
  39. for (int i = 0; *pSrc; ++i, ++pSrc, ++pDest)
  40. {
  41. if (!isalpha(*pSrc))
  42. continue;
  43.  
  44. *pDest = shift(key, toupper(*pSrc));
  45. }
  46.  
  47. return dest;
  48. }
  49.  
  50. /*
  51.  * 解密
  52.  *
  53.  * @param key 密钥 { a, b }
  54.  * @param src 待解密的字符串
  55.  * @param dest 经过解密后的字符串
  56.  */
  57. char * decrypt(int* key, char* src, char* dest)
  58. {
  59. char *pSrc = src;
  60. char *pDest = dest;
  61. int arr[2] = { modInverse(key[0], WIDTH), -key[1] };
  62.  
  63. for (int i = 0; *pSrc; ++i, ++pSrc, ++pDest)
  64. {
  65. if (!isalpha(*pSrc))
  66. continue;
  67.  
  68. *pDest = unshift(arr, toupper(*pSrc));
  69. }
  70.  
  71. return dest;
  72. }
  73.  
  74. int main(int argc, char const *argv[])
  75. {
  76. // 本例推算见 http://e...content-available-to-author-only...a.org/wiki/Affine_cipher
  77.  
  78. int key[] = { 5, 8 }; // 密匙
  79. char text[] = "AFFINECIPHER"; // 明文
  80. char ciphertext[1024], result[1024];
  81.  
  82. // 加密
  83. encrypt(key, text, ciphertext);
  84. printf("%s\n", ciphertext);
  85.  
  86. // 解密
  87. decrypt(key, ciphertext, result);
  88. printf("%s\n", result);
  89.  
  90.  
  91. printf (" \n**** Tabuk University **** \n \n * College Of Computer And Information Technology * \n" "\n * Prepaired By: \n" "\n * Fahd Dayim Alanzi \n" "\n * Omar Ibrahim Almayhubi \n" "\n * Ibrahim Salah Alanzi \n" "\n * Nasser Awad Alqadi \n" "\n * Khalid Abdul Karim Alanzi \n" "\n * Ali Abdullah alBelawi \n" "\n ******* Under supervision *******\n" "\n **** Dr. Ibrahim Alssadi ****\n");
  92.  
  93. return 0;
  94.  
  95.  
  96.  
  97. }
  98.  
  99. /*
  100.  * E(x) = (ax + b) mod m
  101.  */
  102. char shift(int *key, char ch)
  103. {
  104. int offset = ch - 'A';
  105. return (key[0] * offset + key[1]) % WIDTH + 'A';
  106. }
  107.  
  108. /*
  109.  * D(x) = a^{-1}(x - b) mod m
  110.  */
  111. char unshift(int *key, char ch)
  112. {
  113. int offset = ch - 'A';
  114. return (((key[0] * (offset + key[1])) % WIDTH + WIDTH) % WIDTH) + 'A';
  115. }
  116.  
  117. /*
  118.  * 判断 a 与 b 是否互素
  119.  */
  120. int gcd(int a, int b)
  121. {
  122. int tmp;
  123. while (a != 0)
  124. {
  125. tmp = a;
  126. a = b % a;
  127. b = tmp;
  128. }
  129. return b;
  130. }
  131.  
  132. /*
  133.  * 求 a 在密表中的乘法逆
  134.  */
  135. int modInverse( int a, int m)
  136. {
  137. int x1, x2, x3, y1, y2, y3, t1, t2, t3, q;
  138. x1 = y2 = 1, x2 = y1 = 0;
  139. x3 = ( a >= m ) ? a : m;
  140. y3 = ( a >= m ) ? m : a;
  141.  
  142. while ( 1 )
  143. {
  144. if ( y3 == 0 )
  145. {
  146. return x3;
  147. }
  148. else if ( y3 == 1 )
  149. {
  150. return y2;
  151. }
  152. q = x3 / y3;
  153. t1 = x1 - q * y1, t2 = x2 - q * y2, t3 = x3 - q * y3;
  154. x1 = y1, x2 = y2, x3 = y3;
  155. y1 = t1, y2 = t2, y3 = t3;
  156. }
  157. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
IHHWVCSWFRCP
AFFINECIPHER
 
**** Tabuk University  **** 
 
 * College Of Computer And Information Technology * 

 * Prepaired By: 

 * Fahd Dayim Alanzi 

 * Omar Ibrahim Almayhubi 

 * Ibrahim Salah Alanzi 

 * Nasser Awad  Alqadi 

 * Khalid Abdul Karim Alanzi 

 * Ali Abdullah alBelawi 

 ******* Under supervision *******

 **** Dr. Ibrahim Alssadi ****