我正在尝试将数据从 Java (Android) 发送到 Node.js 应用程序,但加密不起作用并且 Node.js 没有正确解密,而且我真的不知道我在做什么。
爪哇:
// Encrypt
byte[] input = jo.toString().getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(ENCRYPTION_KEY.getBytes("UTF-8"));
SecretKeySpec skc = new SecretKeySpec(thedigest, "AES/ECB/PKCS5Padding");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, skc);
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
String query = Base64.encodeToString(cipherText, Base64.DEFAULT);
query
然后发送到我们的服务器并且jo
是JSONObject
在 Node 中,我正在做:
var decipher = crypto.createDecipher('aes-128-ecb', encryption_key);
console.log("System: " + new Buffer(fullBuffer, "base64").toString("binary") );
chunks = []
chunks.push( decipher.update( new Buffer(fullBuffer, "base64").toString("binary") , 'hex', 'utf-8') );
chunks.push( decipher.final('utf-8') );
var txt = chunks.join("");
console.log("System: " + txt);
js = JSON.parse(txt);
console.log("System: " + js);
并且fullBuffer
接收到的 POST 数据是否正确传输