简体   繁体   English

如何使用openssl解密Java-DES加密的消息?

[英]How do I decrypt a Java-DES-encrypted message using openssl?

Since the question title is self-explaining, please consider the following code: 由于问题标题是不言自明的,因此请考虑以下代码:

private static final String ALGORITHM = "DES";
private static final String MESSAGE = "This is an extremely secret message";
private static final byte[] key = { 0, 1, 2, 3, 4, 5, 6, 7 };

...

// Do encryption
final Cipher cipher = Cipher.getInstance(ALGORITHM);
cipher.init(ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
final byte[] encrypted = cipher.doFinal(MESSAGE.getBytes());

// Copy the encrypted message to a file
final InputStream inputStream = new ByteArrayInputStream(encrypted);
final OutputStream outputStream = new FileOutputStream("___SECRET");
copy(inputStream, outputStream);

Now I'm trying to decrypt the ___SECRET file with the following command: 现在,我尝试使用以下命令解密___SECRET文件:

openssl enc -d -des -K 0001020304050607 -iv 0 -in ___SECRET -out ___OPEN

which results in: 结果是:

bad decrypt
3073636028:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:539:

just decrypting the very first block (8 bytes) leaving the rest in trash state (OEM encoding): 只是解密了第一个块(8个字节),其余块处于垃圾状态(OEM编码):

This is MЕ$S6%@╢Т√°ў╝°╢]∙iь

What am I doing wrong and how do I decrypt the encrypted message using openssl ? 我在做什么错,如何使用openssl解密加密的消息?

On Java you use DES in ECB mode and on OpenSSL you use DES in CBC mode (IV is present). 在Java上,您以ECB模式使用DES,而在OpenSSL上,您以CBC模式使用DES(存在IV)。

This is a significant difference as in CBC the blocks are chained - therefore the first block is decrypted correctly but all following blocks are scrambled. 这是一个很大的不同,因为在CBC中,块是链接的-因此,第一个块被正确解密,但随后的所有块都被加密。

You can change the Java part to use "DES/CBC" mode instead and provide an IV or change the openssl part and use -des-ecb instead of -des . 您可以更改Java部分以改为使用“ DES / CBC”模式,并提供IV或更改openssl部分,并使用-des-ecb代替-des

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM