AES encryption and decryption all types of file (java)

how to modify this AES encryption code so that it can encrypt and decrypt any type of file (pdf, docx….), because when I decrypt a pdf file or other I don’t get the original file.

public class EncryptData { private Cipher cipher;  public EncryptData(File originalFile, File encrypted, SecretKeySpec secretKey, String cipherAlgorithm) throws IOException, GeneralSecurityException{     this.cipher = Cipher.getInstance(cipherAlgorithm);           encryptFile(getFileInBytes(originalFile), encrypted, secretKey); }  public void encryptFile(byte[] input, File output, SecretKeySpec key) throws IOException, GeneralSecurityException {     this.cipher.init(Cipher.ENCRYPT_MODE, key);     writeToFile(output, this.cipher.doFinal(input)); } 

}

public class StartEncryption {  public SecretKeySpec getSecretKey(String filename, String algorithm) throws IOException{     byte[] keyBytes = Files.readAllBytes(new File(filename).toPath());     return new SecretKeySpec(keyBytes, algorithm); }  public static void main(String[] args) throws IOException, GeneralSecurityException, Exception{     StartEncryption startEnc = new StartEncryption();          File originalFile = new File("file.docx");     File encryptedFile = new File("EncryptedFiles/encryptedFile");     new EncryptData(originalFile, encryptedFile, startEnc.getSecretKey("OneKey/secretKey", "AES"), "AES"); } 

}

decryption code AES java public class DecryptData { private Cipher cipher;  public DecryptData(File encryptedFileReceived, File decryptedFile, SecretKeySpec secretKey, String algorithm) throws IOException, GeneralSecurityException {     this.cipher = Cipher.getInstance(algorithm);     decryptFile(getFileInBytes(encryptedFileReceived), decryptedFile, secretKey); }  public void decryptFile(byte[] input, File output, SecretKeySpec key) throws IOException, GeneralSecurityException {     this.cipher.init(Cipher.DECRYPT_MODE, key);     writeToFile(output, this.cipher.doFinal(input)); }    }} 

public class StartDecryption {

public SecretKeySpec getSecretKey(String filename, String algorithm) throws IOException{     byte[] keyBytes = Files.readAllBytes(new File(filename).toPath());     return new SecretKeySpec(keyBytes, algorithm); }  public static void main(String[] args) throws IOException, GeneralSecurityException, Exception{     StartDecryption startEnc = new StartDecryption();          File encryptedFileReceived = new File("EncryptedFiles/encryptedFile");     File decryptedFile = new File("DecryptedFiles/decryptedFile");     new DecryptData(encryptedFileReceived, decryptedFile, startEnc.getSecretKey("DecryptedFiles/SecretKey", "AES"), "AES");      } 

}

Add Comment
1 Answer(s)

I changed the code, but it’s the same thing, I don’t get the original file.

public EncryptData(File originalFile, File encrypted, SecretKeySpec secretKey, String cipherAlgorithm) throws IOException, GeneralSecurityException{     this.cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");            encryptFile(getFileInBytes(originalFile), encrypted, secretKey); }  public void encryptFile(byte[] input, File output, SecretKeySpec key) throws IOException, GeneralSecurityException {               this.cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");     int ivSize = 16;     byte[] iv = new byte[ivSize];     SecureRandom random = new SecureRandom();     random.nextBytes(iv);       IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);     cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);     writeToFile(output, this.cipher.doFinal(input)); } 
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.