- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一些遗留数据,在 Node 中加密,我需要在 Ruby 中解密。
问题是,数据是使用现已弃用的方法加密的,createCipher
.此方法使用密码来执行加密。它已被 createCipheriv
取代,这需要一个 32 字节的 key 和一个 16 字节的初始化向量。
在 Node 中,我可以使用类似弃用的 createDecipher
解密字符串,它也接受密码。但是,我不知道 Ruby 中有任何等效方法,这是有道理的,因为这些方法现在已知是不安全的并且已被弃用。
Ruby 的 OpenSSL AES 密码正确地需要一个 32 字节的 key 和 16 字节的 IV,就像较新的 createCipheriv
一样,但我没有这些,只有我在 createCipher 中使用的原始密码
。
如何在 Ruby 中获得类似于 createCipher
/createDecipher
的行为?
具体来说,给定以下 JavaScript...
const crypto = require('crypto');
const cipher = crypto.createCipher("aes-256-cbc", 'secret password');
cipherText = cipher.update('dummy string', "utf8", "hex") + cipher.final("hex");
console.log(cipherText); // f3051259f83c7ca2ac012a396c4c0848
...我如何使用密码'secret password'
解密字符串'f3051259f83c7ca2ac012a396c4c0848'
并返回输入值'dummy string '
在 Ruby 中?
最佳答案
底层算法 aes256 需要 32 位 key 和 16 位初始化向量,而不管 createCipher
等更高级别抽象提供的接口(interface)如何。在幕后,它必须生成一个 32 位 key 和 16 位初始化向量,从输入密码派生。
createCipher
通过对输入密码重复进行 MD5 散列来生成一个字节数组,然后从中提取 32 字节的 key 和 16 字节的初始化向量。它已被弃用,因为这不是生成随机 IV 的安全方法(实际上它生成的 IV 是完全确定的)。
这发生在 Node 的 C 源代码中,在 /deps/openssl/openssl/crypto/evp/evp_key.c 中.
这个函数有很多参数,但是当用于为 createCipher('aes-256-cbc', 'password')
生成 key /iv 时,会调用此方法 here , 具有以下参数:
const EVP_CIPHER *type, // "aes-256-cbc"
const EVP_MD *md, // EVP_md5()
const unsigned char *salt, // null, unused
const unsigned char *data, // "password"
int datal, // 8, length of "password"
int count, // 1, unused
unsigned char *key, // output key buffer
unsigned char *iv, // output iv buffer
此方法必须生成 48 个字节(32 个用于 key ,16 个用于 IV),它首先运行 hash = md5(password)
,生成 16 个字节。
现在,对于后续的每组 16 个字节,它将前 16 个字节与 password
连接起来,并再次对其进行哈希处理:hash = md5(hash + password)
.
有效字节...
md5(password)
生成md5(md5(password) + password)
生成md5(md5(md5(password) + password) + password)
生成。我们可以在 Ruby 中实现相同的功能来导出正确的 key 和 IV 以通过密码解密给定的字符串:
require 'digest'
require 'openssl'
def decrypt(cipher_text, password)
bytes = [ Digest::MD5.digest(password) ]
bytes << Digest::MD5.digest(bytes.last + password)
bytes << Digest::MD5.digest(bytes.last + password)
bytes = bytes.join
cipher = OpenSSL::Cipher.new('aes-256-cbc')
cipher.decrypt
cipher.key = bytes[0...32]
cipher.iv = bytes[32...48]
# OpenSSL deals in raw bytes, not the hex-encoded representation
# 'f3051259f83c7ca2ac012a396c4c0848' => "\xF3\x05\x12Y\xF8<|\xA2\xAC\x01*9lL\bH"
cipher_text = cipher_text.unpack('a2' * (cipher_text.length / 2)).map(&:hex).pack('c*')
cipher.update(cipher_text) + cipher.final
end
decrypt('f3051259f83c7ca2ac012a396c4c0848', 'secret password') # => 'dummy string'
为了完整起见,这里是等效的encrypt
方法,它将允许在 Ruby 中加密的数据在 Node 的弃用 createDecipher
中解密:
def encrypt(plain_text, password)
cipher = OpenSSL::Cipher.new('aes-256-cbc')
cipher.encrypt
bytes = [ Digest::MD5.digest(password) ]
bytes << Digest::MD5.digest(bytes.last + password)
bytes << Digest::MD5.digest(bytes.last + password)
bytes = bytes.join
cipher.key = bytes[0...32]
cipher.iv = bytes[32...48]
cipher_text = cipher.update(plain_text) + cipher.final
# Produce a hex representation
# "\xF3\x05\x12Y\xF8<|\xA2\xAC\x01*9lL\bH" => 'f3051259f83c7ca2ac012a396c4c0848'
cipher_text.unpack('C*').map { |byte| byte.to_s(16) }.map { |str| str.rjust(2, "0") }.join
end
encrypt('dummy string', 'secret password') # => "f3051259f83c7ca2ac012a396c4c0848"
关于node.js - 如何在 Ruby 中解密由 Node 已弃用的 createCipher 加密的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57068729/
我有一个 C# 应用程序调用 Java 网络服务来验证用户密码。我想让 C# 应用程序加密密码,然后让 Java Web 服务解密密码。我已经完成了 Java 端的代码(解密代码),但我无法找出 C#
我正在使用以下代码在使用 openssl 的 Windows 中使用 C 加密和解密二进制数据。如您所见,在这两个函数中,我都知道纯文本的大小。有什么方法可以在不知道纯文本大小的情况下解密消息? #i
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有一个非常恼人的问题,Java中使用RSA算法对字符串进行不可靠的加密和解密。它似乎只能在大约 35% 的时间内工作,而且我不明白为什么它有时能工作,有时却不能。这是我写的一些测试代码,试图验证加密
我已经设法编写了用于文件加密/解密的函数。但它非常慢,尤其是随着文件大小的增加。例如几MB长的音频/视频文件 我几乎浏览了所有帖子来改进它,并尝试更改算法。如果有任何更改可以帮助我提高性能,请帮助我。
我正在尝试让我的转置密码发挥作用。 每当我将加密方法得到的密文输入解密方法时,我应该得到原始的明文......但事实并非如此...... 我做错了什么? 感谢您的帮助! public String E
我正在使用密码来加密和解密消息: public String encrypt(String string) throws InvalidKeyException, IllegalBlockSizeEx
我有一个在 MySQL 中存储数据的 spring-mvc 堆栈。其中一些数据需要保护,所以我想我应该加密它。由于我以后可能需要使用这些数据(信用卡、SSN 等),所以我需要对其进行解密。我认为这排除
作为一名SEOER,都想了解百度算法,通过算法原理来找到捷径的优化方案,那么今天我把研究多年的百度算法原理解密给大家,可能不是最好的,但是我可以给大家保证,这些都是非常实际的。希望给SEOER带来一
我试图找到一种技术来加密和解密程序中的文件,而无需将密码硬编码到程序中,也无需向用户询问密码。 如果我也可以从我正在编写的另一个程序中解密文件,那就太好了。 到目前为止,我还没有多少运气找到一种看起来
有没有一种方法可以使用作为字符串参数传递给程序的私钥而不是使用存储在机器上的证书来解密 PowerShell 中的 RSA?欢迎任何帮助,我的代码如下。 Function Decrypt-Asymme
通过问题Is it possible to use the Grails Jasypt plugin outside the GORM layer for simple String encrypti
我需要解密/加密我的域类中的几列,并且正在寻找有关如何做的信息。我已经找到了jasypt加密插件,但不幸的是它似乎与Grails 2.4不兼容。 我可能可以将一些东西拼凑在一起,但是想要确保Im遵循最
我需要有关声音文件加密/解密的帮助。我想在存储这个声音文件时加密一个声音文件,并在播放这个文件时解密它。我阅读了有关 java 中的加密/解密以及 java 中可用于此的大量示例代码。但这些程序不适用
我很感兴趣是否可以使用 Excel Visual Basic 和某些加密服务提供程序进行字符串加密/解密。 我找到了一个演练 Encrypting and Decrypting Strings in
我们正在使用加密/解密和UIIMAGE。如果我们在不保存到iphone画廊的情况下进行加密和解密以及UIIMAge,则可以正常工作,但是,如果我们进行加密,保存到画廊,将(加密的图像)加载到应用程序中
我正在做一个像这样的简单程序: package rsaexample; import java.io.*; import java.math.BigInteger; import java.secur
我发现这段代码返回给定字符串的校验和。 public static String getChecksum(String md5) { int counter = 0; while (c
我在 Java SE 和 Android 项目上使用相同的代码。在 Java 和 Android 中运行的应用程序连接到相同的 MQTT 代理并交换消息。消息使用 AES 进行加密/解密。我对 Jav
我想在 openssl/libcrypto 中使用 RSA 加密/解密一个长文件(我知道 AES 更好,但这只是为了比较)。我将输入文件分成大小为 numBlocks = inputFileLengt
我是一名优秀的程序员,十分优秀!