使用 AES/ECB/PKSC5 加密,将 Java 迁移到 Python

chr*_*kar 3 python java encryption aes

我的任务是在一个项目上实施 AES 加密。参考代码是用 Java 编写的 - 它需要转换为 Python。在整理我的笔记写一个 SO 问题时,我不小心偶然发现了答案!希望其他人觉得这很有用,我将在这里提及我的笔记作为“分享您的知识”类型的问题。

要求是使用具有给定密钥的 AES 加密消息。这是参考代码(Java)的简化外观,

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Encoder;


public class EncryptAES {

    private static String toHexString(byte[] data) {        
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < data.length; ++i) {
            String s = Integer.toHexString(data[i] & 0XFF);
            buf.append((s.length() == 1) ? ("0" + s) : s);
        }
        return buf.toString();
    }

    public static String encrypt(String input, String key) {
        byte[] crypted = null;
        try {
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, skey);
            crypted = cipher.doFinal(input.getBytes());
            final String encryptedString = toHexString(Base64.encodeBase64(crypted));
            return encryptedString;
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return new String(new BASE64Encoder().encode(crypted));
    }

    public static void main(String[] args) {
        String key = args[0];
        String plaintext = args[1];
        System.out.println("KEY = " + key);
        System.out.println("PLAINTEXT = " + plaintext);
        System.out.println("CIPHER = " + EncryptAES.encrypt(plaintext, key));
    }
}
Run Code Online (Sandbox Code Playgroud)

如果将上述内容另存为“EncryptAES.java”并将库文件保存commons-codec-1.7.jar在同一目录中,则可以使用以下命令对其进行编译,

$ javac EncryptAES.java -cp commons-codec-1.7.jar
Run Code Online (Sandbox Code Playgroud)

这是运行程序几次时的输出,

$ java -cp "commons-codec-1.7.jar:." EncryptAES ddddffffeeeerrrr message
KEY = ddddffffeeeerrrr
MESSAGE = message
CRYPTO = 397a59594d35524e6b6a463253706f41467668646b773d3d
$

$ java -cp "commons-codec-1.7.jar:." EncryptAES qqqqwwwweeeerrrr ThisIsAVeryImportantMessage
KEY = qqqqwwwweeeerrrr
PLAINTEXT = ThisIsAVeryImportantMessage
CIPHER = 56536a384d667736756b595a394e396b6d504d736231444673375250736d5639596f637072792f6e4b424d3d
$
Run Code Online (Sandbox Code Playgroud)

环顾四周,我找到了 Python Crypto 库。这是我必须复制上述输出的早期尝试之一,

#!/usr/bin/python

import sys
from Crypto.Cipher import AES

if __name__ == '__main__':
    key = sys.argv[1]
    plaintext = sys.argv[2]
    print 'KEY = ' + key
    print 'PLAINTEXT = ' + plaintext

    encobj = AES.new(key, AES.MODE_ECB)
    ciphertext = encobj.encrypt(plaintext)
    print 'CIPHER = ' + ciphertext.encode('hex') 
Run Code Online (Sandbox Code Playgroud)

这并不能完全得到我需要的东西。相反,我收到一条关于输入字符串长度需要为 16 的倍数的错误消息。这让我进入下一次尝试,

#!/usr/bin/python

import sys
from Crypto.Cipher import AES

# ref: https://gist.github.com/crmccreary/5610068
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 
unpad = lambda s : s[0:-ord(s[-1])]

class AESCipher:
    def __init__( self, key ):
        """
        Requires hex encoded param as a key
        """
        self.key = key.decode("hex")

    def encrypt( self, raw ):
        """
        Returns hex encoded encrypted value!
        """
        raw = pad(raw)
        cipher = AES.new(self.key, AES.MODE_ECB)
        return cipher.encrypt(raw).encode("hex")


if __name__ == '__main__':
    key = sys.argv[1]
    plaintext = sys.argv[2]
    print 'KEY = ' + key
    print 'PLAINTEXT = ' + plaintext

    # ref: http://stackoverflow.com/a/16882092
    hex_key = "".join("{:02x}".format(ord(c)) for c in key)

    encryptor = AESCipher(hex_key)
    ciphertext = encryptor.encrypt(plaintext)
    print 'CIPHER = ' + ciphertext
Run Code Online (Sandbox Code Playgroud)

老实说,我不确定输出结果如何,

$ python EncryptAES2.py ddddffffeeeerrrr message
KEY = ddddffffeeeerrrr
PLAINTEXT = message
CIPHER = f7361833944d9231764a9a0016f85d93
$
Run Code Online (Sandbox Code Playgroud)

我尝试了很多东西——不同的加密模式、博客、SO 问题,并放弃了自己寻找解决方案。正是在这一点上,我决定收集我的笔记并在这里提出一个问题。现在,如果我不列出我的尝试就没有什么意义了,所以我开始将它们组织在一个文件夹中并标记它们EncryptAES.pyEncryptAES2.py......等。

chr*_*kar 6

当我准备清单时,灵感突然袭来,在我最后一次尝试时,我决定以十六进制重新格式化输出。令我惊喜的是,它奏效了!这是中奖代码,

#!/usr/bin/python

import sys
import base64
from Crypto.Cipher import AES

# ref: http://stackoverflow.com/a/12525165
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS) 

class AESCipher:
    def __init__( self, key ):
        self.key = key

    def encrypt( self, raw ):
        raw = pad(raw)
        cipher = AES.new(self.key, AES.MODE_ECB)
        return base64.b64encode(cipher.encrypt(raw)) 


if __name__ == '__main__':
    key = sys.argv[1]
    plaintext = sys.argv[2]
    print 'KEY = ' + key
    print 'PLAINTEXT = ' + plaintext

    encryptor = AESCipher(key)
    ciphertext = encryptor.encrypt(plaintext)

    hex_ciphertext = "".join("{:02x}".format(ord(c)) for c in ciphertext)

    print 'CIPHER = ' + hex_ciphertext
Run Code Online (Sandbox Code Playgroud)

作为参考,这里是我在 Java 示例中使用的早期输入的输出,

$ python EncryptAES3.py ddddffffeeeerrrr message
KEY = ddddffffeeeerrrr
PLAINTEXT = message
CIPHER = 397a59594d35524e6b6a463253706f41467668646b773d3d
$

$ python EncryptAES3.py qqqqwwwweeeerrrr ThisIsAVeryImportantMessage
KEY = qqqqwwwweeeerrrr
PLAINTEXT = ThisIsAVeryImportantMessage
CIPHER = 56536a384d667736756b595a394e396b6d504d736231444673375250736d5639596f637072792f6e4b424d3d
$
Run Code Online (Sandbox Code Playgroud)

获得这个解决方案让我进行了大量的反复试验。如果有更严格的方法将 Java 转换为 Python,我很乐意听到!