如果使用aes-256-cfb nodejs加密,则无法在Java中解密

时间:2019-04-30 07:02:09

标签: java node.js encryption aes

我在node.js中使用aes-256-cfb对文件流进行了加密,并使用AES / CFB8 / NoPadding在Java中将其读回,但这会损坏数据。

node.js

const aes = crypto.createCipher('aes-256-cfb', '1234567812345678');
aes.setAutoPadding(false);
//const aesd = crypto.createDecipher('aes-256-cfb', password);  If i decrypt //with this in node.js, i am able to get back the data.  just for a test
var readStream = fs.createReadStream('c:\\test\\orig.txt');
var wstream = fs.createWriteStream('c:\\test\\encrypted.txt');

  readStream 
    .pipe(cipher)  // encrypts with aes256
    .pipe(wstream) 

    .on('finish', function () {  // finished
        console.log('done writing encrypted file');
    });

java:

InputStream in = new FileInputStream(new File("c:\\test\\encrypted.txt"));
OutputStream out = new FileOuputStream("c:\\test\\decrypted.txt");
Cipher dcipher = Cipher.getInstance("AES/CFB8/NoPadding");
Key skeySpec = new SecretKeySpec("1234567812345678".getBytes(), "AES");

    byte[] ivd = new byte[dcipher.getBlockSize()];

    IvParameterSpec ivParams = new IvParameterSpec(ivd);

    dcipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParams);

    in = new CipherInputStream(in, dcipher);
    int numRead = 0;
    while ((numRead = in.read(buf)) >= 0) {
        out.write(buf, 0, numRead);
    }
    out.close();

有什么主意吗?

0 个答案:

没有答案