2014-01-15 13 views
1

我正在翻转CryptoPP,无法找到这个特定问题的答案。这里是示例源代码(部分)有点压缩的公钥初始化API吗?

AutoSeededRandomPool prng; 

//Generate a private key 
ECDSA<ECP, CryptoPP::SHA256>::PrivateKey privateKey; 
privateKey.Initialize(prng, CryptoPP::ASN1::secp256r1()); 

// Generate publicKey 
ECDSA<ECP, CryptoPP::SHA256>::PublicKey publicKey; 
privateKey.MakePublicKey(publicKey); 

// Extract Component values 
Integer p = privateKey.GetGroupParameters().GetCurve().GetField().GetModulus(); 
Integer a = privateKey.GetGroupParameters().GetCurve().GetA(); 
Integer b = privateKey.GetGroupParameters().GetCurve().GetB(); 
Integer Gx = privateKey.GetGroupParameters().GetSubgroupGenerator().x; 
Integer Gy = privateKey.GetGroupParameters().GetSubgroupGenerator().y; 
Integer n = privateKey.GetGroupParameters().GetSubgroupOrder(); 
Integer h = privateKey.GetGroupParameters().GetCofactor(); 
Integer Qx = publicKey.GetPublicElement().x; 
Integer Qy = publicKey.GetPublicElement().y; 
Integer x = privateKey.GetPrivateExponent(); 

// Construct Point elelemt; 
ECP curve(p,a,b); 
ECP::Point G(Gx,Gy); 
ECP::Point Q(Qx,Qy); 

//Build publicKey using elements (no point compression) 
ECDSA<ECP, CryptoPP::SHA256>::PublicKey GeneratedPublicKey; 
GeneratedPublicKey.Initialize(curve,G,n,Q); 
assert(GeneratedPublicKey.Validate(prng, 3)); 

//Build publicKey using elements (with point compression)? 

用这种方法,我可以使用组件值生成publicKey。然而,我不能 使它与点压缩 - 这意味着我没有QY值 - 有没有一种 方式来做到这一点?初始化方法有两个重载,但没有一个是针对点数为 的压缩情况。

我的问题是关于“PublicKey.Initialize(curve,G,n,Q)”上的Crypto ++的特定问题。由于我无法将整个publicKey与我当前的项目进行传输 - 我强制将参数域 指定为索引值,并且只能传输Qx值。所以我应该使用类似“PublicKey.Initialize(curve,G,n,Q)”初始化publicKey “但是,我找不到有关点压缩的初始化API。

所以,这不是关于“如何做点压缩”,而是“有没有方法来初始化没有Qy值的 公钥?”

+0

请准确告诉我们你有什么。你从来没有告诉你正在使用的参数//使用元素(使用点压缩)构建publicKey?'。如果它的ASN.1编码值,用'dumpasn1'显示给我们。如果原始表单是二进制的,则将其显示给我们以十六进制编码。 – jww

回答

1

如何构建仅使用x值(点压缩)的ECDSA publicKey?

x是私人的指数。公钥是曲线上的一个点,并且它不使用私有指数。

获取公钥:采用私人指数,并提高您的基点。那就是,Q = G^x

如果要设置私钥或解密器的私有指数,请设置域参数(即DL_GroupParameters_EC<ECP>DL_GroupParameters_EC<EC2M>),然后再调用SetPrivateExponent(x);


您是否回顾了以前的问题How can I recover compressed y value from sender??社区花时间为您提供答案和示例代码,但您没有承认或跟进。

我觉得owlstead说得好here

我们为什么要在乎回答你的,如果你不倾向于接受答案 甚至跟进呢?你的问题是对的,但你对待社区的方式是可怕的。

+0

我在前一篇上看过你的回答,它对我有很大的帮助。再次感谢你:) – Felixr3136148

+0

我很抱歉,如果这个问题看起来像我忽略了你对我以前的问题的答案。不是的,我从你之前的回答中学到了很多,现在我可以做点压缩。这是我脑海中的另一个问题。 – Felixr3136148

0

“有没有方法来初始化没有Qy值的公钥?”

是的,有。这里是一个加密的例子:

#include <string> 
#include <iostream> 
#include <cryptopp/cryptlib.h> 
#include <cryptopp/ecp.h> 
#include <cryptopp/eccrypto.h> 
#include <cryptopp/hex.h> 
#include <cryptopp/oids.h> 
#include <cryptopp/osrng.h> 

using namespace CryptoPP; 
using std::cout; 
using std::endl; 

int main() 
{ 
    OID curve = ASN1::secp256r1(); 
    ECDH<ECP>::Domain domain(curve); 

    SecByteBlock privKey(domain.PrivateKeyLength()); 
    SecByteBlock pubKey(domain.PublicKeyLength()); 
    AutoSeededRandomPool prng; 
    domain.GenerateKeyPair(prng, privKey, pubKey); 

    // Convert public key to string representation 
    std::string pub_str; 
    HexEncoder encoder; 
    encoder.Attach(new StringSink(pub_str)); 
    encoder.Put(pubKey.data(), pubKey.size()); 
    encoder.MessageEnd(); 

    // Uncompressed point - first byte '04' in front of the string. 
    std::cout << "Uncompressed public key (point) " << pub_str << endl; 

    // Extract x value from the point 
    std::string public_point_x = pub_str.substr(2, 64); 

    // Compressed - '02' byte in front of the string. 
    public_point_x = "02" + public_point_x; 
    std::cout << "Compressed public key (point) " << public_point_x << endl; 

    // ----- reconstruct point from compressed point/value. 
    StringSource ss(public_point_x, true, new HexDecoder); 
    ECP::Point point; 
    domain.GetGroupParameters().GetCurve().DecodePoint(point, ss, ss.MaxRetrievable()); 

    cout << "Result after decompression X: " << std::hex << point.x << endl; 
    cout << "Result after decompression Y: " << std::hex << point.y << endl; 

    return 0; 
} 

我希望这是你的问题的答案。我使用的是ECDH,但它与ECDSA类同样适用。