2016年12月28日星期三

异或加密以及python的实现

python2java模块,用来转换代码

java  : Passwd.getBytes()
python : bytearray(passwd)


java
    String secret = "secret";
    byte[] thebytes = secret.getBytes();
   
python3
    secret = "secret"
    secret.encode()
   
python27
    s = 'secret'
    b = bytearray(s)
    for i in b:
        print i
       
这次的安卓代码审计,主要依靠工具JD-GUI,并且使用搜索功能,只要把想要知道的变量名
一个个的搜索,总会找到相要的代码,不要不耐烦.

这次遇到很多不熟悉的函数
map()


数据的进制转换
    int()   可以将 二进制,八进制,十六进制转换成十进制整型
    >>> int('1111', 2)
    15
    >>> int('f', 16)
    15
    >>> int('17', 8)
    15
   
    chr()   将整型数字转换为字符
    ord()   将字符转换为整型
    hex()   将十进制转换为十六进制
    oct()   将十进制转换为八进制
    bin()   将十进制转换为二进制

ASCII码 与 字符 相互转换
  ord()   字符 转 数字
  chr()   数字 转 字符


base64 编码与解码

    import base64
    text = 'test'
    cipher = base64.b64encode(text)
    re_text = base64.b64decode(cipher)

    print cipher
    print re_text



下面是python2.7代码的实现 
import base64

# 密码转成字节码
password = "123456" 
passwd = bytearray(password)

def trans_code(trans_type, para_dict):
    
    if trans_type == 'en':
        # 编码

        for (x,y) in para_dict.items():

            # 参数转成字节码
            bytecode = bytearray(y)

            j = 0
            cryptoValue = ''
            ascii_words = ''
            
            for i in range( len(bytecode) ):
                # 将 参数字节码 与 密码字节码 做异或操作,异或是对称加解密算法
                cryptoValue = bin( bytecode[i] ^ passwd[j] )

                # 密码长度有限,需要循环使用
                j += 1
                if j > len(passwd):
                    j = 0

                # 二进制 转 十进制
                decimal = int(cryptoValue, 2)

                # 十进制 转 ASCII字符
                ascii_words += chr( decimal )

            # base64 编码 ASCII字符
            cipher = base64.b64encode(ascii_words)

            print x + ' : ' + y + ' : ' + cipher

        print '='*10 + 'encode finish' + '='*10


参考链接:
1. 异或加密原理
2. ASCII码对照表
3. Python: convert string to byte array

没有评论:

发表评论