我正在尝试在android中加密我的消息并在节点js服务器中解密。
Android代码:
SecretKeySpec secretkeyspec = new SecretKeySpec("password".getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretkeyspec);
byte[] encoded = cipher.doFinal(s.getBytes());
System.out.println(Arrays.toString(encoded));
节点JS代码:
var crypto = require('crypto');
var CIPHER_ALGORITHM = 'aes-128-cbc';
var key = 'password';
var ivBuffer = new Buffer(16);
ivBuffer.fill(0);
var cipher = crypto.createCipheriv(CIPHER_ALGORITHM, new Buffer(key, 'utf-8'), ivBuffer);
var encryptedBuffer = cipher.update(plainText, 'utf-8');
var fBuf = new Int8Array(Buffer.concat([encryptedBuffer, cipher.final()]));
console.log(fBuf);
当我尝试打印缓冲区时,我在android和节点js之间获得了不同的值。
节点缓冲区:
[26, 116, 2, -56, -70, 121, -44, 66, 101, 84, -46, 127, -70, -42, 67, 31, 124, -104, -24, 88, 74, 4, -22, -70, -39, 48, -120, -21, 37, -15, -24, -30]
Android缓冲区:
[26,116,2,-56,-70,121,-44,66,101,84,-46,127,-70,-42,67,31,-92,97,16, - 101,-45,-68,108,89,-125,17,-71,53,2,-13,31,-79]
有人可以告诉Android默认的AES等效节点js解密代码。
答案 0 :(得分:1)
我终于找到了答案。
var cipher = crypto.createCipheriv(CIPHER_ALGORITHM, new Buffer(key, 'utf-8'), '');
var encryptedBuffer = cipher.update(plainText, 'utf-8');
var finalEncryptedBuffer = new Int8Array(Buffer.concat([encryptedBuffer, cipher.final()]));
console.log(encodeBytes(finalEncryptedBuffer));

答案 1 :(得分:0)
确保两种实现之间的操作模式(例如,CBC)和填充(例如,PKCS5)匹配。