【发布时间】:2013-12-06 17:14:53
【问题描述】:
在 android 中我总是得到 IllegalBlockSizeException,数据在 nodejs 服务器中被加密,看起来像 (node.js: encrypting data that needs to be decrypted?):
var crypto = require('crypto');
console.log(crypto.getCiphers(), crypto.getHashes());
var algorithm = 'aes128'; // or any other algorithm supported by OpenSSL
var key = 'password';
var cipher = crypto.createCipher(algorithm, key);
var encrypted = cipher.update(data, 'utf8', 'binary') + cipher.final('binary');
fs.writeFile(file, encrypted, function (err) {
cb(err);
});
安卓代码:
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
来自文件的调用方法在输入流(is)中:
byte [] b = new byte[2000000];
is.read(b, 0, 2000000);
byte[] decryptedData = decrypt(key,"password".getBytes());
result = new String(decryptedData, "UTF8").split("\n");
android 代码的灵感来自:android encryption/decryption with AES 我不使用 SecureRandom 的 SecretKey 的一部分...这肯定是错误的,但我没有在 node.js 部分使用任何安全随机数。问题也可能与文件中的编码数据有关。
我通常在 nodejs 中生成一个文件,该文件由应用程序下载并存储在 sdcard 中,我不确定我是否应该真正关心这些数据,但如果将其加密会很酷,不是吗?: -)
非常感谢您的任何帮助或建议;-)
【问题讨论】:
-
您是否有理由不能为此使用TLS 这样的标准?
-
因为我想将文件存储在 sdcard 上的 android 设备中,用户可以轻松读取它
标签: android node.js cryptography encryption