用Java加密文件&在Node中解密

时间:2017-06-07 08:41:26

标签: java node.js encryption cryptography blowfish

我不是一个java编码器,但发现这个代码在java中加密/解密文件并且它有效...我正在使用blowfish ...但是我可以使用任何适用于两者的算法

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();
        }


    }
}

现在我想用node.js解密file.encryted ......我正在使用加密模块...但是它给了我不好的解密错误

这是我用来在节点

中解密的代码
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);

这是我得到的错误

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)

非常沮丧地想弄清楚

我尝试了节点中的bf-cbc,bf-ecb,bf-cfb模式,但没有运气

0 个答案:

没有答案