维吉尼亚密码(Vigenere)加密与解密源代码
- 格式:doc
- 大小:25.00 KB
- 文档页数:2
Vigenere 加解密程序制作一、Vigenere密码简介维吉尼亚密码引入了“密钥”的概念,即根据密钥来决定用哪一行的密表来进行替换,以此来对抗字频统计。
实际上,Vigenere密码的代替规则是用明文字母在Vigenere方阵中的列,和密钥字母在Vigenere方阵中的行的交点处的字母来代替该明文字母。
vigenere方阵为:a abcdefghijklmnopqrstuvwxy zb bcdefghijklmnopqrstuvwxyz ac cdefghijklmnopqrstuvwxyz a bd defghijklmnopqrstuvwxyz a b ce efghijklmnopqrstuvwxyz a b c d………z z 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对如下明文加密:TO BE OR NOT TO BE THAT IS THE QUESTION当选定RELATIONS作为密钥时,加密过程是:明文一个字母为T,第一个密钥字母为R,因此可以找到在R行中代替T的为K,依此类推,得出对应关系如下:密钥:RELAT IONSR ELATI ONSRE LATIO NSREL明文:TOBEO RNOTT OBETH ATIST HEQUE STION密文:KSMEH ZBBLK SMEMP OGAJX SEJCS FLZSY解密时利用Vigenere方阵进行反替换!二、程序制作说明本程序通过控制台的方式来实现对用户输入的明文(密文)的加密(解密),在对明文(密文)的加密(解密)时所用的密钥必需保证是一致的。
程序界面为:1、Vigenere方阵的形成Vigenere方阵的形成是在程序的OnInitDialog()函数中调用Vigenre_Array()函数来实现的。
把Vigenere方阵的字母转换为十进制数据进行观察分析可以看出,最大的数是90,并且从左到右是递增的,直到遇到90为止,然后再从数字65开始。
实验二VIGENERE 密码破译实验目的:熟悉vigenere密码加密的密文实验内容及要求:秘钥长度:3~50;明文为普通英文文章(仅含小写字母)实验过程及代码Vignere:'ktbueluegvitnthuexmonveggmrcgxptlyhhjaogchoemq chpdnetxupbqntietiabpsmaoncnwvoutiugtagmmqsxtvxaoniiogta gmbpsmtuvvihpstpdvcrxhokvhxotawswquunewcgxptlcrxtevtubve wcnwwsxfsnptswtagakvoyyak'第一步:位移法求解key的长度这段密文约有200个字母,先假设key<=20位移20次,每次位移后计算与原密文对应相同的字母个数,即s[i]==s[i+j],i为位置,j为位移量cipher=”ktbueluegvitnthuexmonveggmrcgxptlyhhjaogchoemqchpd netxupbqntietiabpsmaoncnwvoutiugtagmmqsxtvxaoniiogtagmbpsmtuvvihpstpdvcrxhokvhxotawswquunewcgxptlcrxtevtubvewcnwwsxfsn ptswtagakvoyyak'deffind_key_length(cipher):'''''shift and fing the most repeats of the cipher''' MAX_LEN_REP = 10shift = [ None ]length = len(cipher)fori in range(1, length//MAX_LEN_REP):repeat = 0for j in range(length):repeat += 1 if cipher[j]==cipher[(j+i)%length] else 0 shift.append(repeat)fori in range(1, length//MAX_LEN_REP):print('{0:2} '.format(i), end='')print()fori in range(1, length//MAX_LEN_REP):print('{0:2} '.format(shift[i]), end='')print()find_key_length(cipher)运行结果如下:从结果可以看出,3 6 8 12 15 重复字母数相对偏高,这些字母的公因子约为3,于是说密钥长度为3的可能性非常大第二步:获取key长度之后,将文本分为key长度份,即s[0],s[3],s[6]……为一份,s[1],s[4],s[7]……为第二份,剩下的为第三份接下来将每份进行频率分析(Frequency Analysis) deffrequency_analysis(cipher, keyLen, k):'''''find the kth part alpha frequency, k in range(keyLen)'''length = len(cipher)keyDict = {}for alpha in range(ord('a'), ord('a')+26):keyDict[chr(alpha)] = 0fori in range(length):keyDict[cipher[i]] += 1 if i % keyLen == k else 0for alpha in range(ord('a'), ord('a')+26):print(' {0} '.format(chr(alpha)), end='')print()for alpha in range(ord('a'), ord('a')+26):print('{0:2} '.format(keyDict[chr(alpha)]), end='') print()frequency_analysis(cipher, 3, 0) #0, 1, 2程序第一部分结果:可以看出 p8, g7, v7, c6 频率较高,而普通英语文本中 e, t, a, i, n, o频率也较高而一份维吉尼亚密码相当于一个凯撒密码,即位移相同字符数那么可以断定明文 -> 密文即为 e ->g, t ->v, a -> c, n->p 所以key的第一个字符为c接下来就是第二第三部分:程序稍作修改frequency_analysis(cipher, 3, 1)frequency_analysis(cipher, 3, 2)运行结果如下:具体判断过程与上面一样,key的第二的字母为a,第三个为t则key为cat既然知道了密钥,那就把明文破译出来吧key = 'cat'defdecoding_vigenere(cipher, key):'''''decoding vigenere cipher with key'''defgetKeyDict(alpha):shift = ord(alpha) - ord('a')keyDict = {}fori in range(ord('a'), ord('a')+26):keyDict[chr(i)] = chr(i + shift) if i + shift <= ord('z') else chr(i + shift -26)#exchang the key and valuekeyDict = dict(map(lambda t:(t[1], t[0]), keyDict.items()))returnkeyDictkeyLen = len(key)cipLen = len(cipher)keyList = [getKeyDict(key[i]) for i in range(keyLen)] plainText = ''fori in range(cipLen):plainText += keyList[i % keyLen][cipher[i]]print(plainText)decoding_vigenere(cipher, key)开始的时候把key算出来直接用了,程序运行的结果就是把密文又加密了一遍在程序中稍作改动,把每个位移的字典key反转(交换键和值),程序运行结果是:看看到底是什么it is essential to seek out enemy agents who have come to conduct espionage against you and to bribe them to serve you give them instructions and care for them thus doubled agents are recruited and used sun tzut heart of war。
VigenereCipher密码解码Vigenere Cipher解密⼀、实验任务Vigenere Cipher加密密⽂如下:KVNIICWZSNOHUNXQGMRNAHUVWPTBWBPKVYPTBASHRZVIIWIBCGJSGEYGFMOAKBTBJKMAQ EGSQJIJHTRGJSFRXTNBVFVCORWGENSIKRRAXVAIHNNZZRRCKMQEGOGRHGJCOYKVNIICWZ SNOHUNXQGMRNACAGLGKMRUKZYFSHZMCEIWNGLGLWBFQTSBVOXZGYCJRFEPWBVRUCAFSHY WFZGFFYEXXWKAGFFJMNEJSNDZRGSUBBRBYBGBKGMPSECHGUIVTJZRQTOESVAMFUQCQVLC OMOQTSNZXJTBCAGRNLIXXVHUGGGNXGHNAVUGVFWKIXWNUHNGIUPMZGGFVAKYBBVGJSURE VHNWALIFGMEXAKRNHREMPZEWGJHURLGTBCSQDCEIULQCAYWYYFGMZOAUTBEQGWQBGQOAB EUBACSHFRRHQFIBQLIFGMEX请写出详细解密过程。
⼆、Vigenere加密与解密Vigenere加密为单表代换密码,其加密过程可表述为:设m是⼀个正整数,定义P=C=K=(Z26)m,对任意的密钥K=(k1,k2,…,k m),定义e K x1,x2,…,x m=(x1+k1,x2+k2,…,x m+k m)和d K y1,y2,…,y m=(y1?k1,y2?k2,…,y m?k m)为Vigenere加密和解密过程,以上所有运算都在(Z26)m 上进⾏。
三、Vigenere密⽂的破译Vigenere解密⾸先在于确定密钥长度m,意⼤利的乔⽡·尼·波塔于1602年⾸先发现Vigenere明/密⽂中重复字母串的距离正好是密钥长度的倍数,这⼀现象称为“合拍”现象。
维吉尼亚(Vigenere)密码算法(Javascript实现加密与解密) 传统加密技术对于当今的⽹络安全发挥不了⼤作⽤,但每⼀本讲述密码学的书的开头都会率先介绍它们,因为它们是密码学的基础,是密码学的历史。
Vigenere密码就是⼀种传统加密技术,它是多表代换密码,能够有效改进单表代换密码的词频分布特征问题。
详细介绍请参考密码学相关书籍。
⼏乎每⼀本密码学的书在讲述Vigenere密码的章节都会有这么⼀个《Vigenere代换表》⽤户讲解Vigenere密码机制: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 ZB C D E F G H I J K L M N O P Q R S T U V W X Y Z AC D E F G H I J K L M N O P Q R S T U V W X Y Z A BD E F G H I J K L M N O P Q R S T U V W X Y Z A B CE F G H I J K L M N O P Q R S T U V W X Y Z A B C DF G H I J K L M N O P Q R S T U V W X Y Z A B C D EG H I J K L M N O P Q R S T U V W X Y Z A B C D E FH I J K L M N O P Q R S T U V W X Y Z A B C D E F GI J K L M N O P Q R S T U V W X Y Z A B C D E F G HJ K L M N O P Q R S T U V W X Y Z A B C D E F G H IK L M N O P Q R S T U V W X Y Z A B C D E F G H I JL M N O P Q R S T U V W X Y Z A B C D E F G H I J KM N O P Q R S T U V W X Y Z A B C D E F G H I J K LN O P Q R S T U V W X Y Z A B C D E F G H I J K L MO P Q R S T U V W X Y Z A B C D E F G H I J K L M NP Q R S T U V W X Y Z A B C D E F G H I J K L M N OQ R S T U V W X Y Z A B C D E F G H I J K L M N O PR S T U V W X Y Z A B C D E F G H I J K L M N O P QS T U V W X Y Z A B C D E F G H I J K L M N O P Q RT U V W X Y Z A B C D E F G H I J K L M N O P Q R SU V W X Y Z A B C D E F G H I J K L M N O P Q R S TV W X Y Z A B C D E F G H I J K L M N O P Q R S T UW X Y Z A B C D E F G H I J K L M N O P Q R S T U VX Y Z A B C D E F G H I J K L M N O P Q R S T U V WY Z A B C D E F G H I J K L M N O P Q R S T U V W XZ 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 加密过程很简单,就是给定密钥字母x和明⽂字母y,密⽂字母是位于x⾏和y列的那个字母。
维吉尼亚密码算法// 实验:维吉尼亚密码算法// 姓名: JJCC X// 学号://#include<iostream>using namespace std;#define MINCHAR 32#define CHARSUM 94char table[CHARSUM][CHARSUM];bool Init();bool Encode(char* key, char* source, char* dest);bool Dncode(char* key, char* source, char* dest);int main(){if(!Init()){cout << "初始化错误!" << endl;return 1;}char key[256];char str1[256];char str2[256];int operation;while(1){do{cout << "请选择⼀个操作:1. 加密; 2. 解密; -1. 退出\n";cin >> operation;}while(operation != -1 && operation != 1 && operation != 2);if(operation == -1)return 0;else if(operation == 1)//加密{cout << "请输⼊密钥:";cin >> key;cout << "请输⼊待加密字符串:";cin >> str1;Encode(key, str1, str2);cout << "加密后的字符串:" << str2 << endl;}else if(operation == 2)//解密{cout << "请输⼊密钥:";cin >> key;cout << "请输⼊待解密字符串:";cin >> str1;Dncode(key, str1, str2);cout << "解密后的字符串:" << str2 << endl;}cout << endl;}return 0;}// 初始化维吉尼亚⽅阵bool Init(){int i, j;for(i = 0; i < CHARSUM; i++){for(j = 0; j < CHARSUM; j++){table[i][j] = MINCHAR + (i + j) % CHARSUM;}}return true;}// 加密// key:密钥// source:待加密的字符串// dest:经过加密后的字符串bool Encode(char* key, char* source, char* dest){char* tempSource = source;char* tempKey = key;char* tempDest = dest;do{*tempDest = table[(*tempKey) - MINCHAR][(*tempSource) - MINCHAR]; tempDest++;if(!(*(++tempKey)))tempKey = key;}while(*tempSource++);dest[strlen(source)] = 0;return true;}// 解密// key:密钥// source:待解密的字符串// dest:经过解密后的字符串bool Dncode(char* key, char* source, char* dest){char* tempSource = source;char* tempKey = key;char* tempDest = dest;char offset;do{offset = (*tempSource) - (*tempKey);offset = offset >= 0 ? offset : offset + CHARSUM;*tempDest = MINCHAR + offset;tempDest++;if(!(*(++tempKey)))tempKey = key;}while(*++tempSource);dest[strlen(source)] = 0;return true;}。
实验一维吉尼亚一、实验目的通过实验掌握维吉尼亚算法,对维吉尼亚加深认识。
二、实验要求用VC 编程实现,密钥是自己名字的全拼,明文是MINGMAXUEKECHENG,通过程序实现求出密文。
三、实验环境安装Windows操作系统的PC机,具有C语言编译环境。
四、实验内容vigenere方阵明文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 za abcdefghijklmnopqrstuvwxy zb bcdefghijklmnopqrstuvwxyz ac cdefghijklmnopqrstuvwxyz a bd defghijklmnopqrstuvwxyz a b ce efghijklmnopqrstuvwxyz a b c d………z z 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源代码:#include "iostream"#include "string"using namespace std;int createVigenere(char vigenere[][26]){for(int i=0;i<26;i++){if(i>0)vigenere[0][i] = vigenere[0][i-1] + 1;elsevigenere[0][i] = 'A';for(int j=1;j<26;j++){if(vigenere[j-1][i]<'Z')vigenere[j][i] = vigenere[j-1][i] +1;elsevigenere[j][i] = vigenere[j-1][i] - 25;}}return 1;}void encrepty(char vigenere[][26],char* source,char* key,char *cipher) {source = strlwr(source);//将大写转换为小写char *p = strlwr(key);char *q = source;char *s = cipher;while(*q != '\0'){while(*p !='\0'&&*q != '\0'){if(*q<'a'||*q>'z')//如果要加密的字符中不是26个字母则将其保存,不为其加密{*s = *q;q++;s++;continue;}*s = vigenere[*q-97][*p-97];//密钥*p-97,密文*q-97s++;p++;q++;}p = key;}*s = '\0';}void deciphring(char vigenere[][26],char* cipher,char* key,char *source) {cipher = strlwr(cipher);char *p = strlwr(key);char *q = cipher;char *s = source;while(*q != '\0'){while(*p !='\0'&&*q!='\0')//如果要加密的字符中不是26个字母则将其保存,不为其解密{if(*q<'a'||*q>'z'){*s = *q;q++;s++;continue;}*s = vigenere[0][(*q-*p+26)%26]+32;s++;p++;q++;}p = key;}*s = '\0';}void main(){char source[1024]={'\0'};//明文char cipher[1024]={'\0'};//密文char key[26] = "lijaixi";//密匙int index[1024] = {0};//标识明文的大小写char vigenere[26][26]={0};//vigenere加密矩阵createVigenere(vigenere);//创建加密矩阵/* for(i=0;i<26;i++)//输出加密矩阵{for(int j=0;j<26;j++){cout<<vigenere[i][j]<<" ";}cout<<endl;}*/cout<<"请输入要加密的字符串:"<<endl;gets(source);char *p = source;int i = 0;while(*p != '\0')//确定明文中大写字母的位置{if(*p>='A'&&*p<='Z'){index[i++] = 1;//如果输入的字母为大写则对应位置保存为1否则为0}else{index[i++] = 0;}p++;}encrepty(vigenere,source,key,cipher);//加密cout<<"加密结果为:";cout<<cipher<<endl;char yy[1024];cout<<"解密结果为:";deciphring(vigenere,cipher,key,yy);//解密p = yy;//加密转换后的结果却是大写i = 0;while(*p != '\0'){if(index[i++] == 1)//如果原来是小写则需要将转换都的大写转换为小写{*p = *p-32;}p++;}cout<<yy<<endl;}运行结果如下图:五、心得体会Vigenere密码比较简单,通过实验更使我明白了密码的加密和加密过程,对它的程序实现方法有了更为深刻的了解。
维吉尼亚密码c语言源代码#include<stdio.h> // 头文件<stdio.h> , 声明标准输入输出函数#include<string.h> // 头文件<string.h> ,声明字符串操作函数char vigenere_table[26][26]; //定义维吉尼亚表void set_vigenere_table(){int i,j;int key; //行指针for (i = 0; i < 26; i++){key=i;for(j=0;j<26;j++){vigenere_table[i][j]='A'+(key%26);key++;}}printf("初始化维吉尼亚表完毕:\n\n");}void vigenere_encrypt(char* source,char* des) //定义加密函数{int source_len=strlen(source);int key_len=strlen(des);int i;for(i=0;i<source_len;i++) //对每个字母进行加密{int source_index= source[i]-'A'; //密文指针int key_index=des[i%key_len]-'A'; //维吉尼亚表指针int des_index=vigenere_table[key_index][source_index]-'A';des[i]=des_index+'A';}des[source_len]='\0';}void vigenere_decrypt(char* source,char* des) //定义解密函数{#define MAX_LEN 26int source_len=strlen(source);int key_len=strlen(des);int i;for(i=0;i<source_len;i++) //对每个字母进行解密{int key_index=des[i%key_len]-'A'; //维吉尼亚表指针int j;for(j=0;j<MAX_LEN;j++) //从维吉尼亚表中查找密文指针{if(vigenere_table[key_index][j]==source[i]){break;}}des[i]=j+'A';}des[source_len]='\0';}int main(){char plain[]="VIGENERECIPHER";char key[]="ABCD";char result[20];char result1[20];set_vigenere_table();vigenere_encrypt(plain,result);printf("明文: %s\n密钥: %s\n密文: %s\n",plain,key,result);vigenere_decrypt(result,result1);printf("解密后: %s\n",result1);return 0;}。
实验报告表格例实验1(Vigenere算法)的源代码如下:#include<iostream>using namespace std;int tt(){chartable[27]={'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 w,k,q;w=k=q=0;char miyao[10];//={'n','i','c','e','d','a','y'};cout<<"请输入密钥,且密钥的个数小于10个"<<endl;//for(int i=0;i<9;i++)//{cin>>miyao;//w++;char minwen[20],miwen[20];cout<<"请输入需要加密的明文,且明文的长度一次不能超过19个字母"<<endl;//for(int j=0;j<19;j++)cout<<"明文:";cin>>minwen;cout<<"输入的明文加密后的密文如下"<<endl;for(int p=0;minwen[p]!='\0';p++){if(miyao[q]=='\0'){q=0; k=((minwen[p]-97)+(miyao[q]-97))%26;//miwen[p]=table[k];cout<<table[k];}else{k=((minwen[p]-97)+(miyao[q]-97))%26;//miwen[p]=table[k];cout<<table[k];}q++;}cout<<endl;//加密完成,下面是解密的过程;cout<<"如果想解密请输入加密后的密文"<<endl; cout<<"密文:";cin>>miwen;cout<<"解密后的明文如下"<<endl;int t=0;for(int i=0;miwen[i]!='\0';i++){if(miyao[t]=='\0'){t=0;k=(((miwen[i]-97)+26)-(miyao[t]-97))%26;cout<<table[k];}else{k=(((miwen[i]-97)+26)-(miyao[t]-97))%26;cout<<table[k];}t++;}cout<<endl;//cout<<miwen<<endl;//cout<<minwen<<endl;return 0;}int main(){ tt();//char a[8];//for(int i=0;i<7;i++)//cin>>a;//for(int j=0;a[j]!='\0';j++)//cout<<a[j]<<endl;return 0;}实验过程如图所示:实验体会:本次实验是对Vigenere算法的设计,Vigenere算法的原理就是通过26个英文字母之间在密钥的限定下进行转换,而完成的加密。
C语言Vigenère密码的综合题一、概述Vigenère密码是一种多表密码,是由法国人布拉瓦·德维热纳于1553年发明的。
它是基于字母表的轮换和偏移来实现加密与解密的。
Vigenère密码算法的核心思想是使用一个密钥串来进行加密和解密操作,相比传统的凯撒密码,Vigenère密码更加安全可靠。
在C语言中实现Vigenère密码算法可以帮助我们更好地了解密码学和算法设计的相关知识。
二、Vigenère密码的原理Vigenère密码算法的核心原理是利用关键词对明文进行分组加密,具体步骤如下:1.确定关键词,将明文和关键词转换为阿拉伯数字表达方式。
2.将关键词复制至与明文长度相等的长度。
3.按照两者的数值进行加法计算。
4.将计算结果对26取模。
5.将计算结果转换为字符形式。
6.将加密后的字符拼接成密文。
三、Vigenère密码的具体实现在C语言中实现Vigenère密码算法需要考虑以下几个关键步骤:1. 输入明文和关键词。
2. 将明文和关键词转换为阿拉伯数字表达方式。
3. 判断明文和关键词的长度以便确定循环加密。
4. 进行加密计算。
5. 输出密文。
四、示例代码下面是一个简单的C语言示例代码,实现了Vigenère密码的加密操作:```c#include <stdio.h>#include <string.h>void encrypt(char* pl本人n, char* key) {int plen = strlen(pl本人n);int klen = strlen(key);for (int i = 0; i < plen; i++) {pl本人n[i] = ((pl本人n[i] - 'A' + key[i klen] - 'A') 26) + 'A'; }}int m本人n() {char pl本人n[100];char key[100];printf("请输入明文:");scanf("s", pl本人n);printf("请输入关键词:");scanf("s", key);encrypt(pl本人n, key);printf("加密后的密文为:s\n", pl本人n);return 0;}```五、总结Vigenère密码算法是一种经典的密码算法,通过使用关键词对明文进行分组加密,可以有效提高密码的安全性。
java实现维吉尼亚加密解密算法加密算法程序:public class mtoc{//输⼊明⽂和密钥,⽤输⼊的密钥对明⽂进⾏加密public static void main(String[] args){int i;char[] c=new char[100];char[] k1=new char[100];//输⼊System.out.print("enter a mingwen string:");String m=MyInput.readString();System.out.print("enter a key string:");String k=MyInput.readString();//构造密钥对照表for(i=0;i<k.length();i++){if(k.charAt(i)>='a'&&k.charAt(i)<='z')k1[i]=(char)(k.charAt(i)-97);if(k.charAt(i)>='A'&&k.charAt(i)<='Z')k1[i]=(char)(k.charAt(i)-65);}//加密for(i=0;i<m.length();i++){if(m.charAt(i)>='a'&&m.charAt(i)<='z')c[i]=(char)((m.charAt(i)-97+k1[i%k.length()])%26+97);if(m.charAt(i)>='A'&&m.charAt(i)<='Z')c[i]=(char)((m.charAt(i)-65+k1[i%k.length()])%26+65);}//输出密⽂for(i=0;i<c.length;i++)System.out.print(c[i]);}}解密算法程序:public class ctom{//输⼊密⽂和密钥,⽤密钥对密⽂解密public static void main(String[] args){int i;char[] m=new char[100];char[] k1=new char[100];//输⼊System.out.print("enter the miwen string:");String c=MyInput.readString();System.out.print("enter the key string:");String k=MyInput.readString();//构造密钥对照表for(i=0;i<k.length();i++){if(k.charAt(i)>='a'&&k.charAt(i)<='z')k1[i]=(char)(k.charAt(i)-97);if(k.charAt(i)>='A'&&k.charAt(i)<='Z')k1[i]=(char)(k.charAt(i)-65);}//解密for(i=0;i<c.length();i++){if(c.charAt(i)>='a'&&c.charAt(i)<='z')m[i]=(char)((c.charAt(i)-97-k1[i%k.length()]+26)%26+97);if(c.charAt(i)>='A'&&c.charAt(i)<='Z')m[i]=(char)((c.charAt(i)-65-k1[i%k.length()]+26)%26+65);}//输出明⽂for(i=0;i<m.length;i++)System.out.print(m[i]);}}需要⽤到的MyInput.java类:// MyInput.java: Contain the methods for reading int, double, and// string values from the keyboardimport java.io.*;public class MyInput{// Read a string from the keyboardpublic static String readString(){BufferedReader br= new BufferedReader(new InputStreamReader(System.in), 1);// Declare and initialize the stringString string = "";// Get the string from the keyboardtry{string = br.readLine();}catch (IOException ex){System.out.println(ex);}// Return the string obtained from the keyboardreturn string;}// Read an int value from the keyboardpublic static int readInt(){return Integer.parseInt(readString());}// Read a double value from the keyboardpublic static double readDouble(){return Double.parseDouble(readString());}// Read a byte value from the keyboardpublic static byte readByte(){return Byte.parseByte(readString());}// Read a short value from the keyboardpublic static short readShort(){return Short.parseShort(readString());}// Read a long value from the keyboardpublic static long readLong(){return Long.parseLong(readString());}// Read a float value from the keyboardpublic static float readFloat(){return Float.parseFloat(readString());}}加密算法程序:public class mtoc{//输⼊明⽂和密钥,⽤输⼊的密钥对明⽂进⾏加密public static void main(String[] args){int i;char[] c=new char[100];char[] k1=new char[100];//输⼊System.out.print("enter a mingwen string:");String m=MyInput.readString();System.out.print("enter a key string:");String k=MyInput.readString();//构造密钥对照表for(i=0;i<k.length();i++){if(k.charAt(i)>='a'&&k.charAt(i)<='z')k1[i]=(char)(k.charAt(i)-97);if(k.charAt(i)>='A'&&k.charAt(i)<='Z')k1[i]=(char)(k.charAt(i)-65);}//加密for(i=0;i<m.length();i++){if(m.charAt(i)>='a'&&m.charAt(i)<='z')c[i]=(char)((m.charAt(i)-97+k1[i%k.length()])%26+97);if(m.charAt(i)>='A'&&m.charAt(i)<='Z')c[i]=(char)((m.charAt(i)-65+k1[i%k.length()])%26+65);}//输出密⽂for(i=0;i<c.length;i++)System.out.print(c[i]);}}解密算法程序:public class ctom{//输⼊密⽂和密钥,⽤密钥对密⽂解密public static void main(String[] args){int i;char[] m=new char[100];char[] k1=new char[100];//输⼊System.out.print("enter the miwen string:");String c=MyInput.readString();System.out.print("enter the key string:");String k=MyInput.readString();//构造密钥对照表for(i=0;i<k.length();i++){if(k.charAt(i)>='a'&&k.charAt(i)<='z')k1[i]=(char)(k.charAt(i)-97);if(k.charAt(i)>='A'&&k.charAt(i)<='Z')k1[i]=(char)(k.charAt(i)-65);}//解密for(i=0;i<c.length();i++){if(c.charAt(i)>='a'&&c.charAt(i)<='z')m[i]=(char)((c.charAt(i)-97-k1[i%k.length()]+26)%26+97);if(c.charAt(i)>='A'&&c.charAt(i)<='Z')m[i]=(char)((c.charAt(i)-65-k1[i%k.length()]+26)%26+65);}//输出明⽂for(i=0;i<m.length;i++)System.out.print(m[i]);}}需要⽤到的MyInput.java类:// MyInput.java: Contain the methods for reading int, double, and // string values from the keyboardimport java.io.*;public class MyInput{// Read a string from the keyboardpublic static String readString(){BufferedReader br= new BufferedReader(new InputStreamReader(System.in), 1);// Declare and initialize the stringString string = "";// Get the string from the keyboardtry{string = br.readLine();}catch (IOException ex){System.out.println(ex);}// Return the string obtained from the keyboardreturn string;}// Read an int value from the keyboardpublic static int readInt(){return Integer.parseInt(readString());}// Read a double value from the keyboardpublic static double readDouble(){return Double.parseDouble(readString()); }// Read a byte value from the keyboard public static byte readByte(){return Byte.parseByte(readString());}// Read a short value from the keyboard public static short readShort(){return Short.parseShort(readString());}// Read a long value from the keyboard public static long readLong(){return Long.parseLong(readString());}// Read a float value from the keyboard public static float readFloat(){return Float.parseFloat(readString());}}。