当前位置:编程学习 > 网站相关 >>

有没有高人用过Crypto++的ECC类库做加解密.我无法解密,附代码,在线等!!

使用ECIES做ECC加密.已经生成了PUBLICKEY和PRIVATEKEY,现在想把他们保存在文件中并读取PUBLICKEY加密,用PRIVATEKEY解密.失败!在邮件列表中也没有找到相关的原因.我的代码有三个程序,一个用来生成PUBLICKEY和PRIVATEKEY分别存入"privkey.dat"和"pubkey.dat"文件,另一个程序读取"pubkey.dat"内容然后加密一个字符串信息,存放在一个文件"cipher.dat"中.另一个取"privkey.dat"和"cipher.dat"来还原加密信息.结果总失败.代码如下:
第一个生成PRIVATEKEY和PUBLICKEY的代码:

#define __STRING(a) #a
#define __XSTRING(a) __STRING(a)

#define USE_ALGORITHM ECP
#define NAME_ALGORITHM __XSTRING(USE_ALGORITHM)

static ECIES<USE_ALGORITHM>::PrivateKey gPrivateKey;
static ECIES<USE_ALGORITHM>::PublicKey gPublicKey;

//static size_t gTestDataSize=sizeof(gTestData);
static byte gEncryptBuf[1024];
static byte gDecryptBuf[1024];

void main()
{
try
{
cout<<"Testing Elliptic Curve "<<NAME_ALGORITHM<<endl;

AutoSeededRandomPool rng;
char* seed="1234567890";
rng.Put((byte*)seed,strlen(seed));
cout<<"Make seed :"<<seed<<endl;

cout<<"Generating PrivateKey"<<endl;

//generate private key
gPrivateKey.Initialize(rng,ASN1::secp521r1());

char privFilename[128]={0};
strcpy(privFilename,"privkey.dat");
char pubFilename[128]={0};
strcpy(pubFilename,"pubkey.dat");

//generate public key from private key
cout<<"Generating PublicKey"<<endl;
gPrivateKey.MakePublicKey(gPublicKey);

cout<<"Validating PrivateKey"<<endl;
if ( gPrivateKey.Validate(rng,3)==false)
cout<< "PrivateKey invalid?!"<<endl;
else
{
//save the private key into privatekey.dat as hex
//HexEncoder privFile(new FileSink(privFilename));
//gPrivateKey.GetPrivateExponent().DEREncode(privFile);
FileSink fs(privFilename);
gPrivateKey.GetPrivateExponent().DEREncode(fs);
//privFile.MessageEnd();

}
cout<<"Validating PublicKey"<<endl;
if ( gPublicKey.Validate(rng,3)==false)
cout<<"PublicKey invalid?!"<<endl;
else
{
/*HexEncoder pubFile(new FileSink(pubFilename));
gPublicKey.GetGroupParameters().GetCurve().EncodePoint(pubFile,
gPublicKey.GetPublicElement(),true);
pubFile.MessageEnd(); */

FileSink fs(pubFilename);
gPublicKey.GetGroupParameters().GetCurve().EncodePoint(fs,
gPublicKey.GetPublicElement(),true);

}
}
catch(CryptoPP::Exception &e)
{
cout<<"CryptoPP::Exception "<<e.what()<<endl;
}
catch(std::exception &e)
{
cout<<"std::exception "<<e.what()<<endl;
}
cout<<"Press any key to exit..."<<endl;
getchar();
return ;
}

第二个用PUBLIC加密:
static ECIES<ECP>::PublicKey gPublicKey;
static char pubFilename[128]={0};
static char gTestData[133]="hello ecc world! i will be harder and harder!";
static byte gEncryptBuf[1024];
static byte gPublicKeyBuf[1024];
void main()
{
strcpy(pubFilename,"pubkey.dat");
cout<<"Load Public Key"<<endl;

string sContents;

//FileSource(pubFilename,true,new StringSink(sContents));




ECP::Point p;
gPublicKey.AccessGroupParameters().Initialize(CryptoPP::ASN1::secp521r1());
gPublicKey.GetGroupParameters().GetCurve().DecodePoint(p,
FileSource(pubFilename,true,new StringSink(sContents)),gPublicKey.GetGroupParameters().GetCurve().EncodedPointSize(true));
gPublicKey.SetPublicElement(p);

cout<<"Instancing PublicKey encryptor"<<endl;
ECIES<USE_ALGORITHM>::Encryptor encryptor(gPublicKey);

int gTestDataSize=sizeof(gTestData);
cout<<"Using Test Date: "<<gTestData<<endl;
cout<<"Encryping test data"<<gTestDataSize<<" bytes"<<endl;

AutoSeededRandomPool rng;
char* seed="1234567890";
rng.Put((byte*)seed,strlen(seed));
encryptor.Encrypt(rng,(const byte*)gTestData,gTestDataSize,gEncryptBuf);
unsigned int ciphertextLength=encryptor.CiphertextLength(gTestDataSize);
cout<<"Ciphertext Length:"<<ciphertextLength<<endl;
cout<<"Cipthertext :"<<(char*)gEncryptBuf<<endl;

//HexEncoder cipherFile(new FileSink("cipher.dat"));
//cipherFile.Put(gEncryptBuf,ciphertextLength);
//cipherFile.MessageEnd();
//FileSink fs1("cipher.dat");
StringSource(gEncryptBuf,ciphertextLength,true,new FileSink("cipher.dat"));



cout<<"Press any key to return..."<<endl;
getchar();
return;
}

第三个解密的!(主要就是这里了,不知道为什么老出错,用的CRYPTO++的SINK和SOURCE这些东西DEBUG的时候还老出错)
#define __STRING(a) #a
#define __XSTRING(a) __STRING(a)

#define USE_ALGORITHM ECP
#define NAME_ALGORITHM __XSTRING(USE_ALGORITHM)

static ECIES<ECP>::PrivateKey gPrivateKey;
static char privFilename[128]={0};
//static const char gTestData[133]="hello ecc world! i will be harder and harder!";
static byte gDecryptBuf[1024];
static byte gPriv[1024];

void main()
{
strcpy(privFilename,"privkey.dat");

AutoSeededRandomPool rng;
char* seed="1234567890";
rng.Put((byte*)seed,strlen(seed));

//string sContents;
//FileSource(privFilename,true,new StringSink(sContents));
//StringSink ss(sContents);

cout<<"Load the PrivateKey"<<endl;

CryptoPP::Integer x;

string stt;
FileSource(privFilename,true,new StringSink(stt));

int plen=stt.length();
const char* pristr=stt.c_str();
memset(gPriv,0,1024);
memcpy(gPriv,(const byte*)stt.c_str(),plen);
cout<<"privatekey is:"<<reinterpret_cast<char*>(gPriv)<<endl;
x.BERDecode((byte*)pristr,plen);
cout<<"x is :"<<x<<endl;
gPrivateKey.AccessGroupParameters().Initialize(CryptoPP::ASN1::secp521r1());
gPrivateKey.SetPrivateExponent(x);
cout<<"Load completed"<<endl;

cout<<"Loading Encryptotext"<<endl;
string sContents;
char const* cipher="cipher.dat";
//FileSource((const char*)cipher,true,new HexDecoder(new StringSink(sContents)));

FileSource((const char*)cipher,true,new StringSink(sContents));

ECIES<USE_ALGORITHM>::Decryptor decryptor(gPrivateKey);
const char* pstr=strdup(sContents.c_str());
cout<<"cipher text:"<<pstr<<endl;
//byte cip[1024]={0};
int len=sContents.length();
//cout<<"cipher text:"<<cip<<endl;
DecodingResult result(decryptor.Decrypt(rng,(byte*)pstr,len,gDecryptBuf));
//decryptor.Decrypt(rng,(byte*)cip,len,gDecryptBuf);
cout<<"Read length is :"<<len<<endl;
cout<<"Decrypt Length:"<<result.messageLength<<endl;
cout<<"Result:"<<(result.isValidCoding==true?"yes":"no")<<endl;
cout<<"Decrypt Text:"<<(char*)gDecryptBuf<<endl;

cout<<"Press any key exit..."<<endl;
getchar();
delete pstr;
return;

} --------------------编程问答-------------------- 晕了.自己完成了.
还是用CRYPTO++的SOURCE和SINK加上64位或32位编码完成的. --------------------编程问答-------------------- 这个简单啊,网上搜一下就得到答案了. --------------------编程问答-------------------- 做的怎么样了,实现了? --------------------编程问答-------------------- 关注,网络安全,!!!!!!!!!!!!!!!!!
补充:云计算 ,  云安全
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,