?? vigenere.cpp
字號(hào):
// Vigenere.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
/*維吉尼亞密碼的C語言源代碼
設(shè)m表示明文序列,k表示密鑰序列,c表示加密后的密文序列,d表示密鑰長度,q表示明文/密文空間元素個(gè)數(shù),則維吉尼亞密碼可以描述為
ci+td=(mi+td+ki) mod q (i=0,1,2,...,d-1)
*/
#include <ctype.h>
#include <conio.h>
#include <string.h>
#include <iostream.h>
void crypt(char m[],char k[],char r[])
{
int i,j,s=0;
j=strlen(k);
for(i=0;m[i];i++)
m[i]=tolower(m[i]);
for(i=0;k[i];i++)
k[i]=tolower(k[i]);
for(i=0;m[i];i++)
if(isalpha(m[i]))
{
r[i]=(m[i]-'a'+k[s%j]-'a')%26+'a';
s++;/*s用來跳過明文中的空格字符*/
}
else
r[i]=m[i];
r[i]=0;/*密文字符串結(jié)束符*/
// for(i=0;r[i];i++)
// r[i]=toupper(r[i]);
}
void decrypt(char c[],char k[],char m[])
{
int i,j,s=0;
j=strlen(k);
for(i=0;c[i];i++)
c[i]=tolower(c[i]);
for(i=0;k[i];i++)
k[i]=tolower(k[i]);
for(i=0;c[i];i++)
if(isalpha(c[i]))
{
m[i]=(c[i]-k[s%j]+26)%26+'a';
s++;
}
else
m[i]=c[i];
m[i]=0;
}
void main(int argc, char *argv[])
{
char m[]="hello world";
char ascii[]="abcdefghijklmnopqrstuvwxyz";
char plaintext[]="abcdefghijklmnopqrstuvwxyz";
char ciphertext[100]={0};
printf("古典密碼算法演示程序(譚文學(xué)2006年3月。)");
// for(int i=0;i<strlen(ascii);i++) printf("%c;", ascii[i]);
int i(0);
int key=26+2;
char command;
/* char command;
if (argc < 2)
command = 'h';
else
command = argv[1][0];
*/
printf("\n");
cout <<"輸入k:啟動(dòng)凱撒加密!\n";
cout <<"輸入v:啟動(dòng)維吉尼亞加密!\n";
cout <<"輸入h:獲得命令幫助!\n";
cout <<"輸入e:退出程序!\n";
cout <<"請輸入指令:";
loop:
cin >> command;
switch (command)
{
case 'k':
{
cout << "input key: ";
cin >> key;
cout << "\ninput plaintext: ";
cin >> plaintext;
printf("\n密鑰:%d",key);
printf("\n加密得到密文:\n");
for(i=0;i<strlen(plaintext);i++) printf("%c;",ciphertext[i]=(plaintext[i]-'a'+key)%26+'a');
printf("\n解密得到明文:\n");
for(i=0;i<strlen(plaintext);i++) printf("%c;",(26+(ciphertext[i]-'a'-key)%26)%26+'a');
printf("\n");
goto loop;
}
case 'v':
{
char k[]="best";
cout <<"\ninput plaintext: ";
cin >> m;
char c[80];
char d[80];
// clrscr();
crypt(m,k,c);
decrypt(c,k,d);
puts(m);
puts(k);
puts(c);
puts(d);
goto loop;
}
case 'e':
{
return;
}
case 'h': ;
{
cout << "輸入k:啟動(dòng)凱撒加密!\n";
cout << "輸入v:啟動(dòng)維吉尼亞加密!\n";
cout << "輸入h:獲得命令幫助!\n";
cout << "輸入e:退出程序!\n";
goto loop;
}
default:goto loop;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -