日期:2014-05-20  浏览次数:20838 次

搜狗网申的java笔试题!
public class Test { 


public static void encode(byte[] in, byte[] out, int password) 

int len = in.length; 

int seed = password ^ 0x36a67d8d; 
for (int i = 0 ; i < len; ++i) { 
byte a = (byte)( ( in[i] ^ seed ) >>> 4 ); 
byte b = (byte)( ( ( ((int)in[i]) << 11 ) ^ seed ) >>> (11-4) ); 
a &= 0xf; 
b &= 0xf0; 
out[i] = (byte)(a | b); 
seed = (((seed << 7) ^ seed ^ out[i]) + 536513); 




public static void decode(byte[] in, byte[] out, int password) 

int len = in.length; 

int seed = password ^ 0x36a67d8d; 
for (int i = 0 ; i < len; ++i) { 
// fill the code here 


public static void main(String [] args) throws Exception 

int password = 0xcd2c0597; 
byte[] buf1 = {77, 92, 43, 109, 120, -102, 97, 127, -105, -104, 47, 67, -2, 12, -89, -57, -125, -65, -7, -55, 123, 97, -99, -76, -127, -44, 66, -77, -115, 89, 118, -17, 64, 32, -58, 54, 121, -54, -78, 77, -70, -51, }; 
byte[] buf2 = new byte[buf1.length]; 
decode(buf1, buf2, password); 
System.out.println(new String(buf2, "GBK")); 



}


大家帮忙看看,改了好久没改对!!!

------解决方案--------------------
Java code


public class Test {

    public static void encode(byte[] in, byte[] out, int password) {
    int len = in.length;

    int seed = password ^ 0x36a67d8d;
    for (int i = 0; i < len; ++i) {
        byte a = (byte) ((in[i] ^ seed) >>> 4);
        byte b = (byte) (((((int) in[i]) << 11) ^ seed) >>> (11 - 4));
        a &= 0xf;
        b &= 0xf0;
        out[i] = (byte) (a | b);
        seed = (((seed << 7) ^ seed ^ out[i]) + 536513);
    }
    }

    public static void decode(byte[] in, byte[] out, int password) {
    int len = in.length;

    int seed = password ^ 0x36a67d8d;
    for (int i = 0; i < len; ++i) {
        // fill the code here  
        //--------------------------------------------------------
        //想办法循环左移4位

        byte a = (byte) ((in[i]) << 4); ////有效位剩余4位
        byte b = (byte) ((in[i]) >>> 4); // //有效位剩余4位

        //---------------------------------------------------------

        a = (byte) (a ^ seed); // 还原
        a &= 0xf0; //无效位再次置0,因为有可能,经过异或之后变成1了
        b = (byte) (((((int) b) << 11) ^ seed) >>> 11); // 还原
        b &= 0x0f; ////无效位再次置0,因为有可能,经过异或之后变成1了 

        //System.out.println(out[i]);
        out[i] = (byte) (a | b); //将临时变量中的数据串接,保存到out[i],这是真实的数据来了,
        //每一次循环seed是不一样的,利用输入数据in[i]和常量536513求出seed
        // System.out.println(out[i]);
        seed = (((seed << 7) ^ seed ^ in[i]) + 536513);
    }
    }

    public static void main(String[] args) throws Exception {
    int password = 0xcd2c0597;
    byte[] buf1 = { 77, 92, 43, 109, 120, -102, 97, 127, -105, -104, 47,
        67, -2, 12, -89, -57, -125, -65, -7, -55, 123, 97, -99, -76,
        -127, -44, 66, -77, -115, 89, 118, -17, 64, 32, -58, 54, 121,
        -54, -78, 77, -70, -51, };
    byte[] buf2 = new byte[buf1.length];
    decode(buf1, buf2, password);
    System.out.println(new String(buf2, "GBK"));
    }

}

/*output
搜狗手机输入法为亿万手机用户负责!!!!!
*/