这个C#RSACryptoServiceProvider方法的ruby等价物是什么

时间:2016-04-21 07:49:19

标签: ruby rsa rsacryptoserviceprovider

int dwKeySize = 1024;
rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
rsaCryptoServiceProvider.FromXmlString(strxmlString);
byte[] bytes = Encoding.UTF8.GetBytes(strInPut);
byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(bytes, false);
return Convert.ToBase64String(encryptedBytes);

c#已生成public RSA key,并希望在ruby中加密与上述算法相当的数据。

1 个答案:

答案 0 :(得分:0)

查看openssl模块OpenSSL (Ruby(2.0.0)。有 也是如何使用它的例子。

实施例

  1. 加密'你好,世界!':

    require 'openssl'
    require 'base64'
    key = OpenSSL::PKey::RSA.new(1024)
    plain_text = "Hello, World!"
    enc_base64_text = Base64.encode64(key.public_encrypt(plain_text))
    # => "\x91W\xF9\v\xDEU|\x9E\xC6\xA6\x1C\xCF\xF5Jc\x88'\x81\xE6\xF8mpe \xF1\x86 tt\rs\xD0OC\xC0\xA8J\xF5}\x90\x89\x8A\xFE\x9Fn1\xA5\xEB5\x95\xC7DH\xF4.\xED\x17\x8C]G\xBF\xEBB\x98'\xFA\xCAGp\xFFJ\xEA*\x1A\a\xC0\x96\x88i\x9F\xCA\x80+u\t\xB6Hk\xAC\xCD\xBA\xD2\xBE\xC0o\xF0z\xA9\xCF\xA6\xFA\xC75\xDF\x9D\xE9~\xD4\xEB\xC3\xDEJ\xBEU\xBA\xD6\xEE?\x17?\xAE\xDC\xC3\xD7\xACR\xD5"
    
  2. 解密:

    key.private_decrypt(Base64.decode64(enc_base64_text))
    # => "Hello, World!"