原理:Double Column Permutation Cipher 是列置换密码通过进行两次加解密运算得到的,列置换密码原理及实现请移步 列置换密码
加密代码实现:
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
#include <iostream> #include <string> #include <string.h> #include <unistd.h> using namespace std; struct group { char ch = '\0'; int num = -1; }; 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; return 1; } i++; } return 0; } //处理密钥,返回指针数组 int *wordKey(string key, int k[20]) { int i, j; // int k[20] = { 0 }; int *p = k; int flag = 0; char temp1; char tem[20] = { 0 }; char keygroup[20] = { 0 }; for (i = 0; i < key.length(); i++) {//设置临时数组 keygroup[i] = key[i]; } //冒泡排序 for (i = 0; i < key.length(); i++) { flag = 0; for (j = 0; j < key.length() - i - 1; j++) { if (keygroup[j] > keygroup[j + 1]) { temp1 = keygroup[j]; keygroup[j] = keygroup[j + 1]; keygroup[j + 1] = temp1; flag = 1; } } if (flag == 0) break; } //设置临时数组 for (i = 0; i < key.length(); i++) { tem[i] = keygroup[i]; } //对应的密钥顺序 cout << endl << "key = (d, f)" << endl; cout << "d = " << key.length() << " "; cout << "f = ("; for (i = 0; i < key.length(); i++) { for (j = 0; j < key.length(); j++) { if (keygroup[j] == key[i] && tem[j] != '#') { k[i] = j + 1; tem[j] = '#'; cout << " " << k[i]; break; } } } cout << " )" << endl << endl; return p; } int main(){ int flag = 0; int k[20] = { 0 }; int k2[20] = { 0 }; int t, i, j, m, n, q; string st, key, key2; char tem[50] = { 0 }; char tem2[50] = { 0 }; char text[50] = { 0 }; char cipher[10][10] = { 0 }; char cipher2[10][10] = { 0 }; int *p, *p2; do { fflush(stdin); cout << "--------Column Permutation cipher(Encryption)--------" << endl; cout << " the null letter is 'x' " << endl; cout << "Please enter the plaintext:"; cin >> st; flag = Check(st); } while (flag); do { //输入key fflush(stdin); cout << "Please enter the key1: "; cin >> key; flag = Check(key); } while (flag); //明文不是key的整数倍补'x' t = st.length(); if (t%key.length() != 0) t = ((t / (key.length()) + 1)*key.length()); for (i = 0; i < t; i++) { if (i < st.length()) tem[i] = st[i]; else tem[i] = 'x'; } p = wordKey(key, k); //第一次加密 cout << "After the first step" << endl; //打印plain matrix cout << "Plaintext Matrix" << endl << endl; m = 0; n = (strlen(tem)) / (key.length()); for (i = 0; i < n; i++) { for (j = 0; j < key.length(); j++) { cipher[i][j] = tem[m]; cout << " " << cipher[i][j]; m++; } cout << endl; } q = 0; cout << "Middle CipherText:"; for (i = 0; i < key.length(); i++) { for (j = 0; j < n; j++) { text[q] = cipher[j][(p[i] - 1)]; cout << cipher[j][(p[i] - 1)]; q++; } } cout << endl << endl; do { //输入key fflush(stdin); cout << "Please enter the key2: "; cin >> key2; flag = Check(key2); } while (flag); //明文不是key的整数倍补'x' t = strlen(text); if (t%key2.length() != 0) t = ((t / (key2.length()) + 1)*key2.length()); for (i = 0; i < t; i++) { if (i < strlen(text)) tem2[i] = text[i]; else tem2[i] = 'x'; } p2 = wordKey(key2, k2); //第二次加密 cout << "After the second step" << endl; //打印plain matrix cout << "Plaintext Matrix" << endl << endl; m = 0; n = (strlen(tem2)) / (key2.length()); for (i = 0; i < n; i++) { for (j = 0; j < key2.length(); j++) { cipher2[i][j] = tem2[m]; cout << " " << cipher2[i][j]; m++; } cout << endl; } cout << "CipherText:"; for (i = 0; i < key2.length(); i++) { for (j = 0; j < n; j++) { cout << cipher2[j][(p2[i] - 1)]; } } cout << endl << endl; } |
解密代码实现:
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
#include <iostream> #include <string> #include <string.h> #include <unistd.h> using namespace std; struct group { char ch = '\0'; int num = -1; }; 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; return 1; } i++; } return 0; } //处理密钥,返回指针数组 int *wordKey(string key, int k[20]) { int i, j; // int k[20] = { 0 }; int *p = k; int flag = 0; char temp1; char tem[20] = { 0 }; char keygroup[20] = { 0 }; for (i = 0; i < key.length(); i++) {//设置临时数组 keygroup[i] = key[i]; } //冒泡排序 for (i = 0; i < key.length(); i++) { flag = 0; for (j = 0; j < key.length() - i - 1; j++) { if (keygroup[j] > keygroup[j + 1]) { temp1 = keygroup[j]; keygroup[j] = keygroup[j + 1]; keygroup[j + 1] = temp1; flag = 1; } } if (flag == 0) break; } //设置临时数组 for (i = 0; i < key.length(); i++) { tem[i] = keygroup[i]; } //对应的密钥顺序 cout << endl << "key = (d, f)" << endl; cout << "d = " << key.length() << " "; cout << "f = ("; for (i = 0; i < key.length(); i++) { for (j = 0; j < key.length(); j++) { if (keygroup[j] == key[i] && tem[j] != '#') { k[i] = j + 1; tem[j] = '#'; cout << " " << k[i]; break; } } } cout << " )" << endl << endl; return p; } int main(){ int flag = 0; int k[20] = { 0 }; int k2[20] = { 0 }; int i, j, m, n, q, d; string st, key, key2; char tem[50] = { 0 }; char tem2[50] = { 0 }; char plain[10][10] = { 0 }; char plain2[10][10] = { 0 }; int *p, *p2; do { fflush(stdin); cout << "--------Column Permutation cipher(Decryption)--------" << endl; cout << "Please enter the ciphertext:"; cin >> st; flag = Check(st); } while (flag); //第一次解密 do { //输入key fflush(stdin); cout << "Please enter the key2: "; cin >> key; flag = Check(key); } while (flag); if (((st.length()) % (key.length())) != 0) { cout << endl << "Error!The length of the ciphertext must be an integral multiple of the length of the key!" << endl << endl; exit(-1); } p = wordKey(key, k); //组建明文序列 m = 0; n = (st.length()) / (key.length()); for (i = 0; i < key.length(); i++) { for (j = 0; j < n; j++) { plain[j][(p[i] - 1)] = st[m]; m++; } } //打印plain matrix q = 0; cout << "Plaintext Matrix" << endl << endl; for (i = 0; i < n; i++) { for (j = 0; j < key.length(); j++) { cout << " " << plain[i][j]; q++; } cout << endl; } cout << endl; //输出明文 cout << "Middle Plaintext:"; for (i = 0; i < n; i++) { for (j = 0; j < key.length(); j++) { cout << plain[i][j]; } } cout << endl << endl; //第二次解密 do { //输入key fflush(stdin); cout << "Please enter the key1: "; cin >> key2; flag = Check(key2); } while (flag); i = 0; d = st.length(); n = (st.length()) / (key.length()); if ((d % key2.length()) != 0) { //检查第一次加密是否使用了无效字符'x',若使用则删除字符'x' while ((d%key2.length() != 0) && (plain[n - 1][(p[i] - 1)] == 'x') && i < key.length()) { plain[n - 1][p[i] - 1] = '\0'; d--; i++; } //如果删除字符'x'后仍然不可整除key2,则打印错误信息,提示输入有误 if ((d % key2.length()) != 0) { cout << endl << "Error!The key or the ciphertext that you have entered is wrong!" << endl << endl; exit(-1); } //组建临时中间明文 q = 0; for (i = 0; i < n; i++) { for (j = 0; j < key.length(); j++) { if (plain[i][j] != '\0') tem[q] = plain[i][j]; q++; } } } else { q = 0; for (i = 0; i < n; i++) { for (j = 0; j < key.length(); j++) { tem[q] = plain[i][j]; q++; } } } p2 = wordKey(key2, k2); m = 0; n = (strlen(tem)) / (key2.length()); for (i = 0; i < key2.length(); i++) { for (j = 0; j < n; j++) { plain2[j][(p2[i] - 1)] = tem[m]; m++; } } //打印plain matrix cout << "Plaintext Matrix" << endl << endl; for (i = 0; i < n; i++) { for (j = 0; j < key2.length(); j++) { cout << " " << plain2[i][j]; } cout << endl; } cout << endl; //输出明文 cout << "Plaintext:"; for (i = 0; i < n; i++) { for (j = 0; j < key2.length(); j++) { cout << plain2[i][j]; } } cout << endl << endl; } |
(编译运行环境:g++ [Debian 7.3.0-19] 7.3.0)
说点什么