pyDes implements Python version of DES symmetric encryption/decryption -- turn

https://my.oschina.net/leejun2005/blog/586451

I have a Java version of the DES encryption/decryption program at hand. I recently thought about refactoring it in Python to facilitate subsequent script parsing. After fiddling with pyDes twice, it seems to be very convenient. Order of magnitude, I did not do specific performance tests here, nor did I choose PyCrypto for three main reasons:

(1) PyCrypto depends on VC++9.0 under windows, which is troublesome to install 

(2) PyCrypto does not support padmode by default, and has strict requirements on the length of the secret key and deflection vector, and the scalability is very poor

(3) It is not used for brute force cracking, and the performance requirements are not high, so I don't care about performance, just use it ^ _ ^

Let’s go to the code directly below~

1. Java Edition

public class EncryptHelper {
    private static String strKey = "test_KEY", strParam = "test__IV";
    public static String desEncrypt(String source) throws Exception {
        if (source == null || source.length() == 0)
            return null;
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
        cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
        return StringHelper.toHexString(
                cipher.doFinal(source.getBytes("UTF-8"))).toUpperCase();
    }

    public static String desDecrypt(String source) throws Exception {
        if (source == null || source.length() == 0)
            return null;
        byte[] src = StringHelper.fromHexString(source);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
        cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
        byte[] retByte = cipher.doFinal(src);
        return new String(retByte);
    }

    public static void main(String[] args) throws Exception {
        System.out
        .println(EncryptHelper
                .desDecrypt("886f930f65f29132f6ace2683c448b5580d681a1fec3fc91cf3161f074b53b935d1c8fe80f99201077b36f923a42ac0e05cabe579308fda08d8ff463ad334677"));
        System.out.println(EncryptHelper.desEncrypt("https://mail.google.com/mail/u/0/#inbox/a1ed0e2f6f28e06b4361"));
    }
}

// result:
 // https://mail.google.com/mail/u/0/ # Inbox / a1ed0e2f6f28e06b4361
 // 886F930F65F29132F6ACE2683C448B5580D681A1FEC3FC91CF3161F074B53B935D1C8FE80F99201077B36F923A42AC0E05CABE579308FDA08D8FF463AD334677
2. Python version
# -*- coding:utf-8 -*-
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
from pyDes import *
from binascii import b2a_hex, a2b_hex

# For Python3, you'll need to use bytes, i.e.:
#   data = b"Please encrypt my data"
#   k = des(b"DESCRYPT", CBC, b"\0\0\0\0\0\0\0\0", pad=None, padmode=PAD_PKCS5)

data = 'https://mail.google.com/mail/u/0/#inbox/a1ed0e2f6f28e06b4361'
KEY = "test_KEY"    #密钥
IV = "test__IV" #Deflection      vector
# CBC mode encryption using DES symmetric encryption algorithm
k = des(KEY, CBC, IV, pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
print b2a_hex(d)
print "Decrypted: %r" % k.decrypt(d)

#result:
#886f930f65f29132f6ace2683c448b5580d681a1fec3fc91cf3161f074b53b935d1c8fe80f99201077b36f923a42ac0e05cabe579308fda08d8ff463ad334677
#Decrypted: ' 

#Or a single-line command as follows:
python -c 'from pyDes import *;from binascii import a2b_hex;import sys;print des("test_KEY", CBC, "test__IV", pad=None, padmode=PAD_PKCS5).decrypt(a2b_hex(sys.stdin.readlines()[0].strip()))' <<<886f930f65f29132f6ace2683c448b5580d681a1fec3fc91cf3161f074b53b935d1c8fe80f99201077b36f923a42ac0e05cabe579308fda08d8ff463ad334677
#https://mail.google.com/mail/u/0/#inbox/a1ed0e2f6f28e06b4361

 

3、Refer

[1] pyDes library implements python's des encryption

http://www.cnblogs.com/SunboyL/p/pyDes.html

[2] Cryptography and Python

http://lenciel.cn/2013/07/cryptography-and-python/

[3] Encryption and decryption tool class EncryptUtil

http://uule.iteye.com/blog/1925046

[4] implementing DES-X (mode CBC) using PyCrypto

https://gist.github.com/doublereedkurt/3921909

[5] python encryption and decryption of strings

http://www.simonzhang.net/?p=1903

[6] Data encryption algorithm

http://baike.baidu.com/view/878529.htm

[7] Asymmetric encryption algorithm

http://baike.baidu.com/view/1490349.htm

[8] Pycrypto and RSA cryptography technical notes

http://python.jobbole.com/84094/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324981630&siteId=291194637