0

i am trying to use AES GCM for decrypting files (small and large files) on Android. I have tried to do it like this tutorial https://proandroiddev.com/security-best-practices-symmetric-encryption-with-aes-in-java-7616beaaade9

But there is only written i should use CiperInputstream if i want to encrypt big files.

So this is the code now

SecureRandom secureRandom = new SecureRandom();
byte[] key = new byte[16];
secureRandom.nextBytes(key);

SecretKey secretKey = SecretKeySpec(key, "AES");
byte[] iv = new byte[12]; //NEVER REUSE THIS IV WITH SAME KEY
secureRandom.nextBytes(iv);

final Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv); //128 bit auth tag length
cipher.init(Cipher.ENCRYPT_MODE, secretKey, parameterSpec);
byte[] cipherText = cipher.doFinal(plainText);

ByteBuffer byteBuffer = ByteBuffer.allocate(4 + iv.length + cipherText.length);
byteBuffer.putInt(iv.length);
byteBuffer.put(iv);
byteBuffer.put(cipherText);
byte[] cipherMessage = byteBuffer.array();

So now i have created the CiperInputStream, how to encrypt, add the IV and at least save the file?

FileInputStream inputStream = new FileInputStream(plainText);
CipherInputStream cis = new CipherInputStream(inputStream, cipherEnc);

Furthermore i want to upload the file, if it is possible without saving the ecnrypted file first. Currently i upload unencrypted files to a webserver with a given link (seafile). I just pass the path of the file to a function and then upload it with OkHttpClient

0 Answers0