`
zhoujianghai
  • 浏览: 435503 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

搜狗的一道关于加密解密的在线测评题目

阅读更多

是一个信息编码的程序,阅读其encode部分,并补全其decode部分 
最后运行程序,会打印出的一句话。这句话就是我们要求的答案。

 

题目源码如下:

 

 

public class Test { 

	/**
	 * 加密过程
	 * @param in
	 * @param out
	 * @param password
	 */
    public static void encode(byte[] in, byte[] out, int password) { 
        int len = in.length; 

        int seed = password ^ 0x8c357ca5; 
        for (int i = 0; i < len; ++i) { 
        	/**
        	 *因为JVM中涉及byte、short、char类型的运算操作首先会把这些值转换成int类型,
        	 * 然后对int类型的值进行运算,所以需要把运算结果强制转换成byte类型
        	 */
            byte a = (byte) ((in[i] ^ seed) >>> 5);  //把异或后的值存放在a的低3位
            byte b = (byte) (((((int) in[i]) << 16) ^ seed) >>> (16 - 3)); //把异或后的值存放在b的高5位
            a &= 0x7; //0x7:0000 0111;将a中未存储有效数的位清零
            b &= 0xf8; //0xf8:1111 1000;将b中未存储有效数的位清零
            out[i] = (byte) (a | b); 
            seed = (seed * 3687989 ^ seed ^ in[i]); 
        } 
    } 
/**
 * 
 * @param in
 * @param out
 * @param password
 */
    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) { 
        	//补全代码....
        	
        } 
    } 

    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")); 
    } 
} 
 

需要补全的代码如下:

 

 

byte a = (byte)(((in[i] & 0x7) << 5) ^ seed);//把值存放到a的高3位
        	byte b = (byte)(((((((int)in[i]) & 0xF8 ) << (16 - 3))) ^ seed) >>> 16); //把值存放到b的低5位
        	a &= 0xe0;  //0xe0:1110 0000;将a中未存储有效数的位清零
        	b &= 0x1f;  //0x1f:0001 1111;将b中未存储有效数的位清零
        	out[i] = (byte)(a | b);
        	seed = (seed * 3687989 ^ seed ^ out[i]); 
 

 

运行结果:

 

结果:搜狗输入法支持各种炫酷的皮肤,彰显个性的你!!!

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics