项目中遇到一个问题,已知一个C#项目中的公钥,形式如下:
//Public key XML string
string publickey = "<RSAKeyValue><Modulus>ma+lXgSUOx73xNJWgC18G6GKYlERmAnZ1BQQPyaZ4mlzbi3A+4"
+"FKTWsGlHqWFG1d6+Gvb1RW6NB3yB4d5CsJRkV3CEbotxT/3UC"
+"cV3txebo+w2BIqtv/qTRXgaxygR1i0=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
下面是我写的一个以此为公钥的加密程序:
require 'openssl'
require 'base64'
require 'xml/libxml'
def rsa_encrypt(public_key, message_string)
Base64.encode64(public_key.public_encrypt(message_string.encode('UTF-16LE'))).rstrip
end
# @param xml_string [Object]
# @return [Object]
def rsa_public_key(xml_string)
d = XML::Parser.string(xml_string).parse
m = Base64.decode64(d.find_first('Modulus').content).unpack('H*')
e = Base64.decode64(d.find_first('Exponent').content).unpack('H*')
pub_key = OpenSSL::PKey::RSA.new
# modulues
pub_key.n = OpenSSL::BN.new m[0].hex.to_s
# exponent
pub_key.e = OpenSSL::BN.new e[0].hex.to_s
# return Public Key
pub_key
end
但在调试中加密结果一直不对,这个问题出在哪里?请高手支招!