犬のペンの試験問題を探します——暗号化して解読します


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