使用Blowfish解密Java [英] Decrypting in Java with Blowfish

查看:338
本文介绍了使用Blowfish解密Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hullo,

我使用Blowfish在Java中加密和解密。

I am encrypting and decrypting in Java with Blowfish.

加密工作正常,但是解密失败。

The encryption works fine, but the decryption fails.

这是我的解密Java代码:

Here is my Java code for decrypting :

String encryptedString = … ;
String decryptedString = null;
SecretKeySpec key = new SecretKeySpec(myKey.getBytes(), "Blowfish");
Cipher cipher;
try {
    cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decrypted = cipher.doFinal(encryptedString.getBytes());
    decryptedString = new String(decrypted, Charset.forName("UTF-8"));
} [ catch Exceptions … ]

我得到一个例外:

异常。 javax.crypto.IllegalBlockSizeException:使用padded cipher解密时,输入长度必须为8的倍数

你能告诉我如何使这个简单的工作?谢谢。

Can you tell me how to make this simply work ? Thank you.

我提供的输入来自我的加密Java代码,Base64中的+编码,并且在将其解密到此解密操作之前从Base64解码。 / p>

The input I give comes from my encryption Java code, + encoding in Base64, and I decode it from Base64 just before giving it to this decrypting operation.

推荐答案

现在我有解决方案!

Unicode的问题,所以我把ISO-8859-1无处不在。 包括Base64编码和解码。

First, there were some problems with Unicode, so I have put ISO-8859-1 everywhere. Including in the Base64 encoding and decoding.

然后,我

这是我的Java代码,适用于Blowfish解密:

Here is my Java code which works for Blowfish decryption :

String encryptedString = … ;
String decryptedString = null;
SecretKeySpec key = new SecretKeySpec(myKey.getBytes(CHARSET_ISO_8859_1), "Blowfish");
Cipher cipher;
try {
    cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key);
    byte[] decrypted = cipher.doFinal(encryptedString.getBytes(CHARSET_ISO_8859_1));
    decryptedString = new String(decrypted, CHARSET_ISO_8859_1);
} [ catch Exceptions … ]


$ b <用于获取 Cipher 实例的Blowfish和Blowfish / ECB / PKCS5Padding

Note that I have replaced "Blowfish" with "Blowfish/ECB/PKCS5Padding" for getting the Cipher instance, but, if you do the same for the key, it fails.

myKey 必须是拉丁语-1个8个字符的字符串。这使得128位的密钥。 Blowfish算法允许更大的密钥,但是它们在Java中失败,因为美国在JRE中的出口限制 - 美国允许加密,但不强于

The key myKey has to be a Latin-1 string of 8 characters. This makes a key of 128 bits. The Blowfish algorithm allows bigger keys, but they fail in Java because of the USA export restriction in the JRE — the USA allow encryption but not stronger than what the NSA can break.

CHARSET_ISO_8859_1 是一个常量,定义如下:这个:

The CHARSET_ISO_8859_1 is a constant defined like this :

final Charset CHARSET_ISO_8859_1 = Charset.forName("ISO-8859-1");

并且 Charset java.nio.charset.Charset

最后但并非最不重要的,我已经改变了我的加密Java代码。

Last but not least, I have changed my encryption Java code accordingly.

这篇关于使用Blowfish解密Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