2014-01-15 20 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類同樣適用。