Vigenère密码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Vigenère密码相关的知识,希望对你有一定的参考价值。
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,且都仅包含英文字母。
#include<iostream> #include<cstring> using namespace std; char k[101],m[1001]; int ki[101],mi[1001],lk,lm,sk,sm,x,y,s;//表打的太可爱=。= 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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}, {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}}; int main() { cin>>k+1>>m+1; lk=strlen(k+1); lm=strlen(m+1); while(sm<lm) { if(sk==lk) sk=0; int o=0,j; x=int(m[++sm]); y=int(k[++sk])-97; if(x<97) { x+=32; o=1; } if(y<0) y+=32; for(j=0; j<=25; j++) { if(f[j][y]==x) break; } if(o==1) cout<<char(j+97-32); else cout<<char(j+97); } return 0; }
以上是关于Vigenère密码的主要内容,如果未能解决你的问题,请参考以下文章