有人知道ruby是否实现了类似于PHP中的openssl_seal函数的东西吗我希望能够与运行this answer的修改实现的服务器进行交互php解决方案非常简单,如果我能为ruby找到同样的方法,那就太好了。
一年前有人在找同样的for python,但什么也没找到。

最佳答案

EVPúu 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来完成seal,但是创建安全会话密钥(本例中是RC4密钥)相当困难,所以你最好不要自己尝试。

09-10 10:18
查看更多