加密代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
#include <iostream> #include <string> #include <unistd.h> using namespace std; //检查输入的字符串是否为纯英文 int Check(string s) { int i = 0; while (s[i] != '\0') { if (!((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z'))) { cout << "Error!Please enter again!" << endl << endl; sleep(1); return 1; } i++; } return 0; } //以下为计算模乘法逆元 int e_gcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int ans = e_gcd(b, a%b, x, y); int temp = x; x = y; y = temp - a / b*y; return ans; } // 计算密钥 int cal(int a, int m) { int x, y; int gcd = e_gcd(a, m, x, y); if (1 % gcd != 0) return -1; x *= 1 / gcd; m = abs(m); int ans = x%m; return ans; } int main() { int flag = 0; int a, b, c, x, i, key; int y = 26; char ch; string st; do { fflush(stdin); cout << "-------------Affine cipher(Encryption)------------" << endl; cout << "Encryption Algorithm: c=ap+b mod 26" << endl; cout << "* gcd(a,26)=1" << endl; cout << "* two numbers are selected(a,b)between 0 and 25" << endl; cout << "* p and c represent letters" << endl; cout << "* p is a plaintext letter and c is a ciphertext letter" << endl << endl; cout << "Please enter the plaintext:" << endl; cout << "Plaintext:"; cin >> st; flag = Check(st); } while (flag); do { //判断a是否与26互素 cout << "a = "; cin >> x; a = x; y = 26; while (x != y) { if (x > y) x = x - y; else y = y - x; } if (a > 26 || a < 0 || y != 1) { cout << "Avalilable!Please enter again!" << endl; } } while (y != 1 || a > 26 || a < 0); do { cout << "b = "; cin >> b; if (b < 0 || b>26) { cout << "Avalilable!Please enter again!" << endl; } } while (b < 0 || b>26); cout << endl; //计算密钥 key = cal(a, 26); if (key < 0) cout << "Public Key = " << a << " Key = " << key + 26 << endl; else cout << "Public Key = " << a << " Key = " << key << endl; cout << "Ciphertext: "; for (i = 0; i < st.length(); i++) { if (st[i] >= 'a'&&st[i] <= 'z') { c = 'a' + ((st[i] - 'a')*a + b) % 26; } else { c = 'A' + ((st[i] - 'A')*a + b) % 26; } ch = c; cout << ch; } cout << endl << endl; return 0; } |
解密代码实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
#include <iostream> #include <string> #include <unistd.h> using namespace std; //检查输入的字符串是否为纯英文 int Check(string s) { int i = 0; while (s[i] != '\0') { if (!((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z'))) { cout << "Error!Please enter again!" << endl << endl; sleep(1); return 1; } i++; } return 0; } int main() { int flag = 0; int a, b, c, i; int y = 26; char ch; string st; do { fflush(stdin); cout << "-------------Affine cipher(Decryption)------------" << endl; cout << "Decryption Algorithm: p=a'(c-b) mod 26" << endl; cout << "* p and c represent letters" << endl; cout << "* p is a plaintext letter and c is a ciphertext letter" << endl << endl; cout << "Please enter the Ciphertext:" << endl; cout << "Ciphertext:"; cin >> st; flag = Check(st); } while (flag); do { cout << "b = "; cin >> b; if (b < 0 || b>26) { cout << "Avalilable!Please enter again!" << endl; } } while (b < 0 || b>26); cout << "Key = "; cin >> a; cout << "Plaintext: "; for (i = 0; i < st.length(); i++) { if (st[i] >= 'a'&&st[i] <= 'z') { c = 'a' + ((st[i] - 'a' - b + 26)*a) % 26; } else { c = 'A' + ((st[i] - 'A' - b + 26)*a) % 26; } ch = c; cout << ch; } cout << endl << endl; return 0; } |
(编译运行环境:g++ [Debian 7.3.0-19] 7.3.0)
说点什么