C/C++ 中,modulus,privateExponent, pulbicExponent 如何转换成 RSA密钥
p.s: 以下内容均以 openssl 实现。
RSA 密钥的存储方式有很多,某些情况下,我们直接保存密钥的 modulus(n),publicExponent(e),privateExponent(d),使用的时候再将它们转换成 RSA 密钥。下面来讲讲在 C/C++ 环境中,如何把这几个大整数转换成 RSA 密钥来用。
根据 openssl 的文档(https://www.openssl.org/docs/man1.1.1/man3/RSA_set0_key.html),openssl提供了这样一个函数
这个参数里面的 n , e , q 对应 RSA 算法里面的参数,实际使用中,它们分别是
n --> modulus
e --> publicExponent
d --> privateExponent
其中,n和e组成了公钥,n和d组成私钥(取自 [RSA算法原理](https://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html)),那么,我们想用 modulus(n),publicExponent(e),privateExponent(q) 来生成一个RSA密钥使用的话,可以用下面这样简短的方式
其中,"the_modulus_hex_str" ,"the_publicexponent_hex_str","the_privateexponent_hex_str"是指对应的巨大整数的十六进制字符串。例如 (注:下面的数据隐去了一部分)
上面对应的巨大整数的十进制数为 (注:下面的数据隐去了一部分)
这样,我们就得到了一个RSA密钥。接下来可以使用这个密钥对一些数据加密/解密。
比如,使用 RSA/ECB/PKCS1Padding 填充方式,进行公钥加密,私钥解密例子
openssl 里面,公钥加密,私钥解密的函数原型是
同样的,还有私钥加密,公钥解密的函数原型是
---------------------
参考资料:
1. [rsa-ecb-pkcs1padding(openssl)](http://www.longshine.wang/%E7%BD%91%E7%BB%9C%E5%AE%89%E5%85%A8/2017/10/24/rsa-ecb-pkcs1padding/)
2. [openssl doc](https://www.openssl.org/docs/)
3. [RSA算法原理](https://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html)