编译Cryptopp
编译过程没有什么特别,需要注意的是,如果使用dll版本的库,只包含IPS认证的算法,而编译静态链接库则包含全部算法,具体参考[1,2]。
- cryptopp - This builds the DLL. Please note that if you wish to use Crypto++ as a FIPS validated module, you must use a pre-built DLL that has undergone the FIPS validation process instead of building your own.
- dlltest - This builds a sample application that only uses the DLL.
- cryptest Non-DLL-Import Configuration - This builds the full static library along with a full test driver.
- cryptest DLL-Import Configuration - This builds a static library containing only algorithms not in the DLL, along with a full test driver that uses both the DLL and the static library.
本人编译的lib库大小为50多M。
RSA算法介绍
关于rsa算法的介绍可以参考阮一峰的两篇两篇博文,在文末[3,4]以列出。
在RSA算法中有几个需要注意的点,我罗列一下:
非对称加密:对称加密算法在加密和解密时使用的是同一个秘钥;而非对称加密算法需要两个密钥来进行加密和解密,这两个秘钥是公开密钥(public key,进行加密)和私有密钥(private key,用于解密)。
RSA用途:RSA加密算法除了用于少量数据加密之外,最主要的应用就是数字签名。
数字签名:包含3个步骤。详见[5]:
- 待发送消息(message)利用Hash函数,生成信息的摘要;
- 私钥加密摘要,生成”数字签名”(signature)
- 发送message+signature
- 公钥解密签名
- message重新生成摘要,与发送过来的摘要进行比较
公钥和私钥:公钥和私钥是成对的,它们互相解密。公钥加密,私钥解密。私钥数字签名,公钥验证。
RSA秘钥长度:cyptopp至少要求秘钥长度为1024;秘钥长度即为n值大小,即n=128byte
明文长度:一般应小于等于密钥长度(Bytes)-11,末尾采用填充。
解决长度限制:主要有2种方式
- 先用对称加密算法(AES/DES等)加密数据,然后用RSA公钥加密对称加密密钥,用RSA的私钥解密得到对称加密的密钥,然后完成反向操作得到明文。
- 分段进行RSA加密
示例代码
加密与解密
1 |
|
注意:
下面是生成的一组秘钥值,30开头的值是密钥的保存形式。查看源代码,可以发现其内部是使用默认使用ASN.1 DER编码进行持久化。
利用dumpans1,可以对DER编码进行解释。图片中的红框部分为密钥种的N值部分。
数字签名
1 |
|
参考
[1]http://www.codegur.me/37488545/how-to-build-crypto-5-6-2-in-msvc2013-for-qt
[2]http://cryptopp.com/wiki/Fips_dll
[3]http://www.ruanyifeng.com/blog/2013/06/rsa_algorithm_part_one.html
[4]http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html
[5]http://www.ruanyifeng.com/blog/2011/08/what_is_a_digital_signature.html
[6]http://www.office68.com/computer/10174.html