0

I want to encrypt the audio file by android and decrypt it by backend sails js. I developed the program for that but I got error in sails js like

error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt

enter image description here

This is my source code for encrypt the audio file in android

     final String ALGORITHM = "blowfish";
     String keyString = "DesireSecretKey";

    private void encrypt(String file) throws Exception {

        File extStore = Environment.getExternalStorageDirectory();
        File inputFile = new File(file);
        File encryptedFile = new 
        File(extStore+"/Movies/encryptAudio.amr");
        doCrypto(Cipher.ENCRYPT_MODE, inputFile, encryptedFile);
     }



     private  void doCrypto(int cipherMode, File inputFile,
                                 File outputFile) throws Exception {

        Key secretKey = new 
        SecretKeySpec(keyString.getBytes(),ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(cipherMode, secretKey);

        FileInputStream inputStream = new FileInputStream(inputFile);
        byte[] inputBytes = new byte[(int) inputFile.length()];
        inputStream.read(inputBytes);

        byte[] outputBytes = cipher.doFinal(inputBytes);

        FileOutputStream outputStream = new 
        FileOutputStream(outputFile);
        outputStream.write(outputBytes);

        inputStream.close();
        outputStream.close();

    }

I installed the crypto , fs libraries in sails js backend by following command npm install crypto npm install fs

This is my source code for decrypt the audio file in sails js

   function decrypt() {
        var crypto = require('crypto'),
        algorithm = 'blowfish',
        password = 'DesireSecretKey';

        var fs = require('fs');

      // input file
        var r = fs.createReadStream(config.UPLOAD_FILES_PATH 
        +'/encryptAudio.amr');

     var decrypt = crypto.createDecipher(algorithm, password,"");

   // write file
    var w = fs.createWriteStream(config.AUDIO_PATH+'decryptAudio.amr');

   // start pipe
      r.pipe(decrypt).pipe(w);
  }

Encryption is working properly & i can get the encrypted audio file.But the issue is i couldn't get the decrypted audio file by sails js. Can you identify the issue?

Bumblebee
  • 21
  • 5

1 Answers1

0

maybe the answer to this question might help as you are using Java's library for cryptography this should pretty much guide you in right direction. Encrypt with Node.js Crypto module and decrypt with Java (in Android app) I would have mentioned this in comments section but I do not have enough reputation points to comment. This is opposite of what you are trying to achieve but still it could help you to analyze your issue.

TGW
  • 805
  • 10
  • 27