#include <iostream>
#include <string.h>
#include <string>
using namespace std;
int main() {
int key = 0;
while(cin >> key && key != 0) {
char plaintext[71] = {'\0'}, ciphertext[71]= {'\0'};
int plaincode[71]= {0}, ciphercode[71]= {0};
int n = 0;
cin >> ciphertext;
n = strlen(ciphertext);
//完成密文数组的编码
for(int i = 0; i < n; i++) {
if(ciphertext[i] == '_')
ciphercode[i] = 0;
else if(ciphertext[i] == '.')
ciphercode[i] = 27;
else
ciphercode[i] = ciphertext[i]- 'a' + 1;
}
// 这段程序想的复杂了!!!
// for(int j = 0; j < n; j++) {
// int index = (key*j) % n;
// for(int t = 0; ; t--) {
// int res = 28 * t + ciphercode[j] +j;
// if(res >= 0 && res <= 27) {
// plaincode[index] = res;
// break;
// }
// }
// }
//已知 x = (y-k) mod 28,求 y mod 28 = (x+k) mod 28, 将密文编码数组转换成明文编码数组
for(int j = 0; j < n; j++)
plaincode[(key*j)% n] = (ciphercode[j] + j) % 28;
//将明文编码数组解码成明文字符
for(int j = 0; j < n; j++) {
if(plaincode[j] == 0)
plaintext[j] = '_';
else if(plaincode[j] == 27)
plaintext[j] = '.';
else
plaintext[j] = plaincode[j] + 'a' -1;
cout << plaintext[j];
}
cout << endl;
}
return 0;
} zoj 1006 Do the Untwist
C++解密算法实现
最新推荐文章于 2024-05-20 09:35:39 发布
本文介绍了一种基于C++的解密算法实现方法,通过输入密文和密钥,利用特定的数学运算来还原原始的明文。该算法特别之处在于使用了模运算和数组操作来处理字母和特殊符号,最终将解密后的明文输出。
7146

被折叠的 条评论
为什么被折叠?



