寻找一种方法来加密节点中的数据(主要是字符串)并在android应用程序(java)中解密.
已成功地在每个中完成(在节点中加密/解密,在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
Run Code Online (Sandbox Code Playgroud)
和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);
}
Run Code Online (Sandbox Code Playgroud)
原始密钥是这样创建的
private static byte[] getRawKey(String seed) throws Exception {
KeyGenerator kgen …Run Code Online (Sandbox Code Playgroud)