2

I'm uploading file on my web site (created using JSP). I want to used encryption while user uploading the file and decrypt in his PC when he needs back his file. I know that I have to send him password, salt, & iv. I will send it through mail. But how to encrypt it at the time of uploading and decrypt it in user PC I don't know.

I have given here AES encryption and decryption code.

encrytion

public class AESFileEncryption {
    public static void main(String[] args) throws Exception {

        // file to be encrypted
        FileInputStream inFile = new FileInputStream("C:\\Users\\gholv\\Desktop\\techpaper.pdf");

        // encrypted file
        FileOutputStream outFile = new FileOutputStream("C:\\Users\\gholv\\Desktop\\techpaper.aes");

        // password to encrypt the file
        String password = "javapapers";

        // password, iv and salt should be transferred to the other end
        // in a secure manner

        // salt is used for encoding
        // writing it to a file
        // salt should be transferred to the recipient securely
        // for decryption
        byte[] salt = new byte[8];
        SecureRandom secureRandom = new SecureRandom();
        secureRandom.nextBytes(salt);
        System.out.println(salt);
        FileOutputStream saltOutFile = new FileOutputStream("C:\\Users\\gholv\\Desktop\\techpapersalt.enc");
        saltOutFile.write(salt);
        saltOutFile.close();

        SecretKeyFactory factory = SecretKeyFactory
                .getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536,
                128);
        SecretKey secretKey = factory.generateSecret(keySpec);
        SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");

        //
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        AlgorithmParameters params = cipher.getParameters();

        // iv adds randomness to the text and just makes the mechanism more
        // secure
        // used while initializing the cipher
        // file to store the iv
        FileOutputStream ivOutFile = new FileOutputStream("C:\\Users\\gholv\\Desktop\\techpaperiv.enc");
        byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
        System.out.println(iv);
        ivOutFile.write(iv);
        ivOutFile.close();

        //file encryption
        byte[] input = new byte[64];
        int bytesRead;

        while ((bytesRead = inFile.read(input)) != -1) {
            byte[] output = cipher.update(input, 0, bytesRead);
            if (output != null)
                outFile.write(output);
        }

        byte[] output = cipher.doFinal();
        if (output != null)
            outFile.write(output);

        inFile.close();
        outFile.flush();
        outFile.close();

        System.out.println("File Encrypted.");

    }
}

decryption

public class AESFileDecryption {
    public static void main(String[] args) throws Exception {

        String password = "javapapers";

        // reading the salt
        // user should have secure mechanism to transfer the
        // salt, iv and password to the recipient
        FileInputStream saltFis = new FileInputStream("C:\\Users\\gholv\\Desktop\\techpapersalt.enc");
        byte[] salt = new byte[8];
        saltFis.read(salt);
        saltFis.close();

        // reading the iv
        FileInputStream ivFis = new FileInputStream("C:\\Users\\gholv\\Desktop\\techpaperiv.enc");
        byte[] iv = new byte[16];
        ivFis.read(iv);
        ivFis.close();

        SecretKeyFactory factory = SecretKeyFactory
                .getInstance("PBKDF2WithHmacSHA1");
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, 65536,
                128);
        SecretKey tmp = factory.generateSecret(keySpec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");

        // file decryption
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
        FileInputStream fis = new FileInputStream("C:\\Users\\gholv\\Desktop\\techpaper.aes");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\gholv\\Desktop\\plainfile_decrypted.pdf");
        byte[] in = new byte[64];
        int read;
        while ((read = fis.read(in)) != -1) {
            byte[] output = cipher.update(in, 0, read);
            if (output != null)
                fos.write(output);
        }

        byte[] output = cipher.doFinal();
        if (output != null)
            fos.write(output);
        fis.close();
        fos.flush();
        fos.close();
        System.out.println("File Decrypted.");
    }
}
  • Please shorten to relevant code –  Apr 29 '18 at 20:39
  • You could simplify all this by using `CiphsrInput/OutputStreams`. What technology is the client using? – user207421 Apr 30 '18 at 08:54
  • It may be late to answer. Does your client upload file through web front-end or mobile app ? for web front-end , there are JavaScript packages which implement AES algorithm. e.g. [crypto-js](https://stackoverflow.com/questions/70938059/aes-encryption-in-javascript) , for Android / iOS there should be relevant libraries to use. – Ham Oct 08 '22 at 16:44

0 Answers0