使用 SecByteBlock 密钥对的 ECDSA
ECDSA using SecByteBlock key pairs
我已经实现了 ECDH
,私有和 public 都是 SecByteBlock
类型,现在我正在尝试使用 SHA256
实现 ECDSA
这些键。
假设pubA
和privA
已经生成,并且有SecByteBlock
个实例,我如何在ECDSA中使用它们?
提前致谢!
OID CURVE = secp256r1();
AutoSeededX917RNG<AES> rng;
// Elliptic Curve Diffie-Hellman
ECDH < ECP >::Domain dhA( CURVE );
SecByteBlock privA(dhA.PrivateKeyLength()), pubA(dhA.PublicKeyLength());
dhA.GenerateKeyPair(rng, privA, pubA);
// Elliptic Curve Digital Signature Algorithm
ECDSA<ECP, SHA256>::PrivateKey privateKey;
DL_GroupParameters_EC<ECP> params(ASN1::secp256k1());
privateKey.Initialize(rng, params);
// Generating private key
ECIES<ECP>::PrivateKey privateKey;
privateKey.Initialize(rng, ASN1::secp160r1());
// Generating matching public key
ECIES<ECP>::PublicKey publicKey;
privateKey.MakePublicKey(publicKey);
ECDSA<ECP>::Signer signer(privateKey);
ECDSA<ECP>::Verifier verifier(publicKey);
我认为这不是个好主意。您应该使用 ECDSA
class 来创建密钥。然而,它就在这里。
#include "cryptlib.h"
#include "eccrypto.h"
#include "secblock.h"
#include "filters.h"
#include "integer.h"
#include "osrng.h"
#include "files.h"
#include "oids.h"
#include "sha.h"
#include <iostream>
#include <stdexcept>
int main(int argc, char* argv[])
{
using namespace CryptoPP;
try
{
const OID CURVE = ASN1::secp256r1();
AutoSeededRandomPool prng;
ECDH <ECP>::Domain dh(CURVE);
SecByteBlock privA(dh.PrivateKeyLength()), pubA(dh.PublicKeyLength());
dh.GenerateKeyPair(prng, privA, pubA);
const Integer x(privA.begin(), privA.size());
ECDSA<ECP, SHA256>::PrivateKey privateKey;
privateKey.Initialize(CURVE, x);
bool valid = privateKey.Validate(prng, 3);
if (valid == false)
throw std::runtime_error("Private key is not valid ECDSA key");
std::cout << "Private key is valid ECDSA key" << std::endl;
}
catch (const std::runtime_error& ex)
{
std::cerr << ex.what() << std::endl;
std::exit(1);
}
return 0;
}
并且:
cryptopp$ g++ test.cxx ./libcryptopp.a -o test.exe
cryptopp$ ./test.exe
Private key is valid ECDSA key
更多信息位于 Elliptic Curve Digital Signature Algorithm Crypto++ wiki。
我已经实现了 ECDH
,私有和 public 都是 SecByteBlock
类型,现在我正在尝试使用 SHA256
实现 ECDSA
这些键。
假设pubA
和privA
已经生成,并且有SecByteBlock
个实例,我如何在ECDSA中使用它们?
提前致谢!
OID CURVE = secp256r1();
AutoSeededX917RNG<AES> rng;
// Elliptic Curve Diffie-Hellman
ECDH < ECP >::Domain dhA( CURVE );
SecByteBlock privA(dhA.PrivateKeyLength()), pubA(dhA.PublicKeyLength());
dhA.GenerateKeyPair(rng, privA, pubA);
// Elliptic Curve Digital Signature Algorithm
ECDSA<ECP, SHA256>::PrivateKey privateKey;
DL_GroupParameters_EC<ECP> params(ASN1::secp256k1());
privateKey.Initialize(rng, params);
// Generating private key
ECIES<ECP>::PrivateKey privateKey;
privateKey.Initialize(rng, ASN1::secp160r1());
// Generating matching public key
ECIES<ECP>::PublicKey publicKey;
privateKey.MakePublicKey(publicKey);
ECDSA<ECP>::Signer signer(privateKey);
ECDSA<ECP>::Verifier verifier(publicKey);
我认为这不是个好主意。您应该使用 ECDSA
class 来创建密钥。然而,它就在这里。
#include "cryptlib.h"
#include "eccrypto.h"
#include "secblock.h"
#include "filters.h"
#include "integer.h"
#include "osrng.h"
#include "files.h"
#include "oids.h"
#include "sha.h"
#include <iostream>
#include <stdexcept>
int main(int argc, char* argv[])
{
using namespace CryptoPP;
try
{
const OID CURVE = ASN1::secp256r1();
AutoSeededRandomPool prng;
ECDH <ECP>::Domain dh(CURVE);
SecByteBlock privA(dh.PrivateKeyLength()), pubA(dh.PublicKeyLength());
dh.GenerateKeyPair(prng, privA, pubA);
const Integer x(privA.begin(), privA.size());
ECDSA<ECP, SHA256>::PrivateKey privateKey;
privateKey.Initialize(CURVE, x);
bool valid = privateKey.Validate(prng, 3);
if (valid == false)
throw std::runtime_error("Private key is not valid ECDSA key");
std::cout << "Private key is valid ECDSA key" << std::endl;
}
catch (const std::runtime_error& ex)
{
std::cerr << ex.what() << std::endl;
std::exit(1);
}
return 0;
}
并且:
cryptopp$ g++ test.cxx ./libcryptopp.a -o test.exe
cryptopp$ ./test.exe
Private key is valid ECDSA key
更多信息位于 Elliptic Curve Digital Signature Algorithm Crypto++ wiki。