有人知道ruby是否实现了类似于PHP中的openssl_seal函数吗?我希望能够与运行修改后的this answer实现的服务器进行交互。PHP解决方案非常简单,如果我能为Ruby找到可以做同样事情的东西,那就太好了。
一年前有人在找同样的for python,但什么也没找到。
发布于 2011-03-07 10:22:36
EVP_Seal使用RSA进行简单的包装,因此您可以使用OpenSSL功能手动完成包装。
下面是一个PHP脚本,它使用了1个证书:
<?php
$pubkey = openssl_pkey_get_public(file_get_contents('selfcert.pem'));
$message = 'hello,world';
$cipher_text = NULL;
$keys = NULL;
openssl_seal($message, $cipher_text, $keys, array($pubkey));
$file = fopen('wrapped.bin', 'wb');
fwrite($file, $keys[0]);
fclose($file);
$file = fopen('data.bin', 'wb');
fwrite($file, $cipher_text);
fclose($file);
?>和一个解封它的Ruby脚本:
require 'openssl'
wrapped = File.read('wrapped.bin')
cipher_text = File.read('data.bin')
privkey = OpenSSL::PKey::RSA.new(File.read('privkey.pem'))
key = privkey.private_decrypt(wrapped)
cipher = OpenSSL::Cipher.new('rc4')
cipher.decrypt
cipher.key = key
p cipher.update(cipher_text) + cipher.final你也可以用Ruby做“密封”,但是创建安全的会话密钥(在这个例子中是RC4密钥)是相当困难的,所以你最好不要自己去做。
发布于 2011-03-07 06:41:48
PHP文档并不清楚openssl_seal到底做了什么,但是它的源代码非常简短(在ext/openssl/openssl.c中查找PHP_FUNCTION(openssl_seal),在线点击http://svn.php.net/viewvc/php/php-src/trunk/ext/openssl/openssl.c?view=markup)。
它是一个EVP_SealIinit()、EVP_Seal_Update()、EVP_Seal_Final()序列的包装器(参见http://www.openssl.org/docs/crypto/EVP_SealInit.html)。据我所知,这些OpenSSL函数不是由OpenSSL Ruby模块公开的,也不是由openssl命令行工具公开的,所以如果你真的想走这条路,我猜你只有两个选择:
发布于 2012-02-24 17:20:15
信封加密做两件事:
如果这是在Ruby到OpenSSL的绑定中就好了,但是你也可以自己来做。从本质上讲,你要做的是:
此时,要将CT解密为PT,您需要同时使用K1和IV。我们需要以安全的方式传输K1:
消费者现在需要逆转这一过程。最终目标是将密文(CT)转换回明文(PT)。为此,我们需要撤消使用K1完成的对称加密。
解密CT
下面这样的代码应该可以解决这个问题:https://gist.github.com/1899731
https://stackoverflow.com/questions/5212213
复制相似问题