pyDes 實現 python 版的 DES 加密/解密

jopen 8年前發布 | 65K 次閱讀 安全相關

手頭有個 Java 版的 DES 加密/解密程序,最近想著將其 Python 重構下,方便后續腳本解析,搗鼓了兩下 pyDes 貌似很方便,不過據網上其他同學測試說 PyCrypto 性能要比 pyDes 高一個數量級,這里我沒有做具體性能測試,也沒有選用 PyCrypto 主要原因有三:

(1)PyCrypto 在 windows 下依賴 VC++9.0,安裝麻煩 

(2)PyCrypto 默認不支持 padmode,且對秘鑰以及偏轉向量長度有嚴格要求,擴展性很差

(3)不是用來搞暴力破解,性能要求不高,所以就不關注性能了,能用就行  ^ _ ^

下面直接上代碼吧~

1、Java 版

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

//結果:
//https://mail.google.com/mail/u/0/#inbox/a1ed0e2f6f28e06b4361
//886F930F65F29132F6ACE2683C448B5580D681A1FEC3FC91CF3161F074B53B935D1C8FE80F99201077B36F923A42AC0E05CABE579308FDA08D8FF463AD334677

2、Python 版

# -*- 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"     #偏轉向量
# 使用DES對稱加密算法的CBC模式加密
k = des(KEY, CBC, IV, pad=None, padmode=PAD_PKCS5)
d = k.encrypt(data)
print b2a_hex(d)
print "Decrypted: %r" % k.decrypt(d)

#結果:
#886f930f65f29132f6ace2683c448b5580d681a1fec3fc91cf3161f074b53b935d1c8fe80f99201077b36f923a42ac0e05cabe579308fda08d8ff463ad334677
#Decrypted: 'https://mail.google.com/mail/u/0/#inbox/a1ed0e2f6f28e06b4361'

3、Refer

[1] pyDes庫 實現python的des加密

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

[2] Cryptography and Python

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

[3] 加密解密工具類 EncryptUtil

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

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

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

[5] python 對字符串的加密解密

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

來自: http://my.oschina.net/leejun2005/blog/586451

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!