古典数据加密的工作原理。
二、实训环境
一台安装有Windows 9X或Windows 2000/XP/NT的计算机。
三、实训内容
用一种高级语言编写程序实现对某一文件内容用恺撒加密(或维吉尼亚加密)法进行加密,然后用解密程序进行解密。
四、实训步骤
1、用一种高级语言编写程序实现对某一文件内容用恺撒加密(或维吉尼亚加密)法进行加密。
2、用解密程序对密文进行解密。
五、实训效果检测
import java.util.Scanner;
public class test {
void mj(){
Scanner in = new Scanner(System.in);
System.out.print("请选择操作(1.加密 2.解密):");
int n=in.nextInt();
if(n == 1){
System.out.print("请输入待加密的字符串:");
String str = in.next();
String jm="";
int key = 3;
for(int i = 0;i < str.length();i++){
char c = str.charAt(i);
if(c >= 'a'&&c <= 'z'){
if(c>='x'&&c<='z'){
c-=26;
c+=key;
}
else{
c+=key;
}
}
else if(c >= 'A'&&c <= 'Z'){
if(c>='X'&&c<='Z'){
c-=26;
c+=key;
}
else{
c+=key;
}
}
jm += c;
}
System.out.print("加密后的字符串是:"+jm);
System.out.print("\\n输入任意建继续,0结束程序:");
n=in.nextInt();
if(n==0){
System.out.print(" 谢谢使用本程序,欢迎再次使用!");
}
else{
this.mj();
}
}
else if(n == 2){
System.out.print("请输入待解密的字符串:");
String str = in.next();
String jm="";
int key = -3;
for(int i = 0;i < str.length();i++){
char c = str.charAt(i);
if(c >= 'a'&&c <= 'z'){
if(c>='a'&&c<='c'){
c+=26;
c+=key;
}
else{
c+=key;
}
}
else if(c >= 'A'&&c <= 'Z'){
if(c>='A'&&c<='C'){
c+=26;
c+=key;
}
else{
c+=key;
}
}
jm += c;
}
System.out.println("解密后的字符串:"+jm);
System.out.print("\\n输入任意建继续,0结束程序:");
n=in.nextInt();
if(n==0){
System.out.print(" 谢谢使用本程序,欢迎再次使用!");
}
else{
this.mj();
}
}
else{
System.out.print("请输入1或2,其他字符无效!\\n输入任意建继续,0结束程序:");
n=in.nextInt();
if(n==0){
System.out.print(" 谢谢使用本程序,欢迎再次使用!");
}
else{
this.mj();
}
}
}
public static void main(String[] args) {
test mj=new test();
System.out.println("******欢迎使用凯撒加密器******");
mj.mj();
}
}
1、凯撒加密运行效果图:
2、凯撒解密运行效果图:
3、凯撒加密解密运行效果图: