凯撒大帝不仅战功卓越,同时对密码算法也是很有研究啊。下面要介绍的加密算法就是由其本人研究并在战争中使用,包括对后人也有着深远的影响。好了,不扯淡了,下面步入正题。
凯撒加密(Caesar cipher)是一种简单的消息编码方式:它根据字母表将消息中的每个字母移动常量位k。举个例子如果k等于3,则在编码后的消息中,每个字母都会向后移动3位:a会被替换为d;b会被替换成e;依此类推。字母表末尾将回卷到字母表开头。于是,w会被替换为z,x会被替换为a。
下面是JAVA的实现代码,包括对数字和字母表的两种实现方式。
ICaesar.java
public interface ICaesar {
String encrypt(String text, int shiftNum);
String decrypt(String text, int shiftNum);
}
Caesar.java
public abstract class Caesar implements ICaesar {
private String table = "";
public Caesar(String table) {
this.table = table;
}
protected void setTable(String table) {
this.table = table;
}
protected String encryptText(String text, int shiftNum) {
int len = table.length();
StringBuffer result = new StringBuffer();
for (int i = 0; i < text.length(); i++) {
char curChar = text.charAt(i);
int idx = table.indexOf(curChar);
if (idx == -1) {
result.append(curChar);
continue;
}
idx = ((idx + shiftNum) % len);
result.append(table.charAt(idx));
}
return result.toString();
}
@Override
public String encrypt(String text, int shiftNum) {
// TODO Auto-generated method stub
return encryptText(text, shiftNum);
}
@Override
public String decrypt(String text, int shiftNum) {
// TODO Auto-generated method stub
shiftNum = table.length() + (shiftNum % table.length());
return encryptText(text, shiftNum);
}
}
LetterCaesar.java
public class LetterCaesar extends Caesar {
private final static String upperLetter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private final static String lowerLetter = "abcdefghijklmnopqrstuvwxyz";
public LetterCaesar() {
super(upperLetter);
// TODO Auto-generated constructor stub
}
private void checkTextTypeAndSetTable(String text) {
char cur = text.charAt(0);
if (Character.isUpperCase(cur)) {
setTable(upperLetter);
} else {
setTable(lowerLetter);
}
}
@Override
public String encrypt(String text, int shiftNum) {
// TODO Auto-generated method stub
checkTextTypeAndSetTable(text);
return super.encrypt(text, shiftNum);
}
@Override
public String decrypt(String text, int shiftNum) {
// TODO Auto-generated method stub
checkTextTypeAndSetTable(text);
return super.decrypt(text, shiftNum);
}
/**
* @param args
*/
public static void main(String[] args) {
ICaesar numberCaesar = new LetterCaesar();
String encodeStr = numberCaesar
.encrypt("ABCDEFGHIGJLMNOPQRSTUVWXYZ", 3);
System.out.println(encodeStr);
String decodeStr = numberCaesar.decrypt(encodeStr, -3);
System.out.println(decodeStr);
encodeStr = numberCaesar.encrypt("abcdefghijklmnopqrstuvwxyq", 3);
System.out.println(encodeStr);
decodeStr = numberCaesar.decrypt(encodeStr, -3);
System.out.println(decodeStr);
}
}
NumberCaesar.java
public class NumberCaesar extends Caesar {
public NumberCaesar() {
super("0123456789");
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
ICaesar numberCaesar = new NumberCaesar();
String encodeStr = numberCaesar.encrypt("0123456789", 13);
System.out.println(encodeStr);
String decodeStr = numberCaesar.decrypt(encodeStr, -13);
System.out.println(decodeStr);
}
}