我正在写一个简单的自定义加密器/解密器。
基本上,我只是取大文件的前1024个字节并加密它。
我使用了一个RandomAccessFile,这样我就可以快速地加密和解密第一个1024字节。现在对于文件使用相同的AES算法的Bad Padding Exception?
,我面临的问题是,即使我使用的加密和解密的算法相同。
加密工作正常,但解密抛出一个javax.crypto.BadPaddingException:鉴于最终块未正确填充。
不管我多么搜索,我无法弄清楚什么是错的。对此的一些研究告诉我,由于UTF和base64等格式不同,填充不正确。但我不确定如果我读取这样一个大文件的第一个1024字节如何填充可能是不正确的&加密没有任何例外。另外我没有对字符串进行转换。
我已经提供了简单的评论,下面的代码
public class Encryptor {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES";
public void encrypt(String key, File inputFile, File outputFile) throws CryptoException {
doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
}
public void decrypt(String key, File inputFile, File outputFile) throws CryptoException {
doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
}
private void doCrypto(int cipherMode, String key, File inputFile, File outputFile) throws CryptoException {
try {
Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(cipherMode, secretKey);
byte[] inputBytes = new byte[16];
byte[] outputBytes = new byte[16];
//Open the file in read write mode
RandomAccessFile fileStore = new RandomAccessFile(inputFile, "rw");
fileStore.seek(0);
//encrypt first 1024bytes
int bytesRead = 0;
for(int ctr=0;bytesRead!= -1 && ctr<64 ;ctr++){
//get file pointer position
long prevPosition = fileStore.getFilePointer();
//read 16 bytes to array
bytesRead = fileStore.read(inputBytes);
//if successful, go move back pointer and overwrite these 16 bytes with encrypted bytes
if(bytesRead != 1){
outputBytes = cipher.doFinal(inputBytes);
fileStore.seek(prevPosition);
fileStore.write(outputBytes);
}
}
fileStore.close();
} catch (NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException
| IllegalBlockSizeException | IOException ex) {
throw new CryptoException(ex);
}
}
清洁和对点..(y)的 – user3041058