Latifrons的技术/生活博客成功落户cnblogs!

非常喜欢这种专门为程序员设计的博客!

嗯,先测试一段:

(这是开心农场加密传输数据的解密算法,最近用到的,Java翻译版本)

 

 

 1import java.io.ByteArrayOutputStream;
 2import java.io.FileInputStream;
 3import java.io.FileNotFoundException;
 4import java.io.IOException;
 5
 6
 7public class Decode {
 8    public static void main(String args[]) throws IOException{
 9        FileInputStream fis = new FileInputStream("file");
10        ByteArrayOutputStream baos = new ByteArrayOutputStream();
11        int ch;
12        while((ch = fis.read()) !=-1){
13            baos.write(ch);
14        }

15        byte[] b = baos.toByteArray();
16        byte[] c = decode(b,162);
17        String s = new String(c);
18        System.out.println(s);
19//        byte[] source = new byte[]{(byte) 0xfd};
20//        for (int decodeKey = 0;decodeKey<1000;decodeKey++){
21//            byte[] a = decode(source,decodeKey);
22//            if (a[0] == 91){
23//                System.out.println(decodeKey);
24//                break;
25//            }
26//        }
27    }

28    
29    
30    public static byte[] decode(byte[] source,int decodeKey){
31        //FD  1D  C4  17  15  07  14  EB  06  C4 DC C4
32        //253 29  196 23  21  7   20  235 6
33        //[   {   "   u   s   e   r   I   d   "  :  "
34        //91  123 34  117 115 101 114 73  100 
35        ByteArrayOutputStream dest = new ByteArrayOutputStream(); 
36        int current = 0;
37        int len = source.length;
38        int bytea = 47;
39        int byteb = 48;
40        //int decodeKey = 0;
41        while (current <len){
42            if (source[current] == bytea){
43                if (current +1 != len && source[current +1== bytea){
44                    current ++;
45                    dest.write(bytea - decodeKey);
46                }
else if (current +1 != len && source[current +1== byteb){
47                    current ++;
48                    if (decodeKey == 0){
49                        dest.write(byteb-decodeKey);
50                    }
else{
51                        dest.write(-decodeKey);
52                    }

53                }
else{
54                    dest.write(source[current]-decodeKey);
55                }

56            }
else{
57                dest.write(source[current]-decodeKey);
58            }

59            current++;
60        }

61        return dest.toByteArray();
62    }

63}

64

 

看,支持语法高亮哦~~嘻嘻

 

posted on 2009-06-17 20:05  Latifrons  阅读(230)  评论(0编辑  收藏  举报