java - IllegalBlockSizeException:使用填充密码解密时输入长度必须是 8 的倍数?
问题描述
对于此代码,我正在尝试解密消息。但我不断收到 IllegalBlockSizeException。
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class Decrypt {
static Cipher DESCipher;
static KeyGenerator KEY_generator;
static SecretKey myDesKey;
static byte[] textEncrypted;
public static byte[] Decrypt(byte[] input, String sk_string) {
try {
//Convert String to secret key
byte[] decodedKey = Base64.getDecoder().decode(sk_string);
SecretKeySpec originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "DES");
//init the Mode
DESCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the same cipher for decryption
DESCipher.init(Cipher.DECRYPT_MODE, originalKey);
// Decrypt the text
byte[] textDecrypted = DESCipher.doFinal(input);
//Return the text has been decrypted
System.out.println("Text Decryted : " + new String(textDecrypted));
return textDecrypted;
} catch (IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String sk = "3Kh/EMEHxBA=";
String input = "[B@7a92922";
byte[] gb= input.getBytes();
Decrypt(gb,sk);
}
}
我的加密类有效,但我无法正确填充输入以进行解密以取回消息。这是我的加密类:
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.io.*;
import java.util.Base64;
public class Encrypt {
static Cipher DESCipher;
static KeyGenerator KEY_generator;
static SecretKey myDesKey;
static byte[] textEncrypted;
public static byte[] Encrypt(String user_input, String sk_string) {
try {
//Convert String to secret key
byte[] decodedKey = Base64.getDecoder().decode(sk_string);
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "DES");
// Create the cipher type as DES
DESCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
// Initialize the cipher for encryption
DESCipher.init(Cipher.ENCRYPT_MODE, originalKey);
//Turn the String of user_input to the byte mode
byte[] text = user_input.getBytes();
// Encrypt the text -> Do final
textEncrypted = DESCipher.doFinal(text);
//Return the text;
//System.out.println(textEncrypted);
return textEncrypted;
} catch (NoSuchAlgorithmException | IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchPaddingException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String s= "hello everyone";
String sk= "3Kh/EMEHxBA=";
System.out.println(s);
System.out.println("Text encrypted : " + Encrypt(s,sk));
}
}
谁能告诉我出了什么问题以及我的代码应该如何修复它?我必须将类分开并将密钥、密码和明文作为字符串输入。谢谢!
解决方案
推荐阅读
- react-native - 如何将 Adobe XD 颜色转换为原生反应代码?
- c# - 如何使用 C# Windows 窗体在整个 Windows 应用程序中使用相同的工具条带控件
- python - 根据 Pandas 中的字符串迭代向列添加值
- vue.js - Vue.js - 通过在 v-model 中传递参数来更改嵌套属性
- javascript - 使用 Webpack 注入多个外部 javascript 文件
- android - Android Unity 控件
- apache-spark - 使用 spark 从 kerberized 集群连接 un kerberized Hive
- kotlin-exposed - Kotlin 在具有复合主键的表上公开 SQL 查询并选择给定 DTO 对象列表中包含的所有内容
- html - 自定义字体在某个 html/css 项目中不起作用
- python - Python3 - 无效的 JSON 有效负载