我怎样才能将这个java方法最好地翻译成python

时间:2016-09-18 11:37:51

标签: java python python-2.7 python-3.x

如何编写较少的python2代码来实现同样的功能,需要加密结果是一样的。

b[i++] = (byte) 172;以什么方式将int转换为python2中的byte ....

public static String encryptPassword(String content) {


    String resultString = "";
       String appkey = "ftjf,ckdfkl";

       byte[] a = appkey.getBytes();
       byte[] datSource = content.getBytes();
       byte[] b = new byte[a.length + 4 + datSource.length];

       int i;
    for (i = 0; i < datSource.length; i++) {
        b[i] = datSource[i];
    }

    b[i++] = (byte) 172;
    b[i++] = (byte) 163;
    b[i++] = (byte) 161;
    b[i++] = (byte) 163;

    for (int k = 0; k < a.length; k++) {
        b[i] = a[k];
        i++;
    }

    try {
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        md5.update(b);
        resultString = new HexBinaryAdapter().marshal(md5.digest());
    } catch (Exception e) {

    }

    return resultString.toLowerCase();
}


public static void main(String[] args) {
    System.out.print(encryptPassword("123456"));
}

1 个答案:

答案 0 :(得分:0)

使用chr获取python中字节的字符。

from Crypto.Hash import MD5

key = 'ftjf,ckdfkl'
src= '123'
b = src+chr(172)+chr(163)+chr(161)+chr(163)+key
md5 = MD5.new()
md5.update(b)
text = md5.hexdigest()
print text

鉴于src'123',输出为:

eedb36cc3204923459dcf891224a0c1d