0

I'm not a java coder but found this code to encrypt/decrypt file in java and it works ... I'm using blowfish ... but i'm open to use any algorithm that works on both

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class TestFileEncryption {

    private static final String ALGORITHM = "Blowfish";
    private static String keyString = "DesireSecretKey";

    public static void encrypt(File inputFile, File outputFile)
            throws Exception {
        doCrypto(Cipher.ENCRYPT_MODE, inputFile, outputFile);
        System.out.println("File encrypted successfully!");
    }

    public static void decrypt(File inputFile, File outputFile)
            throws Exception {
        doCrypto(Cipher.DECRYPT_MODE, inputFile, outputFile);
        System.out.println("File decrypted successfully!");
    }

    private static 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();

    }

    public static void main(String[] args) {

        File inputFile = new File("F:/java/movie.mp4");
        File encryptedFile = new File("F:/java/file.encrypted");

        File decryptedFile = new File("F:/java/javamovie.mp4");

        try {
            TestFileEncryption.encrypt(inputFile, encryptedFile);
            TestFileEncryption.decrypt(encryptedFile, decryptedFile);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

Now I want to decrypt file.encryted with node.js ... I'm using the crypto module ... but it gives me bad decrypt error

Here's the code I'm using to decrypt in node

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

var fs = require('fs');


// input file
var r = fs.createReadStream('file.encrypted');


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

// write file
var w = fs.createWriteStream('nodemovie2.mp4');

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

This is the error I get

Error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
    at Decipher._flush (crypto.js:135:28)
    at Decipher.<anonymous> (_stream_transform.js:118:12)
    at Object.onceWrapper (events.js:293:19)
    at emitNone (events.js:86:13)
    at Decipher.emit (events.js:188:7)
    at prefinish (_stream_writable.js:500:12)
    at finishMaybe (_stream_writable.js:508:7)
    at endWritable (_stream_writable.js:520:3)
    at Decipher.Writable.end (_stream_writable.js:485:5)
    at ReadStream.onend (_stream_readable.js:513:10)

Quite frustrated trying to figure out

I tried bf-cbc,bf-ecb,bf-cfb modes in node but no luck

  • Are you sure that crypto suports blowfish? Maybe try other api e.x https://www.npmjs.com/package/javascript-blowfish – Lemonov Jun 07 '17 at 09:27
  • I would prefer AES over Blowfish for file encryption. It is stronger crypto and probably better supported in modern libraries as well. – quinz Jun 07 '17 at 09:31
  • I tries AES as well ... it gets encryted and decrypted on java and node ... but node cant decrypt files encoded and in java and vice versa .... basically same issue – Sushil Sudhakaran Jun 07 '17 at 09:46
  • @SushilSudhakaran: Did you use a random IV? Make sure you use the same block cipher and IV on both sides. – quinz Jun 07 '17 at 10:58
  • can you please provide an example or tweak my code – Sushil Sudhakaran Jun 07 '17 at 11:00
  • check this https://stackoverflow.com/questions/45347282/file-decryption-not-working-in-node-when-encrypted-from-php problem will be in handling key not in hex – arun-r Aug 09 '17 at 12:50
  • Late but: to decrypt with a key in nodejs use `crypto.createDecipheriv` (not `createDecipher` which takes a _password_ and _derives_ the key and when applicable IV). Also Java defaults to ECB but nodejs/OpenSSL doesn't so you need to specify bf-ecb. See https://stackoverflow.com/questions/32054670/encrypt-string-in-java-decrypt-in-node-js-error-bad-decrypt and https://stackoverflow.com/questions/19698721/encrypt-in-node-and-decrypt-in-java . – dave_thompson_085 May 12 '18 at 10:14

0 Answers0