Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
加密++;:使用PBKDF2生成基于密码的密钥并将其与ECDSA一起使用(从Python代码转换)_Python_C++_Crypto++_Ecdsa_Pbkdf2 - Fatal编程技术网

加密++;:使用PBKDF2生成基于密码的密钥并将其与ECDSA一起使用(从Python代码转换)

加密++;:使用PBKDF2生成基于密码的密钥并将其与ECDSA一起使用(从Python代码转换),python,c++,crypto++,ecdsa,pbkdf2,Python,C++,Crypto++,Ecdsa,Pbkdf2,我试图用密码+ ++将Python密码转换为C++。我认为这是一项相当简单的任务,因为我有示例代码,但我在加载PBKDF2生成的私钥作为ECDSA的私钥时遇到了困难。但由于我不熟悉加密算法,我可能会错过一些东西 Python示例代码(取自ErisX-REST-API-v3.3(第9页),您可以在这里找到:): 生成的签名是:381YXZ1TSTIJROZCOFYPQ19XPFUS8NHQNWBJGG7OQFEA2VZOCG9ZP5ERDxxM4YAKVVVvCuyZBTH4G8NFYXISHWV

我试图用密码+ ++将Python密码转换为C++。我认为这是一项相当简单的任务,因为我有示例代码,但我在加载PBKDF2生成的私钥作为ECDSA的私钥时遇到了困难。但由于我不熟悉加密算法,我可能会错过一些东西

Python示例代码(取自ErisX-REST-API-v3.3(第9页),您可以在这里找到:):

生成的签名是:381YXZ1TSTIJROZCOFYPQ19XPFUS8NHQNWBJGG7OQFEA2VZOCG9ZP5ERDxxM4YAKVVVvCuyZBTH4G8NFYXISHWVUFPI7RGGX

这里是我的C++实现:

#include <iostream>

#include <cryptlib.h>
#include <pwdbased.h>
#include <sha.h>
#include <hex.h>
#include <eccrypto.h>
#include <osrng.h>
#include <asn.h>
#include <oids.h>
#include <sstream>
#include <base64.h>

