public class Test {
public static void encode(byte[] in, byte[] out, int password) {
int len = in.length;
//原理: a^b^b = a
int seed = password ^ 0x8c357ca5;
for (int i = 0; i < len; ++i) {
byte a = (byte) ((in[i] ^ seed) >>> 5);//取得byte的前3位,并存放于低3位
byte b = (byte) (((((int) in[i]) << 16) ^ seed) >>> (16 - 3));// 取得低5位存放于高5位
a &= 0x7; //取低3位
b &= 0xf8; //取高5位
out[i] = (byte) (a | b); //合并
seed = (seed * 3687989 ^ seed ^ in[i]); //如果这里是IN,解密就是OUT,相反亦然
}
}
//解密是加密的反向过程
public static void decode(byte[] in, byte[] out, int password) {
int len = in.length;
int seed = password ^ 0x8c357ca5;
for (int i = 0; i < len; ++i) {
byte a = (byte) (((in[i]&0x7) <<5) ^seed); //取低3位回移5位,成为一个字节
byte b = (byte) ((((((int) in[i])&0xf8) << 13) ^ seed) >>> 16); //取高5位回移,13位,最后这几位成为了高5位
a &= 0xe0;//取高3位
b &= 0x1f;//取低5位
out[i] = (byte) (a | b);
seed = (seed * 3687989 ^ seed ^ out[i]);//这里是OUT
}
}
public static void main(String[] args) throws Exception {
int password = 0xe87dd9d3;
byte[] buf1 = { 29, -16, 96, 43, -85, 25, -96, 83, 13, 66, -109, 49, -111, 0, 60, -101, 99, -86, -38, 86, -35,
48, 23, 83, -102, 25, 73, -116, -101, -88, -5, 14, -14, -112, 87, -87, 2, 108, -58, 40, 56, 12, 108,
77, 83, 38, 20, -114, };
byte[] buf2 = new byte[buf1.length];
decode(buf1, buf2, password);
System.out.println(new String(buf2, "GBK"));
}
}
主要利用了(1):解密是加密的反过程(2):a^b^b=a
搜狗笔试题——加密解密
最新推荐文章于 2022-07-01 11:07:16 发布