我如何编写更少的python2代码来实现相同的目的,需要的加密结果是相同的。

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

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

最佳答案

使用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

关于java - 我怎样才能最好地将此java方法转换为python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39557242/

10-12 16:56