package util;
import java.io.UnsupportedEncodingException;
public class RC4Utils {
public static String RC4(String sInput, String sKey) {
try {
byte[] aInput = sInput.getBytes("UTF-8");
byte[] aKey = sKey.getBytes("UTF-8");
int[] iS = new int[256];
byte[] iK = new byte[256];
for (int i = 0; i < 256; i++)
iS[i] = i;
int j = 1;
for (short i = 0; i < 256; i++) {
iK[i] = (byte) aKey[i % aKey.length];
}
j = 0;
for (int i = 0; i < 256; i++) {
j = (j + iS[i] + iK[i]) & 0xff;
int temp = iS[i] & 0xff;
iS[i] = iS[j] & 0xff;
iS[j] = temp;
}
int i = 0;
j = 0;
for (int x = 0; x < aInput.length; x++) {
i = (i + 1) & 0xff;
j = (j + iS[i]) & 0xff;
int temp = iS[i] & 0xff;
iS[i] = iS[j] & 0xff;
iS[j] = temp;
int t = ((iS[i] + iS[j])) & 0xff;
int iY = iS[t];
byte iCY = (byte) (iY & 0xff);
aInput[x] = (byte) ((aInput[x] ^ iCY) & 0xff);
}
return new String(aInput, "UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
}
}
}
RC4算法
最新推荐文章于 2021-02-21 00:33:30 发布