gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 00:07:15 25 4
gpt4 key购买 nike

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

要求是使用带有给定 key 的 AES 加密消息。以下是引用代码的简化 View (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));
}
}

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

$ javac EncryptAES.java -cp commons-codec-1.7.jar

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

$ 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
$

环顾四周,我找到了 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')

这并不能完全满足我的需要。相反,我收到一条错误消息,提示输入字符串的长度必须是 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

说实话,我不太确定输出是什么,

$ python EncryptAES2.py ddddffffeeeerrrr message
KEY = ddddffffeeeerrrr
PLAINTEXT = message
CIPHER = f7361833944d9231764a9a0016f85d93
$

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

最佳答案

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

#!/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

这里的引用是我在 Java 示例中使用的早期输入的输出,

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

$ python EncryptAES3.py qqqqwwwweeeerrrr ThisIsAVeryImportantMessage
KEY = qqqqwwwweeeerrrr
PLAINTEXT = ThisIsAVeryImportantMessage
CIPHER = 56536a384d667736756b595a394e396b6d504d736231444673375250736d5639596f637072792f6e4b424d3d
$

得到这个解决方案让我经历了很多试错。如果有更规范的方法将 Java 转换为 Python,我很想听听!

关于java - 使用 AES/ECB/PKSC5 加密,将 Java 迁移到 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35203086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com