[英]Encrypting and Decrypting data through transport through Java to Node.js
我正在尝试将数据从Java(Android)发送到Node.js应用程序,除了加密不起作用且Node.js没有正确解密,我真的不知道我在做什么。
Java的:
// 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数据,它正确传输
加密和身份验证对于调试来说是残酷的,因为您所犯的任何错误都会导致整个输出随机化。 建议:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.