使用C语言实现古典加密

合集下载

凯撒密码--C语言实现

凯撒密码--C语言实现

凯撒密码是一种非常古老的加密方法,相传当年凯撒大地行军打仗时为了保证自己的命令不被敌军知道,就使用这种特殊的方法进行通信,以确保信息传递的安全。

他的原理很简单,说到底就是字母于字母之间的替换。

下面让我们看一个简单的例子:“baidu”用凯撒密码法加密后字符串变为“edlgx”,它的原理是什么呢?把“baidu”中的每一个字母按字母表顺序向后移3位,所得的结果就是刚才我们所看到的密文。

/**凯撒密码实现要求,将明文字母变成它后面第三个字母,后面的循环到前面!公式为f(a)=(f(a)+3)%26*/#include <stdio.h>int main(){char P[100];/*定义明文长度*/char C[100];/*定义密文长度*/int K=3,i;printf("Please input Plaintext:\n"); /*输入明文*/gets(P); /* 接受明文*/for(i=0;P[i]!='\0';i++) { /*逐个判断字母的大小*/if(P[i]>='a'&&P[i]<='z') /*小写字母*/C[i]=(P[i]-'a'+K)%26+'a';else if(P[i]>='A'&&P[i]<='Z')/*大写字母*/C[i]=(P[i]-'A'+K)%26+'A';else C[i]=' ';/*如果不是字母,转换为空格*/}printf("The Ciphertext is :\n%s\n",C);/*输出密文*/getch();return 0;}1、程序结构化,用函数分别实现2、对文件的加密,解密输出到文件#include<stdio.h>#include<conio.h>void menu()/*菜单,1.加密2.解密3.退出*/{clrscr();printf("\n=============================================================== ================");printf("\n1.Encrypt the file");printf("\n2.Decrypt the file");printf("\n3.Quit\n");printf("================================================================ ===============\n");printf("Please select a item:");return;}char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/{while(ch>='A'&&ch<='Z'){return ('A'+(ch-'A'+n)%26);}while(ch>='a'&&ch<='z'){return ('a'+(ch-'a'+n)%26);}return ch;}main(){int i,n;char ch0,ch1;FILE *in,*out;char infile[10],outfile[10];textbackground(RED);textcolor(LIGHTGREEN);clrscr();menu();ch0=getch();while(ch0!='3'){if(ch0=='1'){clrscr();printf("\nPlease input the infile:");scanf("%s",infile);/*输入需要加密的文件名*/if((in=fopen(infile,"r"))==NULL){printf("Can not open the infile!\n");printf("Press any key to exit!\n");getch();exit(0);}printf("Please input the key:");scanf("%d",&n);/*输入加密密码*/printf("Please input the outfile:");scanf("%s",outfile);/*输入加密后文件的文件名*/if((out=fopen(outfile,"w"))==NULL){printf("Can not open the outfile!\n");printf("Press any key to exit!\n");fclose(in);getch();exit(0);}while(!feof(in))/*加密*/{fputc(encrypt(fgetc(in),n),out);}printf("\nEncrypt is over!\n");fclose(in);fclose(out);sleep(1);}if(ch0=='2'){clrscr();printf("\nPlease input the infile:");scanf("%s",infile);/*输入需要解密的文件名*/if((in=fopen(infile,"r"))==NULL) {printf("Can not open the infile!\n");printf("Press any key to exit!\n");getch();exit(0);}printf("Please input the key:");scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/ n=26-n;printf("Please input the outfile:");scanf("%s",outfile);/*输入解密后文件的文件名*/if((out=fopen(outfile,"w"))==NULL){printf("Can not open the outfile!\n");printf("Press any key to exit!\n");fclose(in);getch();exit(0);}while(!feof(in)){fputc(encrypt(fgetc(in),n),out);}printf("\nDecrypt is over!\n");fclose(in);fclose(out);sleep(1);}clrscr();printf("\nGood Bye!\n");sleep(3);getch();}}---------------------------------------------------------------------------------------------------------------------------------------------/*移位法:*/#include <stdio.h>#include <stdlib.h>char *Encrypt(char *pwd,int key) /*加密*/{for(int i=0;*(pwd+i)!='\0';i++){if(*(pwd+i)>='a'&&*(pwd+i)<='z')*(pwd+i)=(*(pwd+i)-'a'+key)%26+'a';else if(*(pwd+i)>='A'&&*(pwd+i)<='Z')*(pwd+i)=(*(pwd+i)-'A'+key)%26+'A';}return pwd;}char *Decrypt(char *pwd,int key) /*解密*/{for(int i=0;*(pwd+i)!='\0';i++){if(*(pwd+i)>='a'&&*(pwd+i)<='z'){if(*(pwd+i)-'a'>=key%26)*(pwd+i)=*(pwd+i)-key%26;else *(pwd+i)='z'-(key%26-(*(pwd+i)-'a'))-1;}else if(*(pwd+i)>='A'&&*(pwd+i)<='Z'){if(*(pwd+i)-'A'>=key%26)*(pwd+i)=*(pwd+i)-key%26;else *(pwd+i)='Z'-(key%26-(*(pwd+i)-'A'))-1;}return pwd;}void main(){char *pwd;int key;pwd=(char*)malloc(sizeof(char));printf("Input your password:");gets(pwd);printf("Input a key:");scanf("%d",&key);printf("The Ciphertext is:");printf("%s\n",Encrypt(pwd,key));}------------------------------------------------------------------/*替换法:*/#include <stdio.h>#include <stdlib.h>#include <string.h>void table(char *keyword) /*筛选密钥(去重复去空格)*/{int i,j,k;for(i=0;*(keyword+i)!='\0';i++){for(j=i;*(keyword+j)!='\0';j++){if(i!=j)if(*(keyword+i)==*(keyword+j)||*(keyword+j)==' '){for(k=j;*(keyword+k)!='\0';k++)*(keyword+k)=*(keyword+k+1);j--;}}}}void newTab(char *keyword) /*生成密钥表*/{char ch;int t;for(t=0;*(keyword+t)!='\0';t++);for(ch='a';ch<='z';ch++){for(i=0;*(keyword+i)!=ch;i++){if(*(keyword+i)=='\0'){*(keyword+t)=ch;t++;break;}}}*(keyword+t)='\0';}char *Ciphertext(char *keyword,char *Plaintext) /*按密码表加密*/ {char ch;int i,j;for(i=0;*(Plaintext+i)!='\0';i++){for(ch='a',j=0;ch<='z';ch++,j++){if(*(Plaintext+i)==ch){*(Plaintext+i)=*(keyword+j);break;}}}return Plaintext;}char *Decrypt(char *keyword,char *Plaintext) /*解密*/{char ch;int i,j;for(i=0;*(Plaintext+i)!='\0';i++){for(ch='a',j=0;*(keyword+j)!='\0';ch++,j++){if(*(Plaintext+i)==*(keyword+j)){*(Plaintext+i)=ch;break;}}}return Plaintext;}void main(){char *keyword,*Plaintext,*tmp=NULL;keyword=(char*)malloc(sizeof(char));Plaintext=(char*)malloc(sizeof(char));printf("Input key word:"); /*输入欲用密钥*/gets(keyword);printf("Input Plaintext:"); /*输入要转换的明文*/gets(Plaintext);table(keyword); /*去空格去重复*/newTab(keyword); /*生成密码表*/tmp=Ciphertext(keyword,Plaintext); /*对应着密码表生成密文*/ puts(tmp); /*输出密文*/puts(Decrypt(keyword,tmp)); /*解密输出*/}。

四种古典密码的C++实现(3)-----Playfair密码

四种古典密码的C++实现(3)-----Playfair密码

四种古典密码的C++实现(3)-----Playfair密码 1//Playfair密码2/*理解算法最重要,最好⾃⼰动⼿实现试试看,可以使⽤MFC写⼀个简单的交互界⾯*/3 #include<iostream>4 #include<cstring>56using namespace std;7void encrypt()8 {9const int N=100;10char letters[26]="ABCDEFGHIKLMNOPQRSTUVWXYZ";//⽤于填充矩阵11int flag[25]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};//字母是否已在矩阵中,与letters数组对应12char ch[5][5];//5X5矩阵13char ch1[N];//密钥14char ch2[N];//明⽂15char ch4;//⽆关字符16int len='a'-'A';17 cout<<"输⼊密钥:";18 cin>>ch1;19int flg=1;20while(flg==1)21 {22for(int i=0;i<strlen(ch1);i++)//把所输⼊的密钥转化为⼤写字母23 {24if(ch1[i]>'z'||ch1[i]<'a')25 {26 cout<<"请重新选择操作:"<<endl;27 flg=0;break;28 }29else30 ch1[i]=ch1[i]-len;31 }32if(flg==1)33 { for(int i=0;i<strlen(ch1);i++)//把密钥中的J都变为I34 {35if(ch1[i]=='J')ch1[i]='I';36 }37int i=0;int j=0;38//把密钥中的字母填⼊到矩阵中,并把该字母标记为已⽤39for(int k=0;k<strlen(ch1);k++)40 {41for(int t=0;t<25;t++)42 {43if(ch1[k]==letters[t]&&flag[t]==0)44 {45 ch[i][j]=letters[t];46 flag[t]=1;47if(j<4)j++;48else {i++;j=0;}49 }50 }51 }52for( int k=0;k<25;k++)//按字母表顺序把未⽤字母依次填⼊到矩阵中53 {54if(flag[k]==0)55 {56 ch[i][j]=letters[k];57 flag[k]=1;58if(j<4)j++;59else{i++;j=0;}60 }61 }62 cout<<"密钥填充后的矩阵为: "<<endl;63for(i=0;i<5;i++)64for(j=0;j<5;j++)65 {66 cout<<ch[i][j];67 cout<<"";68if(j==4)69 cout<<endl;70 }71 cout<<endl;72 cout<<"请输⼊明⽂(请输⼊英⽂字符):";73 cin>>ch2;74 cout<<"输⼊⼀个⽆关字符:";75 cin>>ch4;76if(ch4>='a')77 ch4=ch4-len;78for(int k=0;k<strlen(ch2);k++)//把所输⼊的明⽂转化为⼤写字母79 {80if(ch2[k]>='a')81 ch2[k]=ch2[k]-len;82 }83for(int k=0;k<strlen(ch2);k++)//把明⽂中的J都变为I84 {85if(ch2[k]=='J')86 ch2[k]='I';87 }88//为明⽂添加必要的⽆关字符以防⽌同⼀组的两个字符相同89for( int k=0;k<strlen(ch2);k+=2)90 {91if(ch2[k]==ch2[k+1])92 {93for(int t=strlen(ch2);t>k;t--)94 ch2[t+1]=ch2[t];95 ch2[k+1]=ch4;96 }97 }98//若明⽂有奇数个字符,则添加⼀个⽆关字符以凑够偶数个99if(strlen(ch2)%2!=0)100 {101 ch2[strlen(ch2)+1]=ch2[strlen(ch2)];//字符串结尾赋'\0'102 ch2[strlen(ch2)]=ch4;//明⽂串尾插⼊⽆关字符103 }104 cout<<"经过处理后的明⽂为:";105for(int k=0;k<strlen(ch2);k+=2)106 cout<<ch2[k]<<ch2[k+1]<<"";107 cout<<endl;108 cout<<"其最终长度为:"<<strlen(ch2)<<endl;109//////////////////明⽂输⼊并整理完毕///////////////////////////////110for(int k=0;k<strlen(ch2);k+=2)111 {112int m1,m2,n1,n2;113for(m1=0;m1<=4;m1++)114 {for(n1=0;n1<=4;n1++)115 {116if(ch2[k]==ch[m1][n1])break;117 }118if(ch2[k]==ch[m1][n1])break;119 }120for(m2=0;m2<=4;m2++)121 {122for(n2=0;n2<=4;n2++)123 {124if(ch2[k+1]==ch[m2][n2])break;125 }126if(ch2[k+1]==ch[m2][n2])break;127 }128 m1=m1%5;129 m2=m2%5;130if(n1>4){n1=n1%5;m1=m1+1;}131if(n2>4){n2=n2%5;m2=m2+1;}132if(m1==m2)133 {134 ch2[k]=ch[m1][(n1+1)%5];135 ch2[k+1]=ch[m2][(n2+1)%5];136 }137else138 {139if(n1==n2)140 {141 ch2[k]=ch[(m1+1)%5][n1];142 ch2[k+1]=ch[(m2+1)%5][n2];143 }144else145 {ch2[k]=ch[m1][n2];146 ch2[k+1]=ch[m2][n1];147 }148 }149 }150 cout<<"加密后所得到的密⽂是:";151for(int k=0;k<strlen(ch2);k+=2)152 cout<<ch2[k]<<ch2[k+1]<<"";153 cout<<endl;154 }else break;155 }156157 }158159//解密算法160void decrypt()161 {162const int N=100;163char letters[26]="ABCDEFGHIKLMNOPQRSTUVWXYZ";//⽤于填充矩阵164int flag[25]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};165//标记字母是否已在矩阵中,与letters数组对应166char ch[5][5];//5X5矩阵167char ch1[N];//密钥168char ch2[N];//密⽂169int len='a'-'A';170int flg=1;171 cout<<"输⼊密钥:";172 cin>>ch1;173while(flg==1)174 {175for(int i=0;i<strlen(ch1);i++)//把所输⼊的密钥转化为⼤写字母176 {if(ch1[i]>'z'||ch1[i]<'a')177 {178 cout<<"请重新选择操作:"<<endl;179 flg=0;break;180 }181else182 ch1[i]=ch1[i]-len;183 }184if(flg==1)185 { for(int i=0;i<strlen(ch1);i++)//把密钥中的J都变为I186 {187if(ch1[i]=='J')ch1[i]='I';188 }189int i=0;int j=0;190//把密钥中的字母填⼊到矩阵中,并把该字母标记为已⽤191for(int k=0;k<strlen(ch1);k++)192 {193for( int t=0;t<25;t++)194 {195if(ch1[k]==letters[t]&&flag[t]==0)196 {197 ch[i][j]=letters[t];198 flag[t]=1;199if(j<4)j++;200else {i++;j=0;}201 }202 }203 }204for( int k=0;k<25;k++)//按字母表顺序把未⽤字母依次填⼊到矩阵中205 {206if(flag[k]==0)207 {208 ch[i][j]=letters[k];209 flag[k]=1;210if(j<4)j++;211else{i++;j=0;}212 }213 }214 cout<<"密钥填充后的矩阵为: "<<endl;215for(i=0;i<5;i++)216217for(j=0;j<5;j++)218 {219 cout<<ch[i][j];220 cout<<"";221if(j==4)222 cout<<endl;223 }224 cout<<endl;225/////////////////////矩阵⽣成完毕////////////////////////////226int f=0;227do{228 cout<<"请输⼊密⽂(英⽂字符):";229 cin>>ch2;230for(int k=0;k<strlen(ch2);k++)//把所输⼊的密⽂转化为⼤写字母231 {232if(ch2[k]>='a')233 ch2[k]=ch2[k]-len;234 }235for( int k=0;k<strlen(ch2);k++)//把密⽂中的J都变为I236 {237if(ch2[k]=='J')ch2[k]='I';238 }239for( int k=0;k<strlen(ch2);k+=2)240 {241if(ch2[k]==ch2[k+1])242 {243 cout<<"同⼀分组中不能出现相同字符!请重新输⼊。

