凯撒密码的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。
使用JAVA程序实现对明文的加密:
package com.qikeyishu.www;
public class Kaisapwd {
public static void main(String[] args) {
char str[]={'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'};
for(char out:str){
System.out.print(out+" ");
}
for(int i=0;i<26;i++){
if(i<23){
str[i]+=3;
}else{
str[i]-=23;
}
}
System.out.println("n");
for(char out:str){
System.out.print(out+" ");
}
}
}
加密前后:
使用JAVA程序实现对密文的解密:
package com.qikeyishu.www;
public class Kaisapwd {
public static void main(String[] args) {
char str[]={'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'};
for(char out:str){
System.out.print(out+" ");
}
for(int i=0;i<26;i++){
if(i<23){
str[i]-=3;
}else{
str[i]+=23;
}
}
System.out.println("n");
for(char out:str){
System.out.print(out+" ");
}
}
}