// ecctest4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
// Runtime Includes
#include <iostream>
#include <string>
// Crypto++ Library
#ifdef _DEBUG
# pragma comment ( lib, "cryptlibd" )
#else
# pragma comment ( lib, "cryptlib" )
#endif
// Crypto++ Includes
#include "cryptlib.h"
#include "osrng.h" // Random Number Generator
#include "eccrypto.h" // Elliptic Curve
#include "ecp.h" // F(p) EC
#include "integer.h" // Integer Operations
#include "base64.h" // Encodeing-Decoding
// 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; // 1010 1010 1010 1010 0000 0000 0000 0000
const unsigned int FEATURE_MAGIC_MASK = 0xFFFF << 16; // 1111 1111 1111 1111 0000 0000 0000 0000
//
// The following parameters were generated using Elliptic Curve Builder
// ECB can be downloaded at http://www.ellipsa.net/
//
// Curve of Order R*S over GF(P)
// -----------------------------
// P = 4727818631 (33 bits)
// R = 1575934013
// S = 3
// A = 3145529313
// B = -1347526597
//
// Point of Order R
// ----------------
// X = -1556174464
// Y = -2217668713
int main(int argc, char* argv[]) {
byte* CipherText = NULL;
byte* EncodedText = NULL;
byte* DecodedText = NULL;
try {
CryptoPP::AutoSeededRandomPool rng;
// User Defined Domain Parameters
CryptoPP::Integer p("4727818631");
CryptoPP::Integer n("1575934013"); // R from ECB
CryptoPP::Integer h("3"); // S from ECB, must be <= 4
CryptoPP::Integer a("3145529313");
CryptoPP::Integer b("-1347526597");
CryptoPP::Integer x("-1496737238");
CryptoPP::Integer y("-1055837300");
CryptoPP::ECP ec( p, a, b );
CryptoPP::ECIES< CryptoPP::ECP >::PrivateKey PrivateKey;
CryptoPP::ECIES< CryptoPP::ECP >::PublicKey PublicKey;
//
// Curve Initialization and Key Generation
//
PrivateKey.Initialize( ec, CryptoPP::ECP::Point( x, y ), n, h );
PrivateKey.MakePublicKey( PublicKey );
//
// Key Validation
//
// User Defined Domain Parameters always fail, even at level 0???
//
// From cryptlib.h, Validate(...):
//
// 0 - using this object won't cause a crash or exception (rng is ignored)
// 1 - this object will probably function (encrypt, sign, etc.) correctly
// (but may not check for weak keys and such)
// 2 - make sure this object will function correctly, and do reasonable
// security checks
// 3 - do checks that may take a long time
//
// if( false == PrivateKey.Validate( rng, 0 ) )
// { throw std::string( "Private Key Validation" ); }
// if( false == PublicKey.Validate( rng, 0 ) )
// { throw std::string( "Public Key Validation" ); }
// Encryptor and Decryptor
CryptoPP::ECIES< CryptoPP::ECP >::Encryptor Encryptor( PublicKey );
CryptoPP::ECIES< CryptoPP::ECP >::Decryptor Decryptor( PrivateKey );
// Message
unsigned int Features = 0;
Features |= FEATURE_MAGIC;
Features |= FEATURE_USE_WIDGETS;
Features |= FEATURE_USE_SQUARES;
// Runtime Sizes...
unsigned int PlainTextLength = sizeof( Features );
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 64 Encoding
CryptoPP::Base64Encoder Encoder;
Encoder.Put( CipherText, CipherTextLength );
Encoder.MessageEnd();
// Scratch for Base 64 Encoded Ciphertext
unsigned int EncodedTextLength = Encoder.MaxRetrievable();
EncodedText = new byte[ EncodedTextLength + 1 ];
if( NULL == EncodedText )
{ throw std::string( "Base64 EncodedText Allocation Failure" ); }
EncodedText[ EncodedTextLength ] = '\0';
// Fetch Base 64 Encoded Ciphertext
Encoder.Get( EncodedText, EncodedTextLength );
//
// Break the Product Key Here
//
// Diagnostics...
std::cout << "Encoded Text Before Tampering:" << std::endl << EncodedText << std::endl;
// Output
if( FEATURE_EVALUATION == (Features & FEATURE_EVALUATION ) )
{ std::cout << "Evaluation Edition." << std::endl; }
if( FEATURE_USE_SQUARES == (Features & FEATURE_USE_SQUARES) )
{ std::cout << "Operations are permitted on Squares." << std::endl; }
if( FEATURE_USE_CIRCLES == (Features & FEATURE_USE_CIRCLES) )
{ std::cout << "Operations are permitted on Circles." << std::endl; }
if( FEATURE_USE_WIDGETS == (Features & FEATURE_USE_WIDGETS) )
{ std::cout << "Operations are permitted on Widgets." << std::endl; }
std::cout << std::endl << "********************" << std::endl << std::endl;
// The folllowing introduces 1 random error
EncodedText[ 1 ] = 'W';
// The folllowing introduces multiple random errors
// Guessing at a Product Key
/*
char ch = 'A';
for( unsigned int i = 0; i < EncodedTextLength - 1; i += 3 )
{
EncodedText[ i ] = ch++;
}
*/
// Diagnostics...
std::cout << "Encoded Text After Tampering:" << std::endl << EncodedText << std::endl;
// Base 64 Decoding
CryptoPP::Base64Decoder Decoder;
Decoder.Put( EncodedText, EncodedTextLength );
Decoder.MessageEnd();
// Scratch for Base 64 Decoded Ciphertext
unsigned int DecodedTextLength = Decoder.MaxRetrievable();
DecodedText = new byte[ DecodedTextLength ];
if( NULL == DecodedText )
{ throw std::string( "Base64 DecodedText Allocation Failure" ); }
// Fetch Base 64 Decoded Ciphertext
Decoder.Get( DecodedText, DecodedTextLength );
// Scratch for Decryption
unsigned int RecoveredTextLength = Decryptor.MaxPlaintextLength( DecodedTextLength );
if( 0 == RecoveredTextLength )
{ throw std::string("ciphertextLength is not valid (too long or too short)"); }
// Decryption Buffer
unsigned int RecoveredText = static_cast<int>( -1 ); // 1111 ... 1111
if( NULL == RecoveredText )
{ throw std::string( "RecoveredText CipherText Allocation Failure" ); }
// Decryption
Decryptor.Decrypt( rng, DecodedText, DecodedTextLength,
reinterpret_cast<byte *>( &RecoveredText ) );
// Output
if( FEATURE_MAGIC != (RecoveredText & FEATURE_MAGIC_MASK ) )
{
std::cout << "Invalid Product Key!" << std::endl;
}
else
{
if( FEATURE_EVALUATION == (RecoveredText & FEATURE_EVALUATION ) )
{ std::cout << "Evaluation Edition." << std::endl; }
if( FEATURE_USE_SQUARES == (RecoveredText & FEATURE_USE_SQUARES) )
没有合适的资源?快使用搜索试试~ 我知道了~
ecctest4.zip_Elliptic curve
共5个文件
cpp:2个
dsw:1个
h:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 185 浏览量
2022-09-24
14:45:51
上传
评论
收藏 5KB ZIP 举报
温馨提示
http://tecknolojia.blogspot.com/2008/01/elliptic-curve-cryptography-is-not-your.html where u can get info about elliptic curve cryptography suggestions
资源推荐
资源详情
资源评论
收起资源包目录
ecctest4.zip (5个子文件)
ecctest4
ecctest4.cpp 9KB
ecctest4.dsp 4KB
ecctest4.dsw 539B
StdAfx.cpp 295B
StdAfx.h 667B
共 5 条
- 1
资源评论
邓凌佳
- 粉丝: 76
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功