Crypto++(二)数字签名算法DSA
来源:互联网 发布:pc记账软件 编辑:程序博客网 时间:2024/05/21 09:57
本文翻译自 https://www.cryptopp.com/wiki/Digital_Signature_Algorithm,本人英文水平有限,如有翻译不当之处请给出修改建议!
DSA是数字签名算法,DSA是 FIPS 186中指定的三种数字签名方案之一。FIPS 186-2 指定了一个1024位的p,160位的q,并且使用SHA-1作为哈希算法。FIPS 186-3 使用更大的哈希值SHA-2作为哈希算法,p值增大到3072位,q值增大到256位。FIPS 186-3 在2009年6月生效。
在早期的FIPS186文档中,为了允许更小的DSA模运算,未定义 DSA_1024_BIT_MODULUS_ONLY。如果要使用一个更低级别的模运算大小,一定要记住模数的PRIME_LENGTH_MULTIPLE倍 一定要介于 DSA::MIN_PRIME_LENGTH和 DSA::MAX_PRIME_LENGTH之间。
1. 构造函数
DSA::PublicKey 和 DSA::PrivateKey 都是由不接受任何参数的模板类定义的。详细的定义参看gfpcrypt.h头文件。
2 样例程序
2.1 密钥生成
下面的样例代码生成一个DSA密钥。
#include "dsa.h"#include "osrng.h"int main(int argc, char* argv[]){ AutoSeededRandomPool rng; // Generate Private Key DSA::PrivateKey privateKey; privateKey.GenerateRandomWithKeySize(rng, 1024); // Generate Public Key DSA::PublicKey publicKey; publicKey.AssignFrom(PrivateKey); if (!privateKey.Validate(rng, 3) || !publicKey.Validate(rng, 3)) { throw runtime_error("DSA key generation failed"); } return 0;}
2.2 加载和保存
下面的程序序列化DSA密钥为PKCS#8 和 X.509格式扩展了密钥生成样例, 在下面的代码中,编码后的密钥(encodedPublicKey 和 encodedPrivateKey)存在于内存中。密钥能够被持久化到磁盘中,使用FileSink 而是不 StringSink类。下面的代码使用StringSink和string来保存私钥。尽管方便,但是不是一个好的主意。
AutoSeededRandomPool rng;// Generate Public and Private KeysDSA::PrivateKey privateKey = ...; DSA::PublicKey publicKey = ...;...// DER Encoded Keysstring encodedPublicKey, encodedPrivateKey;// Serialize in PKCS#8 and X.509 formatpublicKey.Save( StringSink(encodedPublicKey).Ref() );privateKey.Save( StringSink(encodedPrivateKey).Ref() );// Decode DSA keysDSA::PrivateKey decodedPrivateKey;decodedPrivateKey.Load( StringStore(encodedPrivateKey).Ref());DSA::PublicKey decodedPublicKey;decodedPublicKey.Load( StringStore(encodedPublicKey).Ref());
2.3 签名生成和验证
建立在之前例子的基础上,接下来的例子签名并验证一个消息。签名使用SignerFilter完成,而验证是由SignatureVerificationFilter类完成的。
// Generate or Load the Public and Private KeysDSA::PrivateKey PrivateKey; DSA::PublicKey PublicKey;...string message = "DSA Signature";string signature;DSA::Signer signer( PrivateKey );StringSource ss1( message, true, new SignerFilter( rng, signer, new StringSink( signature ) ) // SignerFilter); // StringSourceDSA::Verifier verifier( PublicKey );StringSource ss2( message+signature, true, new SignatureVerificationFilter( verifier, NULL, THROW_EXCEPTION /* SIGNATURE_AT_END */ ));cout << "Verified signature on message" << endl;
在上述的例子中,过滤器接受message+signature的串联,如果签名首先被插入,SIGNATURE_AT_BEGIN 应该被指定为一个额外的标签值。
DSA::Verifier verifier( PublicKey );StringSource ss( signature+message, true, new SignatureVerificationFilter( verifier, NULL, THROW_EXCEPTION | SIGNATURE_AT_BEGIN ));
接下来的样例验证签名而不抛出异常。该样例使用标签 PUT_RESULT。 SignatureVerificationFilter 将会把结果添加到附加的转换中。结果通过管道输入bool值result。为了方便管道(传输数据)变量被包装成ArraySink。以下代码中有三点值得关注:首先,同时指定PUT_RESULT 和 THROW_EXCEPTION没有任何意义;其次,StringSink不能够被使用因为为bool值不是继承自std::basic_string;最后,唯一的标签是PUT_RESULT,因此签名必须在最后呈现(SIGNATURE_AT_BEGIN没有被指定)。
...DSA::Verifier verifier( PublicKey );bool result = false;StringSource ss( message+signature, true, new SignatureVerificationFilter( verifier, new ArraySink( (byte*)&result, sizeof(result ) ), PUT_RESULT | SIGNATURE_AT_END ));if( true == result ) { cout << "Verified signature on message" << endl;}
最后的代码使用一个鲜为人知的sink称为转向器。并不拥有其附加的BufferedTransformation转向器,所以附加对象没有被删除(行为的结果,转向器需要引用而不是指针)。那是有用的, 当需要一个中间结果,该结果从一个对象。
...DSA::Verifier verifier( PublicKey );SignatureVerificationFilter svf( verifier /* SIGNATURE_AT_END */); // SignatureVerificationFilterStringSource ss( message+signature, true, new Redirector( svf )); // StringSourceif( true == svf.GetLastResult() ) { cout << "Verified signature on message" << endl;}
- Crypto++(二)数字签名算法DSA
- JAVA加密算法- 数字签名算法(RSA、DSA)
- 数字签名算法DSA
- 什么是数字签名算法(DSA)
- 数字签名算法--2.DSA
- 数字签名算法DSA
- 数字签名技术 -- DSA算法
- 数字签名算法DSA
- 数字签名算法DSA
- 数字签名(2):DSA
- 用DSA算法实现数字签名
- 【密钥算法】Java加密技术(六)---DSA 数字签名算法
- Java加密技术(六)——数字签名算法DSA
- Java加密技术(六)——数字签名算法DSA
- Java加密技术(六)——数字签名算法DSA
- Java加密技术(六)——数字签名算法DSA
- Java加密技术(六)——数字签名算法DSA
- Java加密技术(六)——数字签名算法DSA
- easyui pagelist
- jstl EL表达式遍历Map
- 搭建SVN服务器
- UVA-11584-Partitioning by Palindromes(区间DP 最小回文串数量)
- HTML5基础(SQLite数据库)
- Crypto++(二)数字签名算法DSA
- Maven学习 (二) Eclipse 上安装 Maven3插件
- ubuntu terminal配色消失
- PLM 产品生命周期管理
- 获得查询字符串,变为对象格式
- 根Acitivity的启动过程(1) Launcher 向ActivityManagerService发送一个启动MainActivity的进程间通信请求
- 错误:src/core/ngx_regex.c:307: undefined reference to `pcre_free_study',解决
- gcc编译信息带颜色输出
- 宣传片