AESECB加密算法C语言代码实现

AESECB加密算法C语言代码实现

AESECB加密算法C语言代码实现要实现AESECB加密算法的C语言代码,需要先了解AES算法的基本原理和步骤。

以下是一个使用C语言实现AESECB加密算法的示例代码:```c#include <stdio.h>#include <stdlib.h>#include <string.h>#include <openssl/aes.h>//定义密钥长度#define KEY_LENGTH 16//ECBAES加密函数void AESEncrypt_ECB(const unsigned char* plainText, int plainTextLength, unsigned char* cipherText,const unsigned char* key)//创建AES密钥结构体AES_KEY aesKey;//设置加密密码AES_set_encrypt_key(key, KEY_LENGTH * 8, &aesKey);//加密数据AES_ecb_encrypt(plainText, cipherText, &aesKey, AES_ENCRYPT);//ECBAES解密函数void AESDecrypt_ECB(const unsigned char* cipherText, int cipherTextLength, unsigned char* plainText,const unsigned char* key)//创建AES密钥结构体AES_KEY aesKey;//设置解密密码AES_set_decrypt_key(key, KEY_LENGTH * 8, &aesKey);//解密数据AES_ecb_encrypt(cipherText, plainText, &aesKey, AES_DECRYPT);int mai//指定原始明文和密钥unsigned char plainText[] = "Hello, World!";unsigned char key[] = "secretkey";//计算明文长度int plainTextLength = strlen(plainText);//计算加密后的数据长度int cipherTextLength = ((plainTextLength / KEY_LENGTH) + 1) * KEY_LENGTH;//分配加密后数据的内存unsigned char* cipherText = (unsignedchar*)malloc(cipherTextLength);//加密数据AESEncrypt_ECB(plainText, plainTextLength, cipherText, key);//打印加密后的结果printf("Cipher text: ");for (int i = 0; i < cipherTextLength; i++)printf("%02x ", cipherText[i]);}printf("\n");//分配解密后数据的内存unsigned char* decryptedText = (unsignedchar*)malloc(cipherTextLength);//解密数据AESDecrypt_ECB(cipherText, cipherTextLength, decryptedText, key);//打印解密后的结果printf("Decrypted text: %s\n", decryptedText);//释放已分配的内存free(cipherText);free(decryptedText);return 0;```上述代码使用了OpenSSL库提供的AES函数来实现ECB模式的AES加密和解密操作。

