Nicki  Wei
Nicki Wei

Reputation: 171

How to transplant the java MD5 encrypt code into Python?

Here is the standard code that java use to do the MD5 encryption for a string,

import java.security.MessageDigest;

public class TransCode{
public static byte[] transcode(String text) throws Exception{
    byte[] bytes = text.getBytes("UTF-8");
    return bytes;
}

public static void main(String[] args){
    try{
        System.out.println("ORIGIN STRING: hello world");
        byte[] byteArray = TransCode.transcode("hello world");
        int arrLen = byteArray.length;
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        messageDigest.update(byteArray);
        System.out.print("BYTE ARRAY:  ");
        for (int i=0;i<arrLen;i++){
            System.out.print(byteArray[i]);
            System.out.print("  ");
        }
        System.out.println();
        byteArray = messageDigest.digest();
        System.out.print("MD5 RESULT:  ");
        for (int i=0;i<arrLen;i++){
            System.out.print(byteArray[i]);
            System.out.print("  ");
        }
        System.out.print("\n");
    }
    catch(Exception ex){
        System.out.println(ex);
    }
}

}

The result is as follows,

ORIGIN STRING: hello world
BYTE ARRAY:  104  101  108  108  111  32  119  111  114  108  100
MD5 RESULT:  94  -74  59  -69  -32  30  -18  -48  -109  -53  34

so how to transplant this standard java code into python and get the same result, here is the code i wrote that has bugs:

# -*- coding: utf-8 -*-
from hashlib import md5

def MD5Digest(text):
    byteList = []
    for item in text:
        md=md5()
        unit = str(ord(item))
        print unit,  
        md.update(unit)
        byteList.append(md.hexdigest())
    print 
    return byteList

    print MD5Digest(u"hello world".encode("UTF8"))

Result is as follows,

104 101 108 108 111 32 119 111 114 108 100
['c9e1074f5b3f9fc8ea15d152add07294', '38b3eff8baf56627478ec76a704e9b52',     'a3c65c2974270fd093ee8a9bf8ae7d0b', 'a3c65c2974270fd093ee8a9bf8ae7d0b', '698d51a19d8a121ce581499d7b701668', '6364d3f0f495b6ab9dcf8d3b5c6e0b01', '07e1cd7dca89a1678042477183b7ac3f', '698d51a19d8a121ce581499d7b701668', '5fd0b37cd7dbbb00f97ba6ce92bf5add', 'a3c65c2974270fd093ee8a9bf8ae7d0b', 'f899139df5e1059396431415e770c6dd']

You can see although the byte code results seem to be the same, the MD5 encrypt results are totally different.

PLEASE help me to rewrite my python code to debug, thanks a lot. Due to the project reason, the solution to just reference this piece of java code in my python project is not actually allowed, please understand.

Upvotes: 2

Views: 1828

Answers (1)

kalhartt
kalhartt

Reputation: 4129

There are a few issues, first you calculate the hexdigest instead of the digest. Second you calculate the hexdigest inside the loop instead of outside. Instead it should look like this:

# Python 3
from hashlib import md5

def MD5Digest(text):
    digest = md5(text).digest()
    print('ORIGIN STRING:', text)
    print('BYTE ARRAY:', *text)
    print('MD5 RESULT:', *digest)
    return digest

# Python 2
from hashlib import md5

def MD5Digest(text):
    digest = md5(text).digest()
    print 'ORIGIN STRING:', text
    print 'BYTE ARRAY:', " ".join(str(ord(c)) for c in text)
    print 'MD5 RESULT:', " ".join(str(ord(c)) for c in digest)
    return digest

This outputs

>>> MD5Digest(b'hello world')
ORIGIN STRING: b'hello world'
BYTE ARRAY: 104 101 108 108 111 32 119 111 114 108 100
MD5 RESULT: 94 182 59 187 224 30 238 208 147 203 34 187 143 90 205 195

The only difference is the signage, just remember the numbers are equal mod 256.

Upvotes: 1

Related Questions