- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
首先,让我首先声明我不是密码学家,我也不是很擅长编写 C 代码,所以如果这个问题的答案很明显或已经回答,请原谅。我正在开发消息程序,无法在目标平台上使用 TLS。因此,我需要找到一种使用对称预共享 key 密码(如 AES)来加密每条消息的方法。
我正在寻找一种在一端的 mbedtls 程序(例如 aescrypt2)和另一端的 nodejs 程序之间加密和解密数据的方法。 Mbedtls,以前称为 polarssl,是一个为嵌入式设备提供加密的库。源代码中包含一些示例程序,如 aescrypt2、rsaencrypt、ecdsa 和 crypt_and_hash。
Aescrypt2当生成的加密数据也使用 aescrypt2 解密时工作正常,但我似乎无法使用 aescrypt 加密数据以使用 nodejs crypto 或与此相关的任何其他程序(包括 openssl)进行解密。例如:
echo 'this is a test message' >test.txt
aescrypt 0 test.txt test.out hex:E76B2413958B00E193
aescrypt 1 test.out test.denc hex:E76B2413958B00E193
cat test.denc
this is a test message
使用 openssl:
openssl enc -in out.test -out outfile.txt -d -aes256 -k E76B2413958B00E193
bad magic number
一些当前不起作用的示例 Node 代码
var crypto = require('crypto');
var AESCrypt = {};
AESCrypt.decrypt = function(cryptkey, iv, encryptdata) {
encryptdata = new Buffer(encryptdata, 'base64').toString('binary');
var decipher = crypto.createDecipheriv('aes-256-cbc', cryptkey, iv),
decoded = decipher.update(encryptdata, 'binary', 'utf8');
decoded += decipher.final('utf8');
return decoded;
}
AESCrypt.encrypt = function(cryptkey, iv, cleardata) {
var encipher = crypto.createCipheriv('aes-256-cbc', cryptkey, iv),
encryptdata = encipher.update(cleardata, 'utf8', 'binary');
encryptdata += encipher.final('binary');
encode_encryptdata = new Buffer(encryptdata, 'binary').toString('base64');
return encode_encryptdata;
}
var cryptkey = crypto.createHash('sha256').update('Nixnogen').digest(),
iv = 'a2xhcgAAAAAAAAAA',
buf = "Here is some data for the encrypt", // 32 chars
enc = AESCrypt.encrypt(cryptkey, iv, buf);
var dec = AESCrypt.decrypt(cryptkey, iv, enc);
console.warn("encrypt length: ", enc.length);
console.warn("encrypt in Base64:", enc);
console.warn("decrypt all: " + dec);
这每次都会导致错误或垃圾文本。我也尝试过调整各种东西。
我已经尝试了一百种不同的方法,包括使用 -pass pass:password
参数都无济于事。使用 nodejs,我要么得到错误的解密错误,要么在解密时出现乱码。我尝试按照网上的许多教程进行操作,例如 this一,来自this thread的建议,以及我能找到的所有其他东西。我读到过不同的加密程序使用不同的标准,因此并不总是保证跨平台/程序/语言的兼容性,但我想有人曾经陷入过这种困境并且知道解决方案?
我如何使用 nodejs 解密由 aescrypt2(或类似程序)加密的数据?我只能使用系统 exec 调用并让 Node 执行 aescrypt2 来解密/加密数据来使其工作,这并不理想,因为它会大大减慢速度。我愿意使用与 aescrypt2 不同的程序。唯一的要求是必须在Linux上运行,不能使用openssl libs(因为它们在目标系统上不支持),程序应该小而简单,由于空间限制,最重要的是,加密/解密需要与nodejs兼容。任何帮助将不胜感激。
最佳答案
How would I, using nodejs, decrypt data encrypted by aescrypt2 (or a program like it)?
很遗憾,没有比解密文件时 aescrypt2 所做的完全相同的事情更好的答案了。您已经自己链接到源代码,因此只需在 node.js 中执行与在解密分支中的 C 中执行的相同步骤即可。
首先,熟悉layout of the file containing the encrypted data :
/*
* The encrypted file must be structured as follows:
*
* 00 .. 15 Initialization Vector
* 16 .. 31 AES Encrypted Block #1
* ..
* N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
* (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
*/
因此您需要从文件中提取 IV、加密 block 和 HMAC,而不是像您尝试使用 openssl 那样尝试解密整个文件(您的 openssl 示例也没有使用正确的 IV,而是尝试导出它从提供的 key - 阅读 man page )。
接下来,拿到 key 。 actual key used加密/解密不是在命令行上提供的,而是使用 SHA256 使用命令行上传递的 key 对 IV 进行 8192 次散列迭代。
最后,他们使用 AES-256-ECB(您的 openssl 和 node.js 示例使用 CBC!)每 16 个字节和 XOR the result 进行解密。与前面的 16 个字节(IV 用于前 16 个字节)。
也许还有更多,我只是列出了我在阅读 aescrypt2.c 代码时看到的最明显的东西。
所以我的建议是:尝试在 node.js 中编写相同的逻辑,并尝试为相应的 mbedtls 对应项找到 node.js 加密调用。
我不是加密专家,但我敢打赌,aescrypt 实现有太多让人感觉很复杂的步骤(比如生成实际使用的 key ),因为他们知道如何进行加密并且只是以正确的方式进行。
关于c - 与 NodeJs 和 mbedtls 一起工作的加密程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37369020/
我正在使用框架的对象编写一个用于加密/解密的简单库。方法如下: public static byte[] Encrypt(byte[] key, byte[] vector, byte[] input
据我所知,RIM Crypto API 似乎只提供用于对称加密 (3Des) 的 PKCS5 填充模式。我正在使用 JDE 4.6.0。 我正在尝试为黑莓应用程序提供密码学,该应用程序需要与已经使用标
我已经获得了用于加密的 Java 实现,但遗憾的是我们是一家 .net 商店,我无法将 Java 整合到我们的解决方案中。可悲的是,我也不是 Java 专家,所以我已经为此苦苦挣扎了几天,我想我终于可
我正在尝试使用 KMS 和 AWS 加密 SDK 加密数据。查看 AWS documentation 中提供的示例,似乎没有地方可以明确设置数据 key 。 我找到了 EncryptionMateri
我目前有一个用于为我的网站制作哈希的代码,该代码使用 SALT 进行哈希处理,因此密码是不可逆的...... 目前它是 100% 为我的网站工作,它是使用 ASP.NET(C#) 编码的 这是我的代码
我想要做的是在 javascript 中生成一个 key 对,并在 PHP 中使用这些加密,然后用 JS 解密。 我在附加的代码中有两个问题 它不会从装甲文本块重新加载私钥 并且它不会解密 PHP 加
在进行密码哈希时,我有以下 node.js 代码。 body.password = covid@19 salt = "hello@world" body.passwordhex = crypto.cr
我想知道的是在配置文件中加密连接字符串的明确方法。以下是我的问题: 使用机器级加密,访问我的服务器的任何人都不能编写一个小的 .Net 程序来读取连接字符串的内容吗? 如果我将我的应用程序部署到企业环
我知道 rsync 可以在文件传输期间启用/禁用 ssh 加密协议(protocol)。那么,如果 ssh 加密协议(protocol)被禁用了,是不是意味着 rsync 根本不做任何加密呢? 另外,
脚本必须搜索网页内的字符串。但该脚本不应显示它正在搜索的字符串。我的意思是搜索字符串应该采用加密格式或任何其他格式。但如果没有该搜索字符串,则不应显示网页或应在页面上显示错误。 我要开发一个插件。如果
我正在尝试加密 MySQL 上的某些字段。我正在使用 TPC-DS 的 v2.8 版本,并尝试在客户地址表的某些列上使用 AES。知道如何加密字段的所有行吗?我尝试使用 UPDATE customer
我需要一个简单的 javascript 函数,它允许我使用 key 加密 textarea 数据( key 是存储为散列 session 变量的用户密码,由 PHP 打印到字段中) 我基本上希望在用户
如何在 JavaScript 中散列/加密字符串值?我需要一种机制来隐藏 localStorage/cookie 中的一些数据吗? 这与安全问题有关,但我想为我的数据提供一些保护。 最佳答案 有很多
我有一个程序,其中数据库的密码由远程用户设置。该程序将用户名和密码保存到 xml 文件中的加密字符串中,否则应该是人类可读的。现在,这工作正常,我使用带有 key 的 C# DES 加密,它被加密和解
Kotlin 中是否有任何关于椭圆曲线加密的信息? 用于生成 key 对和加密、解密消息。 关于这个主题的信息很少甚至没有。 例如,我想实现 ECC P-521 椭圆曲线。 是否可以在 Kotlin
所以我知道 MD5 在技术上是新应用程序的禁忌,但我随机想到了这个: 自 md5($password); 不安全,不会 md5(md5($password)) 是更好的选择?我使用它的次数越多,它会变
我一直在努力使用 crypto_secretbox_easy() 在 libsodium 中加密/解密一些数据| .我似乎找不到关于使用的任何好的文档。 我想从用户那里获取密码,用它来以某种方式制作
我正在做一个加密项目 视频,我对这个程序有几个问题。 我用命令转码mp4至HLS与 ts段持续时间约为 10 秒。 首先,我需要使用数据库中的 key 加密这些视频。然而, 我不知道是否使用 ffmp
我有一个加密/复制保护问题。 我正在为使用加密狗的公司编写应用程序。请不要告诉我软件保护是没有用的,或者我应该让它自由地飞向空中,或者我花任何时间这样做都是浪费;这不是关于软件保护有效性的哲学问题,更
我对 有一个疑问VIM 加密 key . 我有一个文本文件,我使用加密该文件 :X 现在,加密 key 的存储位置(路径)。 无论是存储在单独的文件中还是存储在文本文件本身中。 如果我打开文件,它会询
我是一名优秀的程序员,十分优秀!