通过android加密音频文件并通过后端sails js解密它

时间:2017-09-22 04:10:35

标签: audio encryption sails.js node-crypto

我想通过android加密音频文件并通过后端sails js解密。我为此开发了程序,但我在sails js中遇到错误

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

enter image description here

这是我在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();

    }

我通过以下命令在sails js后端安装了crypto,fs库         npm安装加密         npm install fs

这是我在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);
  }

加密工作正常&我可以得到加密的音频文件。但问题是我无法通过sails js获得解密的音频文件。你能确定这个问题吗?

1 个答案:

答案 0 :(得分:0)

也许这个问题的答案可能有所帮助,因为您使用Java的库进行加密,这几乎可以指导您正确的方向。 Encrypt with Node.js Crypto module and decrypt with Java (in Android app) 我会在评论部分提到这一点,但我没有足够的声誉点来评论。 这与您要实现的目标相反,但它仍然可以帮助您分析您的问题。