【7、對稱之BlowFish】
blowfish加密函數迭代執行16輪,分組長度64位,密鑰長度從32位到448位。
算法由兩部分組成,密鑰擴展部分和數據加密部分。
加密算法是一種對稱的分組加密算法,每次加密一個64位的分組。使用32-448位可變長度的密鑰。加密過程分爲兩個階段:密鑰預處理和信息加密。
【對稱之 blowfish demo】
略!
【8、對稱之RC4】
RC4於1987年提出,和DES算法一樣,是一種對稱加密算法,也就是說使用的密鑰爲單鑰(或稱爲私鑰)。但不同於DES的是,RC4不是對明文進行分組處理,而是字節流的方式依次加密明文中的每一個字節,解密的時候也是依次對密文中的每一個字節進行解密。
RC4算法的特點是算法簡單,運行速度快,而且密鑰長度是可變的,可變範圍爲1-256字節(8-2048比特)。
【對稱之RC4 小demo】
【RC4 加密】
/**
* RC4加密
* @param data 需要加密的內容
* @param key 自定義密鑰
* @return 加密後的內容
*/
public static String encry_RC4_string(String data, String key) {
if (data == null || key == null) {
return null;
}
return toHexString(asString(encry_RC4_byte(data, key)));
}
private static String toHexString(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
int ch = (int) s.charAt(i);
String s4 = Integer.toHexString(ch & 0xFF);
if (s4.length() == 1) {
s4 = '0' + s4;
}
str = str + s4;
}
return str;// 0x表示十六進制
}
public static byte[] encry_RC4_byte(String data, String key) {
if (data == null || key == null) {
return null;
}
byte b_data[] = data.getBytes();
return RC4Base(b_data, key);
}
private static String asString(byte[] buf) {
StringBuffer strbuf = new StringBuffer(buf.length);
for (int i = 0; i < buf.length; i++) {
strbuf.append((char) buf[i]);
}
return strbuf.toString();
}
private static byte[] RC4Base(byte[] input, String mKkey) {
int x = 0;
int y = 0;
byte key[] = initKey(mKkey);
int xorIndex;
byte[] result = new byte[input.length];
for (int i = 0; i < input.length; i++) {
x = (x + 1) & 0xff;
y = ((key[x] & 0xff) + y) & 0xff;
byte tmp = key[x];
key[x] = key[y];
key[y] = tmp;
xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;
result[i] = (byte) (input[i] ^ key[xorIndex]);
}
return result;
}
【RC4 解密】
/**
* RC4解密
* @param data 需要解密的內容
* @param key 自定義密鑰
* @return 解密後的內容
*/
public static String decry_RC4(String data, String key) {
if (data == null || key == null) {
return null;
}
return new String(RC4Base(HexString2Bytes(data), key));
}
private static byte[] initKey(String aKey) {
byte[] b_key = aKey.getBytes();
byte state[] = new byte[256];
for (int i = 0; i < 256; i++) {
state[i] = (byte) i;
}
int index1 = 0;
int index2 = 0;
if (b_key == null || b_key.length == 0) {
return null;
}
for (int i = 0; i < 256; i++) {
index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
byte tmp = state[i];
state[i] = state[index2];
state[index2] = tmp;
index1 = (index1 + 1) % b_key.length;
}
return state;
}
private static byte[] HexString2Bytes(String src) {
int size = src.length();
byte[] ret = new byte[size / 2];
byte[] tmp = src.getBytes();
for (int i = 0; i < size / 2; i++) {
ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
}
return ret;
}
private static byte uniteBytes(byte src0, byte src1) {
char _b0 = (char) Byte.decode("0x" + new String(new byte[] { src0 })).byteValue();
_b0 = (char) (_b0 << 4);
char _b1 = (char) Byte.decode("0x" + new String(new byte[] { src1 })).byteValue();
byte ret = (byte) (_b0 ^ _b1);
return ret;
}
【對稱之RC4小 demo】
String rc4_str = encry_RC4_string(str, "單位對我的風範");
System.out.println("對稱之RC4加密後的內容" + rc4_str);
System.out.println("對稱之RC4解密後的內容" + decry_RC4(rc4_str, "單位對我的風範"));
【9、對稱之RC5加密解密】
RC5加密算法有四種:
1、原始RC5塊加密,rc5密碼使用固定的輸入長度,使用一個依賴密鑰的轉換產生一個固定的輸出塊。
2、RC5-CBC,是RC5的塊密碼鏈接模式。
3、RC5-CBC-Pad,處理任意長度的明文。
4、RC5-CTS是RC5加密算法的密文挪用模式,處理任意長度的明文且密文的長度匹配明文的長度。