您好,感谢您阅读我的问题。我正在开发一个程序,在一个文件中逐行存储加密的字符串。但是我现在的程序出现了多个错误,此时我只是不知道如何修复它。
我收到错误给定最后一个块未正确填充,并且使用填充密码解密时输入长度必须是16的倍数
以下是代码:
package testing;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class encryptedTest{
ArrayList<String> wordList = new ArrayList<String>();
static String IV = "AAAAAAAAAAAAAAAA";
File txt = new File("encrypted.txt");
FileOutputStream fileOutput;
public encryptedTest(){
wordList.add("TestingTestingTesting"); //First line of encrypted text...If I add 4 characters I get an input error
wordList.add("Test"); // Second line of encrypted text
startEncryption();
}
public void startEncryption() {
try {
String key = "This is the Key ";
if(!txt.exists()){
encrypt(key);
}
decrypt(key);
encrypt(key);
} catch (Exception e) {
e.printStackTrace();
}
}
public void encrypt(String key) throws Exception {
try{
fileOutput = new FileOutputStream(txt);
}catch(Exception e){
e.printStackTrace();
}
for(int i = 0; i < wordList.size(); i++){
byte[] data = wordList.get(i).getBytes();
SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, spec, new IvParameterSpec(IV.getBytes()));
byte[] textEncrypted = cipher.doFinal(data);
fileOutput.write(textEncrypted, 0, textEncrypted.length);
if(!(i == wordList.size() - 1)){
fileOutput.write(System.getProperty("line.separator").getBytes());
}
}
}
public void decrypt(String key) throws Exception {
LineNumberReader lnr = new LineNumberReader(new FileReader(txt));
BufferedReader br = new BufferedReader(new FileReader(txt));
lnr.skip(Long.MAX_VALUE);
for(int i = 0; i < lnr.getLineNumber() + 1; i++) {
try{
FileInputStream fileInput = new FileInputStream(txt);
SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "AES");
String encryptedText = br.readLine();
byte[] bufferdReader = new byte[encryptedText.length()];
bufferdReader = new String(encryptedText).getBytes();
Cipher cipher;
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, spec, new IvParameterSpec(IV.getBytes()));
byte[] decryptedText = cipher.doFinal(bufferdReader);
System.out.println(new String(decryptedText));
}catch(Exception exc){
exc.printStackTrace();
}
}
lnr.close();
}
public static void main(String[] args){
new encryptedTest();
}
}
加密文本的第二行工作正常,它通过加密然后解密。但第一行是导致问题的原因是它当前没有正确填充错误,如果你在字符串上添加4个字符就会出现输入长度错误。
我已经遇到这个问题好几个小时了,只是试图让这段代码工作,但我尝试的任何东西似乎都无法工作,如果你还不知道我是非常新的加密/解密。任何提示或建议将不胜感激。谢谢!