欢迎来到天天文库
浏览记录
ID:22282392
大小:82.18 KB
页数:9页
时间:2018-10-28
《实验一古典密码-vigernere算法》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、实验一古典密码-Vigenere算法姓名:班级:学号:一、实验目的1、理解简单加密算法的原理;2、掌握Vigenere密码的原理,完成Vigenere密码加解密程序的编写;3、通过实验,加深对古典密码体制的了解,掌握对字符进行灵活处理的方法。二,实验要求根据Vigenere密码的原理编写程序,对输入的符号串能够根据设置的密钥分別正确实现Vigenere加密和解密功能。三,实验过程及内容源程序#include"stdio.h”#include"string.h"#defineBUF_MAX100staticintsquare[27][27];//密钥统一转换力大写字母voidUpp
2、erKey(charkey[]){intkey_length=strlen(key);for(inti=0;i='a'&&key[i]<='z'){key[i]-=32;}}}//Vigenere加密voidVigenereEncrypt(charword门,charkey门){chartext[BUF_MAX];intkey_length=strlen(key);intwordjength=strlen(word);inti,j,k;for(i=0;i3、dlij>='a'&&wordlij<=’z’){k=wordlij-'a';k=(k+key[j]-.A.)%26;text[i]=k+'a';}elseif(wordlij>='A*&&wordlij<='Z'){k=wordlij-'A';k=(k+keyljj-'A')%26;text[i]=k+’A';}else{text[i]=word[i];}}text[i]=W;printf("Encryptstring:");puts(text);}//Vigenere解密voidVigenereDecript(charen_word[],charkey门){charword[4、BUF_MAX];intkey_length=strlen(key);intwordjength=strlen(en一word);inti,j,k;for(i=0;i='a'&&en_word[i]<=’z’)k=enlworduj--a-;1<=(k-(keyuj->•)+26)%26;words=k+-a-;}elseif(enlwordsv=->enlword【一】<=N){k=enlworduj->•;=(k-(keyE>)+26)%26;words=k+->;}else{wor5、dE=slwordlij;}}wo3.【i】ll -;Printfc-Decryptstring::);puts(word);}intmain(void){charkey【BUFIMAXLch;charstr【BUFIMAXl={-Q};intij;//^Bnsvigenere^®for(i=1;i<27;T+){fora=l;j<27;j++){square【i】E=2+i+j;if(square【i】s>90){squaressHsquare【islI26;printf("VigenereCode");printf(•’l:Encryption");printf(6、"2:Decryption");printf(.’Pleaseinputyourchoose:"for(i=0;;i++){scanf(H%du,&j);if0=1){VigenereEncrypt(str,key);break;}elseif(j==2){VigenereDecript(str,key);break;}else{printf("输入错误,重新输入:n);fflush(stdin);printf("Pleaseinputastring:");i=0;do{scanf("%c",&ch);str[i++]=ch;}while(ch!=V&&i7、AX);printf("Pleaseinputthekey:’’);scanf("%s",key);UpperKey(key);return0;程序运行结果:Vigenere加密Xj^JigenereCode:Encryptionyourchoose:1astring:hellowordthekey:english2:DecryptionPleaseinputPleaseinputPleaseinputEncryptstring:IrrwwdsejProcessexitedwit
3、dlij>='a'&&wordlij<=’z’){k=wordlij-'a';k=(k+key[j]-.A.)%26;text[i]=k+'a';}elseif(wordlij>='A*&&wordlij<='Z'){k=wordlij-'A';k=(k+keyljj-'A')%26;text[i]=k+’A';}else{text[i]=word[i];}}text[i]=W;printf("Encryptstring:");puts(text);}//Vigenere解密voidVigenereDecript(charen_word[],charkey门){charword[
4、BUF_MAX];intkey_length=strlen(key);intwordjength=strlen(en一word);inti,j,k;for(i=0;i='a'&&en_word[i]<=’z’)k=enlworduj--a-;1<=(k-(keyuj->•)+26)%26;words=k+-a-;}elseif(enlwordsv=->enlword【一】<=N){k=enlworduj->•;=(k-(keyE>)+26)%26;words=k+->;}else{wor
5、dE=slwordlij;}}wo3.【i】ll -;Printfc-Decryptstring::);puts(word);}intmain(void){charkey【BUFIMAXLch;charstr【BUFIMAXl={-Q};intij;//^Bnsvigenere^®for(i=1;i<27;T+){fora=l;j<27;j++){square【i】E=2+i+j;if(square【i】s>90){squaressHsquare【islI26;printf("VigenereCode");printf(•’l:Encryption");printf(
6、"2:Decryption");printf(.’Pleaseinputyourchoose:"for(i=0;;i++){scanf(H%du,&j);if0=1){VigenereEncrypt(str,key);break;}elseif(j==2){VigenereDecript(str,key);break;}else{printf("输入错误,重新输入:n);fflush(stdin);printf("Pleaseinputastring:");i=0;do{scanf("%c",&ch);str[i++]=ch;}while(ch!=V&&i7、AX);printf("Pleaseinputthekey:’’);scanf("%s",key);UpperKey(key);return0;程序运行结果:Vigenere加密Xj^JigenereCode:Encryptionyourchoose:1astring:hellowordthekey:english2:DecryptionPleaseinputPleaseinputPleaseinputEncryptstring:IrrwwdsejProcessexitedwit
7、AX);printf("Pleaseinputthekey:’’);scanf("%s",key);UpperKey(key);return0;程序运行结果:Vigenere加密Xj^JigenereCode:Encryptionyourchoose:1astring:hellowordthekey:english2:DecryptionPleaseinputPleaseinputPleaseinputEncryptstring:IrrwwdsejProcessexitedwit
此文档下载收益归作者所有