# 聊胜于无 Java之Caesar与Vigenere实现

2011-10-29

1 背景介绍话说目前做所谓"企业"开发的语言基本就集中在运用.Net和J2EE上了。又话说，在下很不幸又和Java"同流合污"了一把。现在回想起来，真是感慨万千啊～遥想公瑾当年，小乔初嫁了，雄姿英发，羽扇纶巾，谈笑...

1 背景介绍

2 Caesar加密算法
Caesar加密算法算是经典加密算法中最简单的了。对于标准的Caesar来说，就是把字母序列向后移动一定的数量，替换后得到密文，而这个数量为固定值3。也就是说，在都是由英文字母组成的文本里，字母A将会被替换成D，B会被替换成E，以此类推。

public class Caesar { private String table; private int seedA = 1103515245; private int seedB = 12345; public Caesar(String table, int seed) { this.table = chaos(table, seed, table.length()); } public Caesar(String table) { this(table, 11); } public Caesar() { this(11); } public Caesar(int seed) { this("ABCDEFGHIJKLMNOPQRSTUVWXYZ", seed); } public char dict(int i, boolean reverse) { int s = table.length(), index = reverse ? s - i : i; return table.charAt(index); } public int dict(char c, boolean reverse) { int s = table.length(), index = table.indexOf(c); return reverse ? s - index : index; } public int seed(int seed) { long temp = seed; return (int)((temp * seedA + seedB) & 0x7fffffffL); } public String chaos(String data, int seed, int cnt) { StringBuffer buf = new StringBuffer(data); char tmp; int a, b, r = data.length(); for (int i = 0; i < cnt; i += 1) { seed = seed(seed); a = seed % r; seed = seed(seed); b = seed % r; tmp = buf.charAt(a); buf.setCharAt(a, buf.charAt(b)); buf.setCharAt(b, tmp); } return buf.toString(); } public String crypto(boolean reverse, int key, String text) { String ret = null; StringBuilder buf = new StringBuilder(); int m, s = table.length(), e = text.length(); for(int i = 0; i < e; i += 1) { m = dict(text.charAt(i), reverse); if (m < 0) break; m = m + key + i; buf.append(dict(m % s, reverse)); } if (buf.length() == e) ret = buf.toString(); return ret; } public String encode(int key, String text) { return crypto(false, key, text); } public String decode(int key, String text) { return crypto(true , key, text); } public static void main(String[] args) { Caesar caesar = new Caesar(); String data = caesar.encode(32, "APPLE"); caesar.decode(32, data); } } public class Caesar {
private String table;
private int seedA = 1103515245;
private int seedB = 12345;

public Caesar(String table, int seed) {
this.table = chaos(table, seed, table.length());
}
public Caesar(String table) {
this(table, 11);
}
public Caesar() {
this(11);
}
public Caesar(int seed) {
this("ABCDEFGHIJKLMNOPQRSTUVWXYZ", seed);
}
public char dict(int i, boolean reverse) {
int s = table.length(), index = reverse ? s - i : i;
return table.charAt(index);
}
public int dict(char c, boolean reverse) {
int s = table.length(), index = table.indexOf(c);
return reverse ? s - index : index;
}
public int seed(int seed) {
long temp = seed;
return (int)((temp * seedA + seedB) & 0x7fffffffL);
}

public String chaos(String data, int seed, int cnt) {
StringBuffer buf = new StringBuffer(data);
char tmp; int a, b, r = data.length();
for (int i = 0; i < cnt; i += 1) {
seed = seed(seed); a = seed % r;
seed = seed(seed); b = seed % r;
tmp = buf.charAt(a);
buf.setCharAt(a, buf.charAt(b));
buf.setCharAt(b, tmp);
}
return buf.toString();
}

public String crypto(boolean reverse,
int key, String text) {
String ret = null;
StringBuilder buf = new StringBuilder();
int m, s = table.length(), e = text.length();

for(int i = 0; i < e; i += 1) {
m = dict(text.charAt(i), reverse);
if (m < 0) break;
m = m + key + i;
buf.append(dict(m % s, reverse));
}
if (buf.length() == e)
ret = buf.toString();
return ret;
}
public String encode(int key, String text) {
return crypto(false, key, text);

}
public String decode(int key, String text) {
return crypto(true , key, text);
}

public static void main(String[] args) {
Caesar caesar = new Caesar();
String data = caesar.encode(32, "APPLE");
caesar.decode(32, data);
}
}