单表古典密码的加密运算精品文档7页

单表古典密码的加密运算精品文档7页

单表古典密码的加密运算实验目的:1. 熟练掌握单表古典密码加密算法原理及实现;2. 掌握单表古典密码加密算法的应用;实验内容:1、写出单表古典密码的加法加密运算的算法、程序设计;2、写出意表古典密码的乘法加密运算的算法、程序设计;3、输入模q(=12)及加密密钥k(=5)对下列明文进行加法加密和乘法加密:A graph is finite if both its vertex set and edge set are finite.In this book we study only finite graphs, and so the term ‘graph’always means ‘finite graph’.(注:标点符号及空格也算一个符号,忽略大小)4、求出相对应于上述q和k的解密密钥。

实验结果:1.(1)写出单表古典密码的加法加密运算的算法设X=Y=Z q,K=Z q。

对任意m∈X,k∈K,密文q=k(,c K k+=)mm mod)(加法密码的密钥量为q。

Step1:确定k和q的值;Step2:输入相应的密文;Step3:作变换 c=(m+k) mod q,字母用ASCII码变为数字后计算;Step4:得到相对应的明文;Step5:逐一输出明文;(2)单表古典密码的加法加密运算的程序设计如下:#include<iostream.h>void main()int jiami(int q,int k,int i);char m[100],c[100];int i,j,q,k;cout<<"输入模q:";cin>>q;cout<<"输入密钥k:";cin>>k;cout<<"输入"<<q<<"个明文:"<<endl;for(i=0;i<q;i++)cin>>m[i];for(i=0;i<q;i++)j=jiami(q,k,i);c[i]=m[j];cout<<"加密后的密文为:"<<endl;for(i=0;i<q;i++)cout<<m[i];cout<<endl;int jiami(int q,int k,int i)return (i+k)%q;单表古典密码的加法加密运算运行结果如下:2.(1)写出意表古典密码的乘法加密运算的算法:设X=Y=Z q,K=Z*q。

实验一古典加密算法的实现

实验一古典加密算法的实现

凯撒密码映射表
明 A B C D E 文 密 D E F G H 文 F G H I J K L M I J K L M N O P
明 N O P Q R 文
S T U V W X Y Z
密 Q R S T U V W X Y Z撒密码的算法,对”Thisisanew term”进行加密 • 要求:(1)
要求
• 以密钥为:m*n=3 * 4,以及f= ((1234)(2413))对” ENGINEERING” 进行: • 要求:(1)
– 输入: ENGINEERING – 输出:(密文)
• (2)
– 输入:密文 – 输出: ENGINEERING
实验一 古典加密算法的实现
• 实验目的:
– 通过编程实现凯撒密码算法和矩阵换位密码算 法,加深对古典密码体制的了解。
• 实验原理:
– 古典加密算法的实验原理(详见下文ppt)
• 实验环境:
– 运行Windows操作系统的PC,TC编译环境
(1)凯撒密码加密算法
• 典型的单表代替密码是凯撒密码。 • 凯撒密码是最古老的代替密码,以英文26个字母 为例,它用D表示A,用E表示B,用F表示C,…, 用C表示Z,即:密文字母是明文字母后面的第三 个位置上的字母。这种映射关系表示为如下函数: • F(a)=(a十k)mod n • 其中:a表示明文字母,n为字符集中字母 个数,k=3为密钥。(移位密码、加法密码)
– 输入:Thisisanewterm – 输出:(密文)
• (2)
– 输入:密文 – 输出:Thisisanewterm
思考题:矩阵换位密码算法的实现
• 把明文中的字母按给定的顺序安排在一矩阵中,然 后用另一种顺序选出矩阵的字母来产生密文。 • 例:明文ENGINEERING按行排在3 × 4矩阵中,最 后一行不全用ABC…填充。 • E N G I 给置换f=((1234)(2413)) NIEG • N E E R ERNE • I NG A NAIG • 得到密文:NIEGERNENAIG • 密钥为:m*n=3 * 4,以及f= ((1234)(2413)) • 即:k=(m * n,f)

C++实现古典密码学

C++实现古典密码学

实验一实现一个多表古典加密和解密程序实验目的:掌握多表古典加密方法。

实验要求:能用高级语言实现古典加密方法。

实验内容:多表古典加密方法主要有Playfair体制、Vigenere体制、Beaufor 体制、Vernam体制和Hill体制,用高级语言实现其中一种体制的加密和解密算法。

内容:Vigenere加密解密:#include <iostream>#include <string>using namespace std;const int N=26;charv[N][N]={{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}}; int number(char x)//把行号字母对应到数字{char y='a';for(int i=0;i<N;i++){if(x==(y+i)) return i;}}void encryption(string m,string k)//加密{cout<<"明文:";cin>>m;cout<<"密钥:";cin>>k;int mlen,klen;mlen=m.length();klen=k.length();char *p,*q,*t;//明文,初始密钥,密钥串。

C语言编程练习题之古典密码之旅

C语言编程练习题之古典密码之旅
H A P Y N E W R B C D F G I J K L M O Q S T U V X Z 按照这个表 明文:ILoveHuiwen 密文:BFjtnRsbuni
请编写一个程序,可以通过用户输入密码(一个字符串)生成密码表 (即上面的那个表),然后可以对用户输入的明文进行加密,对输入 的密文进行解密
第三题(Playfair密码)(续):
解密描述:Playfair解密算法首先将密钥填写在一个5*5的矩阵中(去出重复字母 和字母j),矩阵中其它未用到的字母按顺序填在矩阵剩余位置中,根据替换矩 阵由密文得到明文。 对密文解密规则如下: 1 若c1 c2在同一行,对应明文p1 p2分别是紧靠c1 c2 左端的字母。其中最后 一列被看做是第一列的左方。 2 若c1 c2在同一列,对应明文p1 p2分别是紧靠c1 c2 上方的字母。其中最后 一行被看做是第一行的上方。 3 若c1 c2不在同一行,不在同一列,则p1 p2是由c1 c2确定的矩形的其他两 角的字母。 其实就是反其道而行之。
d e f g h i j k l m n o p q r s t u v w x y z a b c
按照这个表 明文:ILoveHuiwen 密文:LOryhKxlzhq
请编写一个程序,可以通过用户输入密码(一个小于26的正整数)生 成密码表(即上面的那个表),然后可以对用户输入的明文进行加密, 对输入的密文进行解密
第三题(Playfair密码):
前面两题的密码表都是一维的,一维密码强度并不够,因此出现了二维密码。 Playfair密码是二维密码的一个典型例子。Playfair密码出现于1854年,它依据一 个5*5的正方形组成的密码表来编写,密码表里排列有25个字母。如果一种语言 字母超过25个,可以去掉使用频率最少的一个。如,法语一般去掉w或k,德语 则是把i和j合起来当成一个字母看待。英语中z使用最少,可以去掉它。 加密描述:第一步是编制密码表。在这个5*5的密码表中,共有5行5列字母。第 一列(或第一行)是密钥,其余按照字母顺序。密钥是一个单词或词组,若有重 复字母,可将后面重复的字母去掉。当然也要把使用频率最少的字母去掉。如: 密钥是Live and learn,去掉后则为liveandr。如果密钥过长可占用第二列或行。

信息安全 实验一 古典密码算法C语言

信息安全 实验一 古典密码算法C语言

信息安全实验报告课程名称: _ 专业:计算机科学与技术 _2010_级_02班实验编号:实验项目_ 指导教师_ _姓名:闫斌学号: 2010012854 实验成绩:___实验一古典密码算法实验名称:古典密码算法实验类型: 设计性实验学时:4适用对象: 信息安全1.实验原理古典密码算法历史上曾被广泛应用,大都比较简单,使用手工和机械操作来实现加密和解密。

它的主要应用对象是文字信息,利用密码算法实现文字信息的加密和解密。

下面介绍两种常见的具有代表性的古典密码算法,以帮助读者对密码算法建立一个初步的印象。

2.实验目的通过变成实现替代密码算法和置换密码算法,加深对古典密码体质的了解,为深入学习密码学奠定基础。

3.实验环境运行windows或linux操作系统的pc机,具有gcc(linux)、VC(Windows)等C语言编译环境。

4.实验内容4.1替代密码算法4.1.1根据实验远离部分对替代密码算法的介绍,创建明文信息,并选择一个密钥k,编写替代密码算法的实现程序,实现加密和解密操作。

替代密码包括多种类型,如单表替代密码、多明码替代密码、多字母替代密码、多表替代密码等。

4.1.2替代密码算法的远离是使用替代法进行加密,就是将明文的字符用其他字符替代后形成密文。

例如字母a、b、c、d,用D、E、F、G做对应替换后形成密文。

4.1.3 代码#include<stdio.h>#include<math.h>#include<string.h>#define N 500int main(){/*--------------------------------*/int i=0,k,m,n,l;char str1[N],str2[N]; /*C=M+K...K is key...*/clrscr();/*--------------------------------*/printf("This is a code password program......\n");printf("Please input proclaimed in writing(M)::\n");gets(str1);/*输入要加密的明文M*/printf("Please input the key(K)(int)::\n");scanf("%d",&k);/*输入密钥K*/m=strlen(str1);/*测试明文的长度*/printf("The M length is %d\n",m);printf("\n *\n *\n *\n***\n *\n");printf("ciphertext(C) is ::\n\n");for(i=0;i<m;i++)/*加密的过程*/{n=(int)str1[i];/*将字符转换成ASCII*/if(str1[i]==' ')/*如果字符串中出现空格返回空格*/{printf(" ");str2[i]=str1[i];}else if(n>96&&n<123)/*对小写进行加密*/{n=(n-97+k)%26;if(n<0)n=26+n;l=(char)(n+97);printf("%c",l);str2[i]=l;}else if(n>64&&n<91)/*对大写进行加密*/{n=(n-65+k)%26;if(n<0)n=26+n;l=(char)(n+97);printf("%c",l);str2[i]=l;}}str2[i]='\0';/*--------------------------------*/printf("\n\nThe C length is %d",strlen(str2));printf("\n\n *\n *\n *\n***\n *\n");printf("When the ciphertext is '%s',\nThe password program is...::\n\n",str2);m=strlen(str2);for(i=0;i<m;i++)/*解密过程*/{n=(int)str2[i];/*将字符转换成ASCII*/if(str2[i]==' ')/*如果是空格,返回的也是空格*/{printf(" ");}else if(n>96&&n<123)/*对小写进行解密*/{n=(n-97-k)%26;if(n<0)n=26+n;l=(char)(n+97);printf("%c",l);}else if(n>64&&n<91)/*对大写进行解密*/{n=(n-65-k)%26;if(n<0)n=26+n;l=(char)(n+97);printf("%c",l);}}str1[i]='\0';getch();return 0;}4.1.4 运行结果4.2 置换密码4.2.1根据实验原理部分对置换密码算法的介绍,自己创建明文信息,并选择一个密钥,编写置换密码算法的实现程序,实现加密和解密操作。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实训一古典加密算法一、实训目的熟悉古典数据加密的工作原理,并且能够利用相关加密算法进行加解密。

二、实训环境一台安装有Windows XP的计算机。

三、实训内容用C编写程序实现对某一文件内容用恺撒加密法进行加密,然后用解密程序进行解密。

四、实训步骤1、用C编写程序实现对某一文件内容用恺撒加密法进行加密。

2、用解密程序对密文进行解密。

五、实训效果检测上交加密程序、解密程序、原文(.txt文档)解:(1)使用的加密、解密程序代码如下:#include<stdio.h>#include<conio.h>#include<string.h>#include<malloc.h>char encrypt(char ch,int n)/*加密函数,把字符向右循环移位n*/{while(ch>='A'&&ch<='Z'){return ('A'+(ch-'A'+n)%26);}while(ch>='a'&&ch<='z'){return ('a'+(ch-'a'+n)%26);}return ch;}void menu()/*菜单,1.加密,2.解密,3.暴力破解,密码只能是数字*/{//clrscr();printf("\n========================================================="); printf("\n1.Encrypt the file");printf("\n2.Decrypt the file");printf("\n3.Force decrypt file");printf("\n4.Quit\n");printf("=========================================================\n"); printf("Please select a item:");return;}void main(){int i,n;char ch0,ch1;FILE *in,*out;char infile[20],outfile[20];//textbackground(BLACK);//textcolor(LIGHTGREEN);//clrscr();menu();ch0=getch();while(ch0!='4'){if(ch0=='1'){// clrscr();printf("\nPlease input the infile:");scanf("%s",infile);/*输入需要加密的文件名*/if((in=fopen(infile,"r"))==NULL){printf("Can not open the infile!\n");printf("Press any key to exit!\n");getch();}printf("Please input the key:");scanf("%d",&n);/*输入加密密码*/printf("Please input the outfile:");scanf("%s",outfile);/*输入加密后文件的文件名*/if((out=fopen(outfile,"w"))==NULL){printf("Can not open the outfile!\n");printf("Press any key to exit!\n");fclose(in);getch();}while(!feof(in))/*加密*/{fputc(encrypt(fgetc(in),n),out);}printf("\nEncrypt is over!\n");fclose(in);fclose(out);// sleep(1);}if(ch0=='2'){// clrscr();printf("\nPlease input the infile:"); scanf("%s",infile);/*输入需要解密的文件名*/if((in=fopen(infile,"r"))==NULL) {printf("Can not open the infile!\n");printf("Press any key to exit!\n");getch();// exit(0);}printf("Please input the key:");scanf("%d",&n);/*输入解密密码(可以为加密时候的密码)*/ n=26-n;printf("Please input the outfile:");scanf("%s",outfile);/*输入解密后文件的文件名*/if((out=fopen(outfile,"w"))==NULL){printf("Can not open the outfile!\n");printf("Press any key to exit!\n");fclose(in);getch();// exit(0);}while(!feof(in)){fputc(encrypt(fgetc(in),n),out);}printf("\nDecrypt is over!\n");fclose(in);fclose(out);// sleep(1);}if(ch0=='3'){// clrscr();printf("\nPlease input the infile:");scanf("%s",infile);/*输入需要解密的文件名*/if((in=fopen(infile,"r"))==NULL){printf("Can not open the infile!\n");printf("Press any key to exit!\n");getch();// exit(0);}printf("Please input the outfile:");scanf("%s",outfile);/*输入解密后文件的文件名*/if((out=fopen(outfile,"w"))==NULL){printf("Can not open the outfile!\n");printf("Press any key to exit!\n");fclose(in);getch();// exit(0);}for(i=1;i<=25;i++)/*暴力破解过程,在察看信息正确后,可以按'Q'或者'q'退出*/ {rewind(in);rewind(out);// clrscr();printf("==========================================================\n"); printf("The outfile is:\n");printf("==========================================================\n"); while(!feof(in)){ch1=encrypt(fgetc(in),26-i);putch(ch1);fputc(ch1,out);}printf("\n========================================================\n");printf("The current key is: %d \n",i);/*显示当前破解所用密码*/printf("Press 'Q' to quit and other key to continue......\n");printf("==========================================================\n");ch1=getch();if(ch1=='q'||ch1=='Q')/*按'Q'或者'q'时退出*/{// clrscr();printf("\nGood Bye!\n");fclose(in);fclose(out);// sleep(3);// exit(0);}}printf("\nForce decrypt is over!\n");fclose(in);fclose(out);// sleep(1);}menu();ch0=getch();}//clrscr();printf("\nGood Bye!\n");(2)原文文件为whx.txt,其内容:ahhdhjshjdhjsdhj。

相关文档
最新文档