多表代替密码,一阶的hill密码
加密: c=p+ki(mod26)
解密: p=c-ki(mod26)
密钥k循环使用
读文件"in.txt"加密结果放在"encode.txt"中,解密结果放在"decode.txt"中
#include <iostream> #include <cstring> #include <math.h> #include <fstream> using namespace std; char temp; char ans; char k[100]; int len_k; int j=0; int i; void encode() { temp=(temp-97+(k[j]-97))%26+97; i=j; j++; j=j%len_k; } void decode() { int sum=temp-97-(k[i]-97); while(sum<0) { sum+=26; } ans=sum%26+97; } int main() { cin.getline(k,100); len_k=strlen(k); ifstream in("in.txt"); ofstream out1("encode.txt"); ofstream out2("decode.txt"); while(in.get(temp)) { if(!(temp>='a'&&temp<='z')) { out1<<temp; out2<<temp; continue; } encode(); out1<<temp; decode(); out2<<ans; } in.close(); out1.close(); out2.close(); return 0; }