int main(int n, char **args){
    std::string message = "test message";
    std::string authId = "12345";
    int iterations= 100000;
    
    // https://cryptopp.com/wiki/PKCS5_PBKDF2_HMAC
    CryptoPP::byte password[] ="abcde";
    size_t plen = strlen((const char*)password);
    CryptoPP::byte salt[] = "12345"; // authID from Python code is the salt;
    size_t slen = strlen((const char*)salt);

    CryptoPP::byte key[32]; // dklen from the Python example

    PKCS5_PBKDF2_HMAC<SHA256> pbkdf;
    byte purpose = 0; // purpose is unused in PKCS5_PBKDF2_HMAC => 0.

    pbkdf.DeriveKey(key, sizeof(key), purpose, password, plen, salt, slen, iterations, 0.0f);

    std::string result;
    std::stringstream ss_result;
    ss_result << "" << key;
    std::cout << "(Derived) key with stringstream: " << key << std::endl;
        
    /// Define a custom encoder for Base58
    // Encoder
    CryptoPP::Base64Encoder encoder(new StringSink(result));
    const CryptoPP::byte ALPHABET[] = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
    AlgorithmParameters params = MakeParameters(Name::EncodingLookupArray(),(const byte *)ALPHABET);
    encoder.IsolatedInitialize(params);

    // Decoder
    //int lookup[256];
    //const CryptoPP::byte ALPHABET[] = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
    //Base64Decoder::InitializeDecodingLookupArray(lookup, ALPHABET, 58, false);

    //Base64Decoder decoder;
    //AlgorithmParameters params_dec = MakeParameters(Name::DecodingLookupArray(),(const int *)lookup);
    //decoder.IsolatedInitialize(params_dec);

    encoder.Put(key, sizeof(key));
    encoder.MessageEnd();

    std::cout << "(Derived) key with Base58-Encoding: " << result << std::endl;

    CryptoPP::AutoSeededRandomPool prng; //Create a random number
    CryptoPP::ECDSA<ECP, CryptoPP::SHA256>::PrivateKey privateKey;
    ECDSA<ECP,CryptoPP::SHA256>::Signer signer ( privateKey );
    signer.AccessKey().Initialize(prng, CryptoPP::ASN1::secp256k1());

    CryptoPP::StringSource ss2(result, true);
    signer.AccessKey().Load(ss2);
    
    std::string signature;
    StringSource ss1( message, true /*pump all*/,
            new SignerFilter( prng,    signer,
            new HexEncoder(new StringSink(signature))
        ) // SignerFilter
    );
    std::cout << "Signature: " << (signature) << std::endl;

}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int n,字符**args){
std::string message=“测试消息”;
std::string authId=“12345”;
int迭代次数=100000次;
// https://cryptopp.com/wiki/PKCS5_PBKDF2_HMAC
CryptoPP::字节密码[]=“abcde”;
size\u t plen=strlen((常量字符*)密码);
CryptoPP::byte salt[]=“12345”;//Python代码中的authID是salt;
大小=strlen((常量字符*)盐;
CryptoPP::byte key[32];//Python示例中的dklen
PKCS5_PBKDF2_HMAC pbkdf;
字节目的=0;//目的在PKCS5_PBKDF2_HMAC=>0中未使用。
pbkdf.DeriveKey(key,sizeof(key),目的,密码,plen,salt,slen,迭代次数,0.0f);
std::字符串结果;
std::stringstream ss_结果;
ss_结果
#include <iostream>

#include <cryptlib.h>
#include <pwdbased.h>
#include <sha.h>
#include <hex.h>
#include <eccrypto.h>
#include <osrng.h>
#include <asn.h>
#include <oids.h>
#include <sstream>
#include <base64.h>

int main(int n, char **args){
    std::string message = "test message";
    std::string authId = "12345";
    int iterations= 100000;
    
    // https://cryptopp.com/wiki/PKCS5_PBKDF2_HMAC
    CryptoPP::byte password[] ="abcde";
    size_t plen = strlen((const char*)password);
    CryptoPP::byte salt[] = "12345"; // authID from Python code is the salt;
    size_t slen = strlen((const char*)salt);

    CryptoPP::byte key[32]; // dklen from the Python example

    PKCS5_PBKDF2_HMAC<SHA256> pbkdf;
    byte purpose = 0; // purpose is unused in PKCS5_PBKDF2_HMAC => 0.

    pbkdf.DeriveKey(key, sizeof(key), purpose, password, plen, salt, slen, iterations, 0.0f);

    std::string result;
    std::stringstream ss_result;
    ss_result << "" << key;
    std::cout << "(Derived) key with stringstream: " << key << std::endl;
        
    /// Define a custom encoder for Base58
    // Encoder
    CryptoPP::Base64Encoder encoder(new StringSink(result));
    const CryptoPP::byte ALPHABET[] = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
    AlgorithmParameters params = MakeParameters(Name::EncodingLookupArray(),(const byte *)ALPHABET);
    encoder.IsolatedInitialize(params);

    // Decoder
    //int lookup[256];
    //const CryptoPP::byte ALPHABET[] = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
    //Base64Decoder::InitializeDecodingLookupArray(lookup, ALPHABET, 58, false);

    //Base64Decoder decoder;
    //AlgorithmParameters params_dec = MakeParameters(Name::DecodingLookupArray(),(const int *)lookup);
    //decoder.IsolatedInitialize(params_dec);

    encoder.Put(key, sizeof(key));
    encoder.MessageEnd();

    std::cout << "(Derived) key with Base58-Encoding: " << result << std::endl;

    CryptoPP::AutoSeededRandomPool prng; //Create a random number
    CryptoPP::ECDSA<ECP, CryptoPP::SHA256>::PrivateKey privateKey;
    ECDSA<ECP,CryptoPP::SHA256>::Signer signer ( privateKey );
    signer.AccessKey().Initialize(prng, CryptoPP::ASN1::secp256k1());

    CryptoPP::StringSource ss2(result, true);
    signer.AccessKey().Load(ss2);
    
    std::string signature;
    StringSource ss1( message, true /*pump all*/,
            new SignerFilter( prng,    signer,
            new HexEncoder(new StringSink(signature))
        ) // SignerFilter
    );
    std::cout << "Signature: " << (signature) << std::endl;

}