硬货,直接上代码
java:
/**
* RC4对称性加密解密算法
* RC4是密钥长度可变的流加密算法簇。其核心部分的S-box长度无限制。该算法的速度可以达到DES加密的10倍左右,且具有很高级别的非线性
* @author ftb
*/
public class RC4 {
private static final String strSourceKey = "123456789!@#$%^cq_youzhoukeji";
/**
* 通过时间戳生成key
* @param lTsms
* @return
*/
public static String makeKey(long lTsms) {
String strKey = strSourceKey + Long.toString(lTsms);
String strRet = strKey;
try {
strRet = SecurityUtil.getInstance().MD5String(strKey);
} catch (Exception e) {
LoggerUtil.getLogger().error(SecurityUtil.class.getSimpleName() + "md5 err!", e);
}
return strRet;
}
/**
* 解密后的数据为String类型
* @param data
* @param key
* @return
*/
public static String decryRC4Str(byte[] data, String key) {
if (data == null || key == null) {
return null;
}
return asString(RC4Base(data, key));
}
/**
* 解密后的数据为String类型
* @param data
* @param key
* @return
*/
public static String decryRC4Str(String data, String key) {
if (data == null || key == null) {
return null;
}
return new String(RC4Base(hexString2Bytes(data), key));
}
/**
* 解密后的数据为byte[]类型
* @param data
* @param key
* @return
*/
public static byte[] decryRC4Byte(byte[] data, String key) {
if (data == null || key == null) {
return null;
}
return RC4Base(data, key);
}
/**
* 加密后的数据为byte[]类型
* @param data
* @param key
* @return
*/
public static byte[] encryRC4Byte(byte[] data, String key) {
if (data == null || key == null) {
return null;
}
return RC4Base(data, key);
}
/**
* 加密后的数据为String类型
* @param data
* @param key
* @return
*/
public static String encryRC4Str(String data, String key) {
if (data == null || key == null) {
return null;
}
return toHexString(asString(encryRC4Byte(data.getBytes(), key)));
}
/**
* 将byte[]转化成String类型
* @param buf
* @return
*/
public 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();
}
/**
* 初始计算Key
* @param aKey
* @return
*/
private static byte[] prepareKey(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;
// for (int i = 0, j = 0; i < 256; i++)
// {
// j = (b_key[i % b_key.length] + state[i] + j) % 256;
// byte swapByte = state[i];
// state[i] = state[j];
// state[j] = swapByte;
// }
// return state;
}
/**
* 将String转化成16进制的String
* @param s
* @return
*/
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表示十六进制
}
/**
* 将String转换成byte[]
* @param src
* @return
*/
public 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;
}
/**
* byte的异或
* @param src0
* @param src1
* @return
*/
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算法核心
* @param input
* @param mKkey
* @return
*/
private static byte[] RC4Base(byte[] input, String mKkey) {
int x = 0;
int y = 0;
byte key[] = prepareKey(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;
}
}
c#:
/// <summary>
/// RC4流数据加解密
/// </summary>
public static class RC4
{
#region Decrypt
/// <summary>
/// 解密
/// </summary>
/// <param name="key">密码</param>
/// <returns>返回解密字节数组</returns>
public static byte[] Decrypt(string key, byte[] data)
{
return ToRC4(key, data);
}
#endregion
#region Encrypt
/// <summary>
/// 加密
/// </summary>
/// <param name="key">密码</param>
/// <returns>返回加密字节数组</returns>
public static byte[] Encrypt(string key, byte[] data)
{
return ToRC4(key, data);
}
#endregion
#region ToRC4
/// <summary>
/// 将明文与伪随机数流进行异或运算,得到密文
/// </summary>
/// <param name="key">秘钥</param>
/// <param name="data">数据</param>
/// <returns>返回密文或明文</returns>
private static byte[] ToRC4(string key, byte[] data)
{
byte[] keys = key.ToByteArray();
byte[] box = GetKeys(keys);
int x = 0;
int y = 0;
byte[] outData = new byte[data.Length];
for (int i = 0; i < data.Length; i++)
{
x = (x + 1) % 256;
y = (box[x] + y) % 256;
byte swapByte = box[x];
box[x] = box[y];
box[y] = swapByte;
outData[i] = (byte)(data[i] ^ box[(box[x] + box[y]) % 256]);
}
return outData;
}
#endregion
#region GetKeys
/// <summary>
/// 将秘钥散列成 256 字节
/// </summary>
/// <param name="keys">秘钥</param>
private static byte[] GetKeys(byte[] b_key)
{
byte[] state = Enumerable.Range(0, 256).Select(i => (byte)i).ToArray();
for (int i = 0, j = 0; i < 256; i++)
{
j = (b_key[i % b_key.Length] + state[i] + j) % 256;
byte swapByte = state[i];
state[i] = state[j];
state[j] = swapByte;
}
return state;
}
#endregion
}