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
via Sushil Sudhakaran
No comments:
Post a Comment