3 Vigenere加密算法

public class Vigenere { private String table; private int seedA = 1103515245; private int seedB = 12345; public Vigenere(String table, int seed) { this.table = chaos(table, seed, table.length()); } public Vigenere(String table) { this(table, 11); } public Vigenere() { this(11); } public Vigenere(int seed) { this("ABCDEFGHIJKLMNOPQRSTUVWXYZ", seed); } private char dict(int i, boolean reverse) { int s = table.length(), index = reverse ? s - i : i; return table.charAt(index); } private int dict(char c, boolean reverse) { int s = table.length(), index = table.indexOf(c); return reverse ? s - index : index; } private int seed(int seed) { long temp = seed; return (int)((temp * seedA + seedB) & 0x7fffffffL); } public String chaos(String data, int seed, int cnt) { StringBuffer buf = new StringBuffer(data); char tmp; int a, b, r = data.length(); for (int i = 0; i < cnt; i += 1) { seed = seed(seed); a = seed % r; seed = seed(seed); b = seed % r; tmp = buf.charAt(a); buf.setCharAt(a, buf.charAt(b)); buf.setCharAt(b, tmp); } return buf.toString(); } public String crypto(boolean reverse, String key, String text) { String ret = null; StringBuilder buf = new StringBuilder(); int m, k, s = table.length(), e = text.length(), ke = key.length(); for(int i = 0; i < e; i += 1) { m = dict(text.charAt(i), reverse); k = dict(key.charAt(i % ke), false); if (m < 0 || k < 0) break; m = m + k + i; buf.append(dict(m % s, reverse)); } if (buf.length() == e) ret = buf.toString(); return ret; } public String encode(String key, String text) { return crypto(false, key, text); } public String decode(String key, String text) { return crypto(true , key, text); } public static void main(String[] args) { Vigenere vigenere = new Vigenere(); String data = vigenere.encode("BELLASO", "APPLE"); vigenere.decode("BELLASO", data); } } public class Vigenere {
private String table;
private int seedA = 1103515245;
private int seedB = 12345;

public Vigenere(String table, int seed) {
this.table = chaos(table, seed, table.length());
}
public Vigenere(String table) {
this(table, 11);
}
public Vigenere() {
this(11);
}
public Vigenere(int seed) {
this("ABCDEFGHIJKLMNOPQRSTUVWXYZ", seed);
}

private char dict(int i, boolean reverse) {
int s = table.length(), index = reverse ? s - i : i;
return table.charAt(index);
}
private int dict(char c, boolean reverse) {
int s = table.length(), index = table.indexOf(c);
return reverse ? s - index : index;
}
private int seed(int seed) {
long temp = seed;
return (int)((temp * seedA + seedB) & 0x7fffffffL);
}

public String chaos(String data, int seed, int cnt) {
StringBuffer buf = new StringBuffer(data);
char tmp; int a, b, r = data.length();
for (int i = 0; i < cnt; i += 1) {
seed = seed(seed); a = seed % r;
seed = seed(seed); b = seed % r;
tmp = buf.charAt(a);
buf.setCharAt(a, buf.charAt(b));
buf.setCharAt(b, tmp);
}
return buf.toString();
}
public String crypto(boolean reverse,
String key, String text) {
String ret = null;
StringBuilder buf = new StringBuilder();
int m, k, s = table.length(),
e = text.length(),
ke = key.length();

for(int i = 0; i < e; i += 1) {
m = dict(text.charAt(i), reverse);
k = dict(key.charAt(i % ke), false);
if (m < 0 || k < 0) break;
m = m + k + i;
buf.append(dict(m % s, reverse));
}
if (buf.length() == e)
ret = buf.toString();
return ret;
}
public String encode(String key, String text) {
return crypto(false, key, text);

}
public String decode(String key, String text) {
return crypto(true , key, text);
}
public static void main(String[] args) {
Vigenere vigenere = new Vigenere();
String data = vigenere.encode("BELLASO", "APPLE");
vigenere.decode("BELLASO", data);
}
}

4 总结

--------------------------------------------------------------------------------

Footnotes:
1Struts2 + Spring + Hibernate