Search code examples
javapython-2.7encryptiondes

DES in python can't get the correct encoded data using pycrypto


I hava a algorithm to encrypt data in java ,I want to rewrite it in python.But the two algorithm can't get the same encoded data. java code is :

String strDefaultKey = "QabC-+50";
Key key = new SecretKeySpec(strDefaultKey.getBytes("UTF-8"), "DES");
encryptCipher = Cipher.getInstance(DES_ECB);
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
String seed = "2016-09-19 05:11";
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(seed.getBytes());
byte[] m = md5.digest();
encryptCipher.doFinal(m);
byte[] encodeUrl = Base64.encodeBase64(sEncription.encrypt(m));
String finalUrl = new String(encodeUrl);
finalResult = finalUrl.substring(2, 8) + finalUrl.substring(10, 13);

my python code is:

m = 'QabC-+50'
text = '2016-09-19 05:11'
md5 = MD5.new()
md5.update(text)
text = md5.hexdigest()

cipher = DES.new(m, DES.MODE_ECB)
text_temp = cipher.encrypt(text)
final_str = base64.b64encode(text_temp)
print final_str
print final_str[2:8] + final_str[10:13]
print type(text_temp)

The two version codes can't get the same final string. Does anybody know why?


Solution

  • You crypto logic is ok,the difference between the two methods is their MD5 result.

    Without the MD5 step:

    Java code(I don't know what your sEncription is,remove it):

    import java.security.Key;
    import java.security.MessageDigest;
    import java.util.Base64;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    
    public class H {
        public static void main(String args[]){
            try{
            String strDefaultKey = "QabC-+50";
            Key key = new SecretKeySpec(strDefaultKey.getBytes("UTF-8"), "DES");
            Cipher encryptCipher = Cipher.getInstance("DES/ECB/NoPadding");
            encryptCipher.init(Cipher.ENCRYPT_MODE, key);
            String seed = "2016-09-19 05:11";
            byte[] a = encryptCipher.doFinal(seed.getBytes());
            byte[] encodeUrl = Base64.getEncoder().encode(a);
    //      byte[] encodeUrl = Base64.encodeBase64(sEncription.encrypt(m));
            String finalUrl = new String(encodeUrl);
            String finalResult = finalUrl.substring(2, 8) + finalUrl.substring(10, 13);
            System.out.println(finalUrl);
            System.out.println(finalResult);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    }
    

    OUTPUT:

    Wm+DLy8m9G2BJnH2wvtKvA==
    +DLy8m2BJ
    

    Python Code:

    from Crypto.Hash import MD5
    from Crypto.Cipher import DES
    import base64
    
    m = 'QabC-+50'
    text = '2016-09-19 05:11'
    md5 = MD5.new()
    md5.update(text)
    # text = md5.hexdigest()
    cipher = DES.new(m, DES.MODE_ECB)
    text_temp = cipher.encrypt(text)
    print 'text_temp is ', text_temp
    final_str = base64.b64encode(text_temp)
    print final_str
    print final_str[2:8] + final_str[10:13]
    

    OUTPUT:

    Wm+DLy8m9G2BJnH2wvtKvA==
    +DLy8m2BJ
    

    So without the MD5 step,the java code and the python code have the some output.

    What does matter is the MD5 method in the java code, it is not a right way to get the MD5 value of a string.

    Code below contains the right way to get the string's MD5 value:

    import java.security.Key;
    import java.security.MessageDigest;
    import java.util.Base64;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    
    public class H {
        public static void main(String args[]){
            try{
            String strDefaultKey = "QabC-+50";
            Key key = new SecretKeySpec(strDefaultKey.getBytes("UTF-8"), "DES");
            Cipher encryptCipher = Cipher.getInstance("DES/ECB/NoPadding");
            encryptCipher.init(Cipher.ENCRYPT_MODE, key);
            String seed = "2016-09-19 05:11";
            String seedMd5 = MD5(seed);
            byte[] a = encryptCipher.doFinal(seedMd5.getBytes());
            byte[] encodeUrl = Base64.getEncoder().encode(a);
            String finalUrl = new String(encodeUrl);
            String finalResult = finalUrl.substring(2, 8) + finalUrl.substring(10, 13);
            System.out.println(finalUrl);
            System.out.println(finalResult);
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    
        static String MD5(String src) {
            MessageDigest md;
            try {
                md = MessageDigest.getInstance("MD5");
                StringBuffer deviceIDString = new StringBuffer(src);
                src = convertToHex(md.digest(deviceIDString.toString().getBytes()));
            } catch (Exception e) {
                src = "00000000000000000000000000000000";
            }
            return src;
        }
    
        private static String convertToHex(byte[] data) {
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < data.length; i++) {
                int halfbyte = (data[i] >>> 4) & 0x0F;
                int two_halfs = 0;
                do {
                    if ((0 <= halfbyte) && (halfbyte <= 9))
                        buf.append((char) ('0' + halfbyte));
                    else
                        buf.append((char) ('a' + (halfbyte - 10)));
                    halfbyte = data[i] & 0x0F;
                } while (two_halfs++ < 1);
            }
            return buf.toString();
        }
    }
    

    OUTPUT:

    c/C16RAE1fADZXNi2H0YlevNhuucGYYHGVQ7v0Eoo9w=
    C16RAEADZ
    

    Python Code:

    from Crypto.Hash import MD5
    from Crypto.Cipher import DES
    import base64
    
    m = 'QabC-+50'
    text = '2016-09-19 05:11'
    md5 = MD5.new()
    md5.update(text)
    text = md5.hexdigest()
    cipher = DES.new(m, DES.MODE_ECB)
    text_temp = cipher.encrypt(text)
    final_str = base64.b64encode(text_temp)
    print final_str
    print final_str[2:8] + final_str[10:13]
    

    OUTPUT:

    c/C16RAE1fADZXNi2H0YlevNhuucGYYHGVQ7v0Eoo9w=
    C16RAEADZ
    

    Now everything is ok! :)