使用md5消息摘要和添加到python的desed/CBC/pkcs5p转换用于3DES加密的java代码

2024-10-06 12:09:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个工作的java代码,它用3DES加密加密密码短语-

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Main{

    public static void main(String[] args) throws Exception {
        String text = "aug@2019";
        String codedtext = new Main().encrypt(text);
        System.out.println(codedtext);
    }

    public String encrypt(String message) throws Exception {
        final MessageDigest md = MessageDigest.getInstance("md5");
        final byte[] digestOfPassword = md.digest("Lgp!kdao2020"
                .getBytes("utf-8"));
        final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        for (int j = 0, k = 16; j < 8;) {
            keyBytes[k++] = keyBytes[j++];
        }

        final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
        final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
        final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);

        final byte[] plainTextBytes = message.getBytes("utf-8");
        final byte[] cipherText = cipher.doFinal(plainTextBytes);
        final String encodedCipherText = new String(java.util.Base64.getMimeEncoder().encode(cipherText),
                         "UTF-8");
        return encodedCipherText;
    }

}

我希望在Python中使用相同的加密,因此从这两个代码生成的加密是相同的,这是我的Python代码

import base64
from Crypto.Cipher import AES
import pyDes
from Crypto import Random
import hashlib


def encrypt(message, passkey):
    hash_object = hashlib.md5(passkey.encode("utf-8"))
    digested_passkey = hash_object.digest()
    print(digested_passkey)
    key24 = digested_passkey[:24]
    des = pyDes.des(key24);
    message = message.encode('utf-8')
    message = message + (16 - len(message) % 16) * chr(16 - len(message) % 16)
    iv = Random.new().read(AES.block_size)
    cipher = AES.new(des, AES.MODE_CBC, iv)
    return base64.b64encode(iv + cipher.encrypt(message))

print(encrypt('aug@2019', 'Lgp!kdao2020'))

我收到一个错误->;ValueError:DES密钥大小无效。密钥的长度必须正好为8字节


Tags: importmessagenewstringjavabytecryptoutf
1条回答
网友
1楼 · 发布于 2024-10-06 12:09:56

两种代码之间有很多不同之处:

  • 在Java代码中,密钥是通过将16字节MD5散列与同一散列的前8字节连接起来生成的。在Python代码中,密钥生成似乎根本不起作用(从{: <24}更改为[:24]并不能使它变得更好)。最后的第二个更改digested_passkey[:24]digested_passkey相同,并且有效,因为PyCryptodome根据2密钥三重DES自动将密钥扩展到24字节
  • 在Python代码中,使用了两个库:pyDesPyCryptodome。此处只能应用一个库。关于PyCryptodomeAES。实际上,AES是比Triple DES更快/更现代的算法,但它与Java代码不同
  • Python代码还实现了填充,这是不必要的,因为PyCryptodome(与传统的PyCrypto)支持填充。此外,填充的实现是错误的,因为它使用了16字节的块大小,但Triple-DES的块大小是8字节
  • 在Java代码中,IV采用0向量,在Python代码中采用随机IV(这实际上是正确的,但与Java代码不同)
  • 在Java代码IV中,密文和密文没有连接,而在Python代码中,它们是连接的(这实际上是正确的,但与Java代码不同)

除此之外,所使用的算法是不安全的(MD5)或过时/缓慢的(三重DES),如评论中所述。同样,0-向量作为IV是完全不安全的

import base64
#from Crypto.Cipher import AES
from Crypto.Cipher import DES3
from Crypto.Util.Padding import pad
#import pyDes
#from Crypto import Random
import hashlib


def encrypt(message, passkey):
    
    #hash_object = hashlib.md5(passkey.encode("utf-8"))                       
    hash_object = hashlib.md5(passkey) 
    digested_passkey = hash_object.digest()
    print(digested_passkey)

    #key24 = "[:24]".format(digested_passkey) 
    key24 = digested_passkey + digested_passkey[0:8]        # Derive key as in Java
    
    #des = pyDes.des(key24);                                # Remove pyDes
    
    #message = message.encode('utf-8') 
    #message = message + (16 - len(message) % 16) * chr(16 - len(message) % 16) 
    message = pad(message, 8)                               # Use padding from PyCryptodome       
    
    #iv = Random.new().read(AES.block_size)                 # For Java code compliance: Use 0-IV
    iv = bytes.fromhex('0000000000000000')

    #cipher = AES.new(des, AES.MODE_CBC, iv)                # For Java code compliance: Use TripleDES
    cipher = DES3.new(key24, DES3.MODE_CBC, iv)
    
    #return base64.b64encode(iv + cipher.encrypt(message))  # For Java code compliance: Don't concatenate IV and ciphertext
    return base64.b64encode(cipher.encrypt(message)) 

#print(encrypt('aug@2019', 'Lgp!kdao2020'))                 # Better: Pass binary data
print(encrypt(b'aug@2019', b'Lgp!kdao2020'))

它以Java代码(7B0aNUwOU1ECqKqnIZs6mQ==)的形式给出结果

相关问题 更多 >