1、工具
public class RC4Utils {
private static final String CHARSET = "UTF-8";
// ===============================加密 begin
public static byte[] encrypt(String source, String key) throws Exception {
if (source == null || key == null) {
return null;
}
byte b_data[] = source.getBytes(CHARSET);
return RC4Base(b_data, key);
}
public static byte[] encrypt(byte[] source, String key) throws Exception {
return RC4Base(source, key);
}
public static String encryptToHexString(byte[] source, String key) throws Exception {
byte[] encodeByte = encrypt(source, key);
return HexUtils.toHexString(encodeByte);
}
public static String encryptToHexString(String source, String key) throws Exception {
byte[] encodeByte = encrypt(source, key);
return HexUtils.toHexString(encodeByte);
}
public static String encryptToBase64String(String source, String key) throws Exception {
byte[] encodeByte = encrypt(source, key);
return Base64.getEncoder().encodeToString(encodeByte);
}
// ===============================加密 end
// ===============================解密 begin
/**
* 解密
* @param source
* @param key
* @return
* @throws Exception
*/
public static byte[] decrypt(byte[] source, String key) throws Exception {
return RC4Base(source, key);
}
/**
*
* @param source 16进制
* @param key
* @return
* @throws Exception
*/
public static byte[] decryptHex(String source, String key) throws Exception {
return decrypt(HexUtils.fromHexString(source), key);
}
public static byte[] decryptBase64(String source, String key) throws Exception {
return decrypt(Base64.getDecoder().decode(source), key);
}
public static String decryptToString(byte[] source, String key) throws Exception {
byte[] decodeByte = decrypt(source, key);
String decodeStr = new String(decodeByte, CHARSET);
return decodeStr;
}
/**
* @param source 16进制
* @param key
* @return
* @throws Exception
*/
public static String decryptHexToString(String source, String key) throws Exception {
byte[] decodeByte = decryptHex(source, key);
String decodeStr = new String(decodeByte, CHARSET);
return decodeStr;
}
public static String decryptBase64ToString(String source, String key) throws Exception {
byte[] decodeByte = decryptBase64(source, key);
String decodeStr = new String(decodeByte, CHARSET);
return decodeStr;
}
// ===============================解密 end
/** ACCESS_TOKEN的加密钥匙 **/
public static final String ACCESSKEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxACCESSKEY";
/** REFRESH_TOKEN的加密钥匙 **/
public static final String REFRESHKEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxREFRESHKEY";
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[] 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;
}
}