Vigenère密码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vigenère密码相关的知识,希望对你有一定的参考价值。
1197 Vigenère密码
2012年NOIP全国联赛提高组
16 世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法——Vigenère密码。Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为南军所广泛使用。
在密码学中,我们称需要加密的信息为明文,用 M 表示;称加密后的信息为密文,用C 表示;而密钥是一种参数,是将明文转换为密文或将密文转换为明文的算法中输入的数据,记为k。 在Vigenère密码中, 密钥k是一个字母串, k=k1k2…kn。当明文M=m1m2…mn时,得到的密文C=c1c2…cn,其中ci=mi®ki,运算®的规则如下表所示:
Vigenère加密在操作时需要注意:
1. ®运算忽略参与运算的字母的大小写,并保持字母在明文 M中的大小写形式;
2. 当明文M的长度大于密钥k的长度时,将密钥k 重复使用。
例如,明文M=Helloworld,密钥k=abc 时,密文C=Hfnlpyosnd。
明文 |
H |
e |
l |
l |
o |
w |
o |
r |
l |
D |
密钥 |
a |
b |
c |
a |
b |
c |
a |
b |
c |
a |
密文 |
H |
f |
n |
l |
p |
y |
o |
s |
n |
d |
输入共2行。
第一行为一个字符串,表示密钥k,长度不超过100,其中仅包含大小写字母。第二为一个字符串,表示经加密后的密文,长度不超过1000,其中仅包含大小写字母
输出共1行,一个字符串,表示输入密钥和密文所对应的明文
CompleteVictory
Yvqgpxaimmklongnzfwpvxmniytm
Wherethereisawillthereisaway
对于 100%的数据,输入的密钥的长度不超过 100,输入的密文的长度不超过 1000,且都仅包含英文字母。
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 char k[101],m[1001]; 5 int ki[101],mi[1001],lk,lm,sk,sm,x,y,s;//打表太可爱 6 int f[26][26]={{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}, 7 {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,97}, 8 {99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98}, 9 {100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99}, 10 {101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100}, 11 {102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101}, 12 {103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102}, 13 {104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103}, 14 {105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104}, 15 {106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105}, 16 {107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106}, 17 {108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107}, 18 {109,110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108}, 19 {110,111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109}, 20 {111,112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110}, 21 {112,113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111}, 22 {113,114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112}, 23 {114,115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113}, 24 {115,116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114}, 25 {116,117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115}, 26 {117,118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116}, 27 {118,119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117}, 28 {119,120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118}, 29 {120,121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119}, 30 {121,122,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120}, 31 {122,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}}; 32 int main() 33 { 34 cin>>k+1>>m+1; 35 lk=strlen(k+1); 36 lm=strlen(m+1); 37 while(sm<lm) 38 { 39 if(sk==lk) 40 sk=0; 41 int o=0,j; 42 x=int(m[++sm]); 43 y=int(k[++sk])-97; 44 if(x<97) 45 { 46 x+=32; 47 o=1; 48 } 49 if(y<0) 50 y+=32; 51 for(j=0; j<=25; j++) 52 { 53 if(f[j][y]==x) 54 break; 55 } 56 if(o==1) 57 cout<<char(j+97-32); 58 else 59 cout<<char(j+97); 60 } 61 return 0; 62 }
以上是关于Vigenère密码的主要内容,如果未能解决你的问题,请参考以下文章