📄 keyval.cpp
字号:
// ecctest7.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
// Array sizing of 0 causes 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 );
int main(int argc, char* argv[]) {
// 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");
CryptoPP::AutoSeededRandomPool rng;
try {
// PlainText (Message M)
unsigned int Features = 0;
unsigned int PlainTextLength = sizeof( Features );
assert ( 4 == PlainTextLength );
Features |= FEATURE_MAGIC;
Features |= FEATURE_USE_WIDGETS;
Features |= FEATURE_USE_SQUARES;
PrintCurveParameters( p, a, b, x, y, n, h ); std::cout << std::endl;
CryptoPP::ECP ec( p, a, b );
ECIESNullT< CryptoPP::ECP >::PrivateKey PrivateKey;
// Key Initialization
// No need for a Public Key for Decryption
PrivateKey.Initialize( ec, CryptoPP::ECP::Point( x, y ), n , h);
// if( false == PrivateKey.Validate( rng, 0 ) )
// { throw std::string( "Private Key Validation" ); }
// if( false == PublicKey.Validate( rng, 0 ) )
// { throw std::string( "Public Key Validation" ); }
// Pretty Print Base32 Encoded
std::string PrettyPrintEncodedText = "ASBCB-W28V7-446JS-AC684-3WQRB-PG9SK-3CHSA-AAAAB";
std::cout << "Product Key:" << std::endl;
std::cout << " " << PrettyPrintEncodedText << std::endl;
// Base32 Pretty Print Decoded
std::string PrettyPrintDecodedText;
for(unsigned int j = 0; j < PrettyPrintEncodedText.length(); j++)
{
if( '-' != PrettyPrintEncodedText[ j ] )
{ PrettyPrintDecodedText += PrettyPrintEncodedText[ j ]; }
};
// Diagnostics
// std::cout << "Unformatted Product Key:";
// std::cout << std::endl << " " << PrettyPrintDecodedText << std::endl << std::endl;
// Base32 Decoding
CryptoPP::Base32Decoder Decoder;
Decoder.Put( reinterpret_cast<const byte*>(PrettyPrintDecodedText.c_str()),
PrettyPrintDecodedText.length() );
Decoder.MessageEnd();
// Scratch forBase 32 Encoded Ciphertext
unsigned int DecodedTextLength = Decoder.MaxRetrievable();
byte* DecodedText = new byte[ DecodedTextLength + 1 ];
if( NULL == DecodedText )
{ throw std::string("DecodedText Allocation Failure"); }
DecodedText[ DecodedTextLength ] = '\0';
Decoder.Get( DecodedText, DecodedTextLength );
// Decryption
ECIESNullT< CryptoPP::ECP >::Decryptor Decryptor( PrivateKey );
// Scratch for Decryption
unsigned int RecoveredTextLength = Decryptor.MaxPlaintextLength( DecodedTextLength );
if( 0 == RecoveredTextLength )
{ throw std::string("ciphertextLength is not valid (too long or too short)"); }
// Sanity Check
assert( RecoveredTextLength == PlainTextLength );
// Decryption Buffer
byte* RecoveredText = new byte[ RecoveredTextLength ];
if( NULL == RecoveredText )
{ throw std::string( "RecoveredText CipherText Allocation Failure" ); }
// Decryption
CryptoPP::DecodingResult Result =
Decryptor.Decrypt( rng, DecodedText, DecodedTextLength, RecoveredText );
// Crypto++ Test
if( false == Result.isValidCoding )
{ throw std::string("CryptoPP::DecodingResult: Invalid Coding"); }
// Conversion for Convenience
// Don't be fooled:
// RecoveredFeatures = static_cast<unsigned int>( *RecoveredText );
// Only converts byte[ 0 ], not bytes[ 0 - 4 ]
unsigned int RecoveredFeatures = *(reinterpret_cast<unsigned int*>( RecoveredText ) );
// Sanity Check
assert( Features = RecoveredFeatures );
// Output
if( FEATURE_MAGIC != (RecoveredFeatures & FEATURE_MAGIC_MASK ) )
{ throw std::string("RecoveredFeatures: Magic: Invalid Product Key"); }
else
{
if( FEATURE_EVALUATION == (RecoveredFeatures & FEATURE_EVALUATION ) )
{ std::cout << "Evaluation Edition." << std::endl; }
if( FEATURE_USE_SQUARES == (RecoveredFeatures & FEATURE_USE_SQUARES) )
{ std::cout << "Operations are permitted on Squares." << std::endl; }
if( FEATURE_USE_CIRCLES == (RecoveredFeatures & FEATURE_USE_CIRCLES) )
{ std::cout << "Operations are permitted on Circles." << std::endl; }
if( FEATURE_USE_WIDGETS == (RecoveredFeatures & FEATURE_USE_WIDGETS) )
{ std::cout << "Operations are permitted on Widgets." << std::endl; }
}
// Cleanup
delete[] DecodedText;
delete[] RecoveredText;
}
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;
}
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 << " b = " << b << std::endl;
std::cout << " x = " << x << std::endl;
std::cout << " y = " << y << std::endl;
std::cout << " n = " << n << std::endl;
std::cout << " h = " << h << std::endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -