我正在寫一個簡單的自定義加密器/解密器。
基本上,我只是取大文件的前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