Hill加密
希尔密码(Hill Cipher)是运用基本矩阵论原理的替换密码,由Lester S. Hill在1929年发明。每个字母当作26进制数字:A=0, B=1, C=2… 一串字母当成n维向量,跟一个n×n的矩阵相乘,再将得出的结果MOD26
c++实现加密算法:
#include <iostream>
using namespace std;
int getNum(char c);
char getLettet(int a);
int main()
{
/**本代码以3个字母的长度为例*/
int p[3];//用来存储明文
int c[3]={0,0,0};//用来存储密文
int k[3][3] ={17,17,5,21,18,21,2,2,19};//密匙
cout<<"输入3个小写英文s字母的明文:"<<endl;
for(int i=0;i<3;i++){
char tem;
cin >>tem;
p[i]=getNum(tem);
}
cout<<"密文是:"<<endl;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i]+=p[j]*k[j][i];
}
cout<<getLettet(c[i]%26);
}
return 0;
}
int getNum(char c){
//将字符转为数字
switch(c){
case 'a':return 0;
case 'b':return 1;
case 'c':return 2;
case 'd':return 3;
case 'e':return 4;
case 'f':return 5;
case 'g':return 6;
case 'h':return 7;
case 'i':return 8;
case 'j':return 9;
case 'k':return 10;
case 'l':return 11;
case 'm':return 12;
case 'n':return 13;
case 'o':return 14;
case 'p':return 15;
case 'q':return 16;
case 'r':return 17;
case 's':return 18;
case 't':return 19;
case 'u':return 20;
case 'v':return 21;
case 'w':return 22;
case 'x':return 23;
case 'y':return 24;
case 'z':return 25;
}
return -1;
}
char getLettet(int a){
//将数字转为明文
switch(a){
case 0:return 'a';
case 1:return 'b';
case 2:return 'c';
case 3:return 'd';
case 4:return 'e';
case 5:return 'f';
case 6:return 'g';
case 7:return 'h';
case 8:return 'i';
case 9:return 'j';
case 10:return 'k';
case 11:return 'l';
case 12:return 'm';
case 13:return 'n';
case 14:return 'o';
case 15:return 'p';
case 16:return 'q';
case 17:return 'r';
case 18:return 's';
case 19:return 't';
case 20:return 'u';
case 21:return 'v';
case 22:return 'w';
case 23:return 'x';
case 24:return 'y';
case 25:return 'z';
}
return -1;
}
- 在线加密/解密 网站:http://www.atoolbox.net/Tool.php?Id=914