資源簡介
用維吉尼亞密碼實現控制臺對文件的加解密 形式:
cipher -e/-d key inputfile outputfile
代碼片段和文件信息
#include?
#include?
#define?MAX_LEN?100
//加密函數
char*?Encryption(char?Text[]char?Key[]char?cipherText[]int?textLen){
int?i;
for?(i=0;i {
if?(*(Text+i)>=‘a‘)
{
*(cipherText+i)=((*(Text+i)-‘a‘)+(*(Key+i)-‘a‘))%26+‘a‘;
}else?if?(*(Text+i)<=‘Z‘)
{
*(cipherText+i)=((*(Text+i)-‘A‘)+(*(Key+i)-‘a‘))%26+‘A‘;//秘鑰只能為小寫字母組成
}
}
//轉換大小寫
for?(i=0;i {
if?(cipherText[i]>=‘a‘&&cipherText[i]<=‘z‘){
cipherText[i]-=32;
}
else?if?(cipherText[i]>=‘A‘&&cipherText[i]<=‘Z‘)
{
cipherText[i]+=32;
}
}
}
//解密函數
char*?Decryption(char?Text[]char?Key[]char?cipherText[]int?cipherLen){
int?i;
for?(i=0;i {
if?(*(cipherText+i)>=‘a‘)
{
if((*(cipherText+i)-‘a‘)-(*(Key+i)-‘a‘)<0){
*(Text+i)=(26+((*(cipherText+i)-‘a‘)-(*(Key+i)-‘a‘))%26)+‘a‘;
}else{
*(Text+i)=((*(cipherText+i)-‘a‘)-(*(Key+i)-‘a‘))%26+‘a‘;
}
}else?if?(*(Text+i)<=‘Z‘)
{
if((*(cipherText+i)-‘A‘)-(*(Key+i)-‘a‘)<0){
*(Text+i)=(26+((*(cipherText+i)-‘A‘)-(*(Key+i)-‘a‘))%26)+‘A‘;
}else{
*(Text+i)=((*(cipherText+i)-‘A‘)-(*(Key+i)-‘a‘))%26+‘A‘;
}//秘鑰只能為小寫字母組成
}
}
//轉換大小寫
for?(i=0;i {
if?(Text[i]>=‘a‘&&Text[i]<=‘z‘){
Text[i]-=32;
}
else?if?(Text[i]>=‘A‘&&Text[i]<=‘Z‘)
{
Text[i]+=32;
}
}
}
//明密文處理函數(去掉空格和標點符號)
char*?chuliText(char?text[]){
int?ik;
//剔除標點符號和空格
for?(i=0k=0;text[i]!=0;i++)
{
if?((text[i]>=‘a‘&&text[i]<=‘z‘)||(text[i]>=‘A‘&&text[i]<=‘Z‘))
{
text[k]=text[i];
k++;
}
}
text[k]=‘\0‘;
return?text;
}
int?main(int?argcchar?*argv[]){
FILE?*fp;
char?plainText[MAX_LEN]={0}key[MAX_LE
- 上一篇:Gabor濾波器紋理特征提取
- 下一篇:電梯仿真.zip
評論
共有 條評論