寻找一种在 Node 中加密数据(主要是字符串)并在 android 应用程序 (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 应用程序。
后端服务准备数据并将其发布给第三方。
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/