javax.crypto.BadPaddingException:解密错误-无法从文件中解密多个块
希望有人能给我指出正确的方向 我对加密(Java或其他语言)不是特别熟悉,但我说过我会帮助某人完成一项指定使用RSA加密/解密的任务 为了让自己熟悉正在努力实现的目标,我制作了一个小示例程序:javax.crypto.BadPaddingException:解密错误-无法从文件中解密多个块,java,encryption,rsa,badpaddingexception,Java,Encryption,Rsa,Badpaddingexception,希望有人能给我指出正确的方向 我对加密(Java或其他语言)不是特别熟悉,但我说过我会帮助某人完成一项指定使用RSA加密/解密的任务 为了让自己熟悉正在努力实现的目标,我制作了一个小示例程序: 生成6个随机数,并将它们放入逗号分隔的字符串中 加密数字序列 将加密的数字序列附加到文件中-如果程序运行多次,同一文件可以多次写入 读取加密文件-应获取所有已写入的序列 解密每个序列并打印出逗号分隔字符串的集合 这在第一次尝试时(即,当第一个序列写入空文件时)效果良好,并且返回正确的数字序列 当文件包
- 生成6个随机数,并将它们放入逗号分隔的字符串中
- 加密数字序列
- 将加密的数字序列附加到文件中-如果程序运行多次,同一文件可以多次写入
- 读取加密文件-应获取所有已写入的序列
- 解密每个序列并打印出逗号分隔字符串的集合
decrypt
例程因BadPaddingException
而崩溃。我在这里做了一个步骤,每个要解密的字节[]
(代表一个加密序列)都是128字节,所以这不像是不规则的字节数造成的问题
我知道RSA不再是推荐的解决方案,但这是我必须考虑的规范。
我也知道,RSA和BadPaddingException有很多问题需要解决,但我还没有遇到一个解决这个问题的问题
我的示例代码如下:
public class EncryptDecrypt {
private static Cipher cipher;
private static KeyPair keyPair;
public static void main(String[] args)
{
String[] numbers = getNumbers();
String numbersStr = String.join(", ", numbers);
System.out.println("Generated: " + numbersStr + ", NumBytes: " + numbersStr.getBytes().length);
byte[] encryptedNumbers = encrypt(numbersStr);
System.out.println("Encrypted: " + encryptedNumbers.toString() + ", NumBytes: " + encryptedNumbers.length);
writeToFile(encryptedNumbers);
System.out.println("Encrypted numbers written to data.txt");
ArrayList<byte[]> encryptedData = readFromFile();
System.out.println("Encrypted numbers read from data.txt, NumSequences: " + encryptedData.size());
ArrayList<String> decryptedSequences = decrypt(encryptedData);
for (int i = 0; i < decryptedSequences.size(); i++)
{
String sequence = decryptedSequences.get(i);
System.out.println("Sequence " + i + ": " + sequence);
}
}
private static String[] getNumbers()
{
String[] numbers = new String[6];
int min = 1;
int max = 60;
for (int i = 0; i < numbers.length; i++)
{
double number = (Math.random() * (max - min) + min);
numbers[i] = number >= 10 ? Integer.toString((int) number) : "0" + Integer.toString((int) number);
}
return numbers;
}
private static byte[] encrypt(String data)
{
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(2048);
keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
cipher.update(data.getBytes());
byte[] encrypted = cipher.doFinal();
return encrypted;
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
private static void writeToFile(byte[] data)
{
FileOutputStream fileOut = null;
try {
File file = new File("data.txt");
file.createNewFile();
fileOut = new FileOutputStream(file, true);
fileOut.write(data);
fileOut.flush();
fileOut.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fileOut.close();
} catch (IOException ex) {
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private static ArrayList<byte[]> readFromFile()
{
File file = new File("data.txt");
if (file.exists())
{
try {
ArrayList<byte[]> encryptedSequences = new ArrayList<>();
FileInputStream fileIn = new FileInputStream(file);
int blockSize = 128;
int numBlocks = fileIn.available() / blockSize;
for (int i = 0; i < numBlocks; i++)
{
byte[] encryptedSequence = new byte[blockSize];
fileIn.read(encryptedSequence);
encryptedSequences.add(encryptedSequence);
}
fileIn.close();
return encryptedSequences;
} catch (FileNotFoundException ex) {
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE, null, ex);
}
}
return null;
}
private static ArrayList<String> decrypt(ArrayList<byte[]> data)
{
try {
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
PrivateKey privateKey = keyPair.getPrivate();
cipher.init(Cipher.DECRYPT_MODE, privateKey);
ArrayList<String> decryptedStrings = new ArrayList<>();
for (byte[] sequence : data)
{
byte[] decryptedBytes = cipher.doFinal(sequence);
String decryptedString = new String(decryptedBytes);
decryptedStrings.add(decryptedString);
}
return decryptedStrings;
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
公共类加密解密{
私有静态密码;
私有静态密钥对;
公共静态void main(字符串[]args)
{
字符串[]numbers=getNumbers();
String numberstr=String.join(“,”,numbers);
System.out.println(“生成:“+numberstr+”,NumBytes:“+numberstr.getBytes().length”);
字节[]encryptedNumbers=加密(numbersStr);
System.out.println(“加密:“+encryptedNumbers.toString()+”,NumBytes:“+encryptedNumbers.length”);
写入文件(加密的数字);
System.out.println(“写入data.txt的加密数字”);
ArrayList encryptedData=readFromFile();
System.out.println(“从data.txt读取的加密数字,NumSequences:+encryptedData.size());
ArrayList decryptedSequences=解密(encryptedData);
对于(int i=0;i=10?整数。toString((int)数字):“0”+整数。toString((int)数字);
}
返回号码;
}
私有静态字节[]加密(字符串数据)
{
试一试{
KeyPairGenerator KeyPairGenerator=KeyPairGenerator.getInstance(“RSA”);
keyPairGenerator.initialize(2048);
keyPair=keyPairGenerator.generateKeyPair();
PublicKey PublicKey=keyPair.getPublic();
cipher=cipher.getInstance(“RSA/ECB/PKCS1Padding”);
cipher.init(cipher.ENCRYPT_模式,公钥);
cipher.update(data.getBytes());
byte[]encrypted=cipher.doFinal();
返回加密;
}catch(NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex){
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE,null,ex);
}
返回null;
}
私有静态void writeToFile(字节[]数据)
{
FileOutputStream fileOut=null;
试一试{
File File=新文件(“data.txt”);
createNewFile();
fileOut=新的FileOutputStream(file,true);
文件输出。写入(数据);
flush();
fileOut.close();
}捕获(FileNotFoundException ex){
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE,null,ex);
}捕获(IOEX异常){
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE,null,ex);
}最后{
试一试{
fileOut.close();
}捕获(IOEX异常){
Logger.getLogger(EncryptDecrypt.class.getName()).log(Level.SEVERE,null,ex);
}
}
}
私有静态ArrayList readFromFile()
{
File File=新文件(“data.txt”);
if(file.exists())
{
试一试{
ArrayList encryptedSequences=新的ArrayList();
FileInputStream fileIn=新的FileInputStream(文件);
int blockSize=128;
int numBlocks=fileIn.available()/blockSize;
对于(int i=0;i