0

No result while decryption.

What i am doing is simple encryption and decryption. Encryption occured as per expected but get -1 byte from encrypted file. I search a lot but didnt file solution. Even not getting any error related to key, file, or something.

What i have done so far

  1. I change directory from app specific to external (same)
  2. My path doesnt contain spaces
  3. run on main and background thread no luck. And 3 days still cant find out my problem.

`

try {
     //ENCRYPT FILE
     File fileToEncrypt = new File(fileModel.getFilePath());
if (fileToEncrypt.exists()){
File encryptedFile = new File(fileHelper.getAppPhotosDirectory(), fileToEncrypt.getName());
if (encryptedFile.exists()) encryptedFile.delete();

                    FileInputStream fileInputStream = new FileInputStream(fileToEncrypt);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

                    int nextByte = fileInputStream.read();
                    while (nextByte != -1){
                        byteArrayOutputStream.write(nextByte);
                        nextByte = fileInputStream.read();
                    }

                    KeyGenParameterSpec keyGenParameterSpec = new KeyGenParameterSpec.Builder(
                            MasterKey.DEFAULT_MASTER_KEY_ALIAS,
                            KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT)
                            .setBlockModes(KeyProperties.BLOCK_MODE_GCM)
                            .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
                            .setKeySize(256).build();

MasterKey masterKey = new MasterKey.Builder(requireContext().getApplicationContext(), MasterKey.DEFAULT_MASTER_KEY_ALIAS)
.setKeyGenParameterSpec(keyGenParameterSpec).build();
EncryptedFile encryptedFileStreamer = new EncryptedFile.Builder(
requireContext().getApplicationContext(),
encryptedFile,
masterKey,
EncryptedFile.FileEncryptionScheme.AES256_GCM_HKDF_4KB).build();
OutputStream outputStream = encryptedFileStreamer.openFileOutput();
outputStream.write(byteArrayOutputStream.toByteArray());

                    outputStream.flush();
                    outputStream.close();
                    fileInputStream.close();
                    byteArrayOutputStream.flush();
                    byteArrayOutputStream.close();

if (encryptedFile.exists()){
FileModel encryptedFileModel = new FileModel(encryptedFile.getName(), encryptedFile.getPath());
                        Log.d("analyze", "File encrypted in: "+encryptedFileModel.getFilePath());
                        Log.d("analyze", "Now decrypting file again"+encryptedFileModel.getFilePath());

                        String pathToDecrypt = fileHelper.getAppTempDirectory();


                        InputStream inputStream = encryptedFileStreamer.openFileInput();
                        Log.d("analyze", "First byte from file: "+inputStream.read());


                    }else{
                        Log.d("analyze", "File not encrypted");
                    }
                }

                //DECRYPT FILE
            } catch (GeneralSecurityException | IOException generalSecurityException){
                generalSecurityException.printStackTrace();
            }

`

1 Answers1

0

As you can read in image encryption with jetpack EncryptedFile security

you should not use inputStream.read().

Instead use a byte array buffer to read: inputStream.read(buffer)

blackapps
  • 8,011
  • 2
  • 11
  • 25