寻找一种在 Node 中加密数据(主要是字符串)并在安卓应用程序(java)中解密的方法。
在每一个中都已成功完成(在 Node 中加密/解密,在 java 中加密/解密),但似乎无法在它们之间工作。
可能我没有以相同的方式加密/解密,但是每种语言的每个库对相同的事物都有不同的名称...
任何帮助表示赞赏。
这里有一些代码: Node.js
var crypto = require('crypto')
var cipher = crypto.createCipher('aes-128-cbc','somepass')
var text = "uncle had a little farm"
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex')
//now crypted contains the hex representation of the ciphertext
和java
private static String decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec );
byte[] decrypted = cipher.doFinal(encrypted);
return new String(decrypted);
}
原始 key 是这样创建的
private static byte[] getRawKey(String seed) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] seedBytes = seed.getBytes()
sr.setSeed(seedBytes);
kgen.init(128, sr); // 192 and 256 bits may not be available
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return raw;
}
而加密的十六进制字符串被转换成这样的字节
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
最佳答案
谢谢大家。您的回答和评论为我指明了正确的方向,并且通过更多研究,我设法获得了一个工作原型(prototype)(粘贴在下面)。 事实证明, Node 的加密使用 MD5 对 key 进行哈希处理,而填充显然是使用 PKCS7Padding 完成的(通过反复试验得到了那个)
至于首先这样做的原因: 我有一个由三部分组成的应用程序: A. 后端服务 B. 第三方数据存储 C. 一个安卓应用作为客户端。
后端服务准备数据并将其发布给第三方。 android 应用获取和/或更新数据存储中的数据,服务可能会根据这些数据进行操作。
加密的需求是保持数据的私密性,即使是来自第三方提供商。
至于 key 管理 - 我想我可以让服务器在每个预先配置的时间段创建一个新 key ,用旧 key 对其进行加密并将其发布到数据存储中供客户端解密并开始使用,但它很友好对我的需要有点矫枉过正。
我也可以创建一个 key 对,并使用它每隔一段时间传输新的对称 key ,但这更加矫枉过正(更不用说工作了)
任何人,这是代码: 在 Node.js 上加密
var crypto = require('crypto')
var cipher = crypto.createCipher('aes-128-ecb','somepassword')
var text = "the big brown fox jumped over the fence"
var crypted = cipher.update(text,'utf-8','hex')
crypted += cipher.final('hex')
//now crypted contains the hex representation of the ciphertext
在 Java 上解密:
public static String decrypt(String seed, String encrypted) throws Exception {
byte[] keyb = seed.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(keyb);
SecretKeySpec skey = new SecretKeySpec(thedigest, "AES/ECB/PKCS7Padding");
Cipher dcipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
dcipher.init(Cipher.DECRYPT_MODE, skey);
byte[] clearbyte = dcipher.doFinal(toByte(encrypted));
return new String(clearbyte);
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
关于java - 使用 Node.js Crypto 模块加密并使用 Java 解密(在 Android 应用程序中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7787773/