使用Blowfish在Java中解密 [英] Decrypting in Java with Blowfish
问题描述
Hullo,
我正在使用Blowfish在Java中加密和解密。
加密工作正常,但解密失败。
这是我的解密代码:
String encryptedString = ...;
String decryptptedString = null;
SecretKeySpec key = new SecretKeySpec(myKey.getBytes(),Blowfish);
密码密码;
try {
cipher = Cipher.getInstance(Blowfish);
cipher.init(Cipher.DECRYPT_MODE,key);
byte [] decryptpted = cipher.doFinal(encryptedString.getBytes());
decryptptedString = new String(解密,Charset.forName(UTF-8));
} [捕获异常...]
我收到一个例外:
异常。 javax.crypto.IllegalBlockSizeException:使用填充密码进行解密时,输入长度必须是8的倍数。
你能告诉我如何使其简单的工作?谢谢。
我输入的输入来自我的加密Java代码,Base64中的+编码,我在将其解密到Base64之前就已经被解密了。 / p>
现在我有解决方案!
首先, Unicode的问题,所以我把ISO-8859-1无处不在。 包括Base64编码和解码。
然后,我以下是我的Java代码,它适用于Blowfish解密:
String encryptedString = ...;
String decryptptedString = null;
SecretKeySpec key = new SecretKeySpec(myKey.getBytes(CHARSET_ISO_8859_1),Blowfish);
密码密码;
try {
cipher = Cipher.getInstance(Blowfish / ECB / PKCS5Padding);
cipher.init(Cipher.DECRYPT_MODE,key);
byte [] decryptpted = cipher.doFinal(encryptedString.getBytes(CHARSET_ISO_8859_1));
decryptptedString = new String(解密,CHARSET_ISO_8859_1);
} [catch例外...]
请注意,我已经替换了
c code code code code code code code code code code code $但是,如果您对密钥执行相同操作,则失败。
密钥 myKey
必须是拉丁文-1字符串,8个字符。这使得128位的键。 Blowfish算法允许更大的密钥,但由于JRE中的美国出口限制,它们在Java中失败 - 美国允许加密,但不能比 NSA 可以中断。
CHARSET_ISO_8859_1
是一个常量定义如下所示:
final Charset CHARSET_ISO_8859_1 = Charset.forName(ISO-8859-1);
和 最后但并非最不重要的是,我已经相应地更改了加密Java代码。 Hullo, I am encrypting and decrypting in Java with Blowfish. The encryption works fine, but the decryption fails. Here is my Java code for decrypting : I get an exception : Can you tell me how to make this simply work ? Thank you. 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. Now I have the solution ! First, there were some problems with Unicode, so I have put ISO-8859-1 everywhere. Including in the Base64 encoding and decoding. Then, I have juggled with the variants. Here is my Java code which works for Blowfish decryption : Note that I have replaced The key The And Last but not least, I have changed my encryption Java code accordingly. 这篇关于使用Blowfish在Java中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Charset
是
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 … ]
Exception. javax.crypto.IllegalBlockSizeException: Input length must be multiple of 8 when decrypting with padded cipher
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 … ]
"Blowfish"
with "Blowfish/ECB/PKCS5Padding"
for getting the Cipher
instance, but, if you do the same for the key, it fails.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
is a constant defined like this :final Charset CHARSET_ISO_8859_1 = Charset.forName("ISO-8859-1");
Charset
is java.nio.charset.Charset
.