Atul Thakre
Atul Thakre

Reputation: 500

Need to confirm its 256 bit file encryption/Decryption or not?

We have written 256-bit file encryption/decryption code in Java. It's working fine and it properly encrypts and decrypts file with same secret key +salt combination.

import java.io.IOException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.File;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.KeySpec;
import java.util.Base64;
import javax.crypto.KeyGenerator;


public class CryptoUtils
{
    private static final String ALGORITHM = "AES";
    private static final String TRANSFORMATION = "AES";
    private static final String SECRET_KEY = "ncrm7tDy0Y137YNT4+6/0szt4weszTlqj/iPLySCTKY=";
    private static final String SALT = "ssshhhhhhhhhhh!!!!";
    
    private static void doCrypto256(final int opmode, final File file, final File file2) {       
        try {
            byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
            IvParameterSpec ivspec = new IvParameterSpec(iv);
            SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");            
            KeySpec spec = new PBEKeySpec (SECRET_KEY.toCharArray(), SALT.getBytes(), 100, 256);
            SecretKey tmp = factory.generateSecret(spec);            
            SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");           
            Cipher instance = Cipher.getInstance("AES/CBC/PKCS5Padding");
            instance.init(opmode, secret,ivspec);             
            final FileInputStream fileInputStream = new FileInputStream(file);
            final byte[] array = new byte[(int)file.length()];
            fileInputStream.read(array);
            final byte[] doFinal = instance.doFinal(array);
            final FileOutputStream fileOutputStream = new FileOutputStream(file2);
            fileOutputStream.write(doFinal);
            fileInputStream.close();
            fileOutputStream.close();
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }  
    
    public static void main(String[] args) {     
       File inputFile = new File("D:/JaveEncryptDecrypt/sample.pdf");            
        try {         
          doCrypto256(1, inputFile, inputFile);  //encryption on 256 
         doCrypto256(2, inputFile, inputFile);  //Decryption on 256 
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
    }
}

I have generated an AES-256 secret key, which I have defined inside the class. I want to know if it is 256-bit encryption/decryption or not? If I need to change anything, please let me know.

Thanks

Upvotes: 4

Views: 292

Answers (0)

Related Questions