凯撒密码一种代换密码,据说凯撒是率先使用加密函的古代将领之一,因此这种加密方法被称为恺撒密码。凯撒密码的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。
使用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+" ");
}
}
}
解密前后: