📄 keygen.cpp
字号:
// KeyGen.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// Runtime Includes
#include <iostream>
#include <string>
// The Features Available...
const unsigned int FEATURE_EVALUATION = 0x01; // 0000 0001
const unsigned int FEATURE_USE_SQUARES = 0x02; // 0000 0010
const unsigned int FEATURE_USE_CIRCLES = 0x04; // 0000 0100
const unsigned int FEATURE_USE_WIDGETS = 0x08; // 0000 1000
const unsigned int FEATURE_MAGIC = 0xAAAA << 16;
const unsigned int FEATURE_MAGIC_MASK = 0xFFFF << 16;
// Crypto++ Library
#ifdef _DEBUG
# pragma comment ( lib, "cryptlibd" )
#else
# pragma comment ( lib, "cryptlib" )
#endif
// Crypto++ Includes
// Expedient Implementation of a NULL Hash
#include "cryptlib.h"
#include "iterhash.h"
// TRUEHash
// A hash that always returns 0x01
// This is a Visual C++ workaround (possible others)
// due to not being being able to create a NULL HMAC class
// The NULL HMAC cannot compile due to a digest size of 0
// because of array sizing of 0 (major problems in SecByteBlock())
class TRUEHash : public CryptoPP::IteratedHashWithStaticTransform<CryptoPP::word32, CryptoPP::BigEndian, 32, 4, TRUEHash>
{
public:
static void InitState(HashWordType *state)
{ state[0] = 0x01; return; }
static void Transform(CryptoPP::word32 *digest, const CryptoPP::word32 *data) { return; }
static const char *StaticAlgorithmName() {return "TRUE HASH";}
};
// Crypto++ Includes
// Template Specialization modification of
// struct ECIES in ecccrypto.h
#include "pubkey.h"
#include "dh.h"
#include "sha.h"
#include "eccrypto.h"
#include "ecp.h"
template <class EC, class COFACTOR_OPTION = CryptoPP::NoCofactorMultiplication, bool DHAES_MODE = false>
struct ECIESNullT
: public CryptoPP::DL_ES<
CryptoPP::DL_Keys_EC<EC>,
CryptoPP::DL_KeyAgreementAlgorithm_DH<typename EC::Point, COFACTOR_OPTION>,
CryptoPP::DL_KeyDerivationAlgorithm_P1363<typename EC::Point, DHAES_MODE, CryptoPP::P1363_KDF2<CryptoPP::SHA1> >,
CryptoPP::DL_EncryptionAlgorithm_Xor<CryptoPP::HMAC<TRUEHash>, DHAES_MODE>,
CryptoPP::ECIES<EC> >
{
static std::string StaticAlgorithmName() {return "ECIES with NULL T";}
};
// Crypto++ Includes
// Required for main(...) and Function
#include "cryptlib.h"
#include "osrng.h" // Random Numbeer Generator
#include "eccrypto.h" // Elliptic Curve
#include "ecp.h" // F(p) EC
#include "integer.h" // Integer Operations
#include "base32.h" // Encodeing-Decoding
#include "nbtheory.h" // ModularSquareRoot(...)
// Function Prototypes
void PrintCurveParameters( CryptoPP::Integer p, CryptoPP::Integer a, CryptoPP::Integer b,
CryptoPP::Integer x, CryptoPP::Integer y,
CryptoPP::Integer n, CryptoPP::Integer h );
//
// Solves y^2 = x^3 + a*x + b
//
CryptoPP::Integer Y(CryptoPP::Integer x, CryptoPP::Integer a,
CryptoPP::Integer b, CryptoPP::Integer p);
int main(int argc, char* argv[]) {
byte* CipherText = NULL;
byte* EncodedText = NULL;
CryptoPP::AutoSeededRandomPool rng;
try {
// PlainText (Message M)
unsigned int Features = 0;
Features |= FEATURE_MAGIC;
Features |= FEATURE_USE_WIDGETS;
Features |= FEATURE_USE_SQUARES;
// Runtime Sizes...
unsigned int PlainTextLength = sizeof( Features );
// User Defined Domain Parameters
CryptoPP::Integer p("214644128745822931");
CryptoPP::Integer a("0");
CryptoPP::Integer b("-23719602096934623");
CryptoPP::Integer n("71548043092139563"); // R from ECB
CryptoPP::Integer h("3"); // S from ECB, must be <= 4
CryptoPP::Integer x("-35255913743814615");
CryptoPP::Integer y("-71911853159754273");
PrintCurveParameters( p, a, b, x, y, n, h ); std::cout << std::endl;
CryptoPP::ECP ec( p, a, b );
ECIESNullT< CryptoPP::ECP >::PrivateKey PrivateKey;
ECIESNullT< CryptoPP::ECP >::PublicKey PublicKey;
// Curve Initialization
PrivateKey.Initialize( ec, CryptoPP::ECP::Point( x, y ), n , h);
PrivateKey.MakePublicKey( PublicKey );
// Key Initialization
ECIESNullT< CryptoPP::ECP >::Encryptor Encryptor( PublicKey );
// No Need for ECIESNullT< CryptoPP::ECP >::Decryptor
// in the Key Generator. The software will employ it.
// if( false == PrivateKey.Validate( rng, 0 ) )
// { throw std::string( "Private Key Validation" ); }
// if( false == PublicKey.Validate( rng, 0 ) )
// { throw std::string( "Public Key Validation" ); }
const unsigned int ITERATIONS = 128;
for(int i = 0; i < ITERATIONS; i++ ) {
unsigned int CipherTextLength = Encryptor.CiphertextLength( PlainTextLength );
if( 0 == CipherTextLength )
{ throw std::string("plaintextLength is not valid (too long)"); }
// Scratch for Encryption
CipherText = new byte[ CipherTextLength ];
if( NULL == CipherText )
{ throw std::string( "CipherText Allocation Failure" ); }
// Encryption
Encryptor.Encrypt( rng, reinterpret_cast<const byte*>( &Features ),
PlainTextLength, CipherText );
// Base 32 Encoding
CryptoPP::Base32Encoder Encoder;
Encoder.Put( CipherText, CipherTextLength );
Encoder.MessageEnd();
// Scratch for Base 32 Encoded Ciphertext
unsigned int EncodedTextLength = Encoder.MaxRetrievable();
EncodedText = new byte[ EncodedTextLength + 1 ];
EncodedText[ EncodedTextLength ] = '\0';
// Fetch Base 32 Encoded Ciphertext
Encoder.Get( EncodedText, EncodedTextLength );
const unsigned int BREAK = 5;
for(unsigned int i = 0; i < EncodedTextLength; i++)
{
if( 0 != i && 0 == i % BREAK ) { std::cout << "-"; }
std::cout << EncodedText[ i ];
}; std::cout << std::endl;
// Cleanup
delete[] CipherText;
delete[] EncodedText;
// Create a new, Tempoaray Public Key
PrivateKey.MakePublicKey( PublicKey );
}
}
catch( CryptoPP::Exception& e ) {
std::cerr << "Crypto++ Error: " << e.what() << std::endl;
}
catch( std::string& s ) {
std::cerr << "Error: " << s << std::endl;
}
catch (...) {
std::cerr << "Unknown Error" << std::endl;
}
return 0;
}
CryptoPP::Integer Y(CryptoPP::Integer x, CryptoPP::Integer a,
CryptoPP::Integer b, CryptoPP::Integer p)
{
CryptoPP::Integer y = CryptoPP::a_exp_b_mod_c( x, 3, p );
y += ( a * x + b ) % p;
y = CryptoPP::ModularSquareRoot( y, p );
//
// If y = 0, the cipher text will not decrypt properly
// In this case, generate different Domain Parameters
//
assert( 0 != y);
return y;
}
void PrintCurveParameters( CryptoPP::Integer p, CryptoPP::Integer a, CryptoPP::Integer b,
CryptoPP::Integer x, CryptoPP::Integer y,
CryptoPP::Integer n, CryptoPP::Integer h )
{
std::cout << "Curve Parameters: " << std::endl;
std::cout << " p = " << p << " (" << p.BitCount() << " bits)" << std::endl;
std::cout << " a = " << a << std::endl;
std::cout << " x = " << x << std::endl;
std::cout << " y = " << y << std::endl;
std::cout << " b = " << b << std::endl;
std::cout << " n = " << n << std::endl;
std::cout << " h = " << h << std::endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -