uzqp文件的加解密

帮朋友做的,根据python版本翻译成的java版本,记录一下代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @author xirtam
 * @data 2015年03月26日19:49:39
 *
 */
public class Tool {

    public static int[] xorstr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
            1, 13, 14, 15, 16 };

    /**
     * 加密/解密,如果inpath传uzqp,outPath传png,则解密,反之加密
     * 
     * @param inPath
     * @param outPath
     */
    public static void decodeAndEncode(String inPath, String outPath) {
        try {
            StringBuilder target = new StringBuilder();
            FileInputStream fis = new FileInputStream(new File(inPath));
            byte[] bytes = new byte[fis.available()];
            fis.read(bytes);
            char[] chars = new char[bytes.length];
            for (int i = 0; i < chars.length; i++) {
                chars[i] = (char) bytes[i];
                target.append(chr((byte) (ord(chars[i]) ^ (xorstr[i
                        % (xorstr.length)]))));
            }
            fis.close();
            System.out.println("i: " + chars.length);
            System.out.println("o: " + target.toString().length());
            FileOutputStream fos = new FileOutputStream(new File(outPath));
            byte[] fuck = new byte[chars.length];
            for (int i = 0; i < target.length(); i++) {
                fuck[i] = (byte) target.toString().charAt(i);
            }
            fos.write(fuck);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static char chr(byte c) {
        return (char) c;
    }

    public static byte ord(char c) {
        return (byte) c;
    }

    public static void main(String[] args) {
        decodeAndEncode(
                "/Users/corleone/Documents/temp/testpy/btn_menu111.png.uzqp",
                "/Users/corleone/Documents/temp/testpy/Bubble3333.png");

        // decodeAndEncode("/Users/corleone/Documents/temp/testpy/Bubble3.png",
        // "/Users/corleone/Documents/temp/testpy/btn_menu111.png.uzqp");
    }

}

python版本

def fun():  
  fn1='./btn_menu1.png.uzqp'  
  xorstr='\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x01\x0d\x0e\x0f\x10'  
  with open(fn1,'rb') as f:  
    data = f.read();  
    data0=""  
    print len(data)
    for i in range(len(data)): 
      data0  += chr( ord(data[i]) ^ ord(xorstr[i % (len(xorstr))]))  
  
  fn2='Bubble2.png'  
  with open(fn2,'wb') as f:  
    f.write(data0)  
  
if __name__ == '__main__':  
  fun()  

 

posted @ 2015-03-27 11:38  带狗狗带  Views(1166)  Comments(0Edit  收藏  举报