class EncoderRSA{ public: EncoderRSA(); voID keyGeneration(); std::string encrypt(std::string plainText); std::string decrypt(std::string cypher); voID setRsaPublicKey(char *publicKeyInChararray); char *getRsaPublicKey(); private: autoSeededRandomPool prng; // Pseudo Random Number Generator ECIES<ECP>::Decryptor rsaDecryptor; ECIES<ECP>::Encryptor rSAEncryptor;};
而且,严格来说,素数(问题)方法:
char *EncoderRSA::getRsaPublicKey() { std::string file = "publicKey.txt"; //Save public key in file fileSink sink(file.c_str()); this->rSAEncryptor.GetPublicKey().Save(sink); //Read file with public key into the buffer std::ifstream infile (file.c_str(),std::ifstream::binary); if (!infile.is_open()) { std::cout << "Can't open file to write" << std::endl; exit(1); } // get size of file infile.seekg (0,infile.end); long size = infile.tellg(); infile.seekg (0); // allocate memory for file content char* buffer = new char[size]; infile.read (buffer,size); infile.close(); return buffer;}voID EncoderRSA::setRsaPublicKey(char *publicKeyInChararray) { std::string file = "publicKey.txt"; int size = strlen(publicKeyInChararray); //Write received public key in file std::ofstream outfile (file.c_str(),std::ofstream::binary); if (!outfile.is_open()) { std::cout << "Can't open file to write" << std::endl; exit(1); } outfile.write (publicKeyInChararray,size); outfile.close(); // release dynamically-allocated memory delete[] publicKeyInChararray; //Load public key from file fileSource source(file.c_str(),true); this->rSAEncryptor.AccesspublicKey().Load(source);}
main.cpp代码:
int main() { char *buffer = keysEncoder.getRsaPublicKey(); cout << "buffer: " << buffer << endl; //... //send buffer //receive buffer from other sIDe //.. keysEncoder.setRsaPublicKey(buffer); string decoded = keysEncoder.decrypt(cypher); cout << "decoded: " << decoded << endl; return 0;}
但它因错误而崩溃:
terminate called after throwing an instance of 'CryptopP::BERDecoderErr'wait(): BER decode errorAborted (core dumped)Process returned 134 (0x86) execution time: 2.891
为什么?
解决方法 我删除了对RSA的引用,因为它看起来像是在使用ECIES.干得好.terminate called after throwing an instance of ‘CryptopP::BERDecoderErr’
显然,你需要设置一个try / catch:
try{ ...}catch(const BERDecoderErr& ex){ cerr << ex.what() << endl;}
06001
ASN.1 / DER编码可能会嵌入NulL,因此您无法使用传统的C字符串对其进行 *** 作.
这可能应该返回一个std :: string,因此输出不会在第一个NulL字符处被截断:
// get size of fileinfile.seekg (0,infile.end);long size = infile.tellg();infile.seekg (0);// allocate memory for file contentstring buffer(size,'0');infile.read (&buffer[0],size);infile.close();return buffer;
可以在Read whole ASCII file into C++ std::string找到另一种从文件执行读取的方法:
std::ifstream infile (file.c_str(),std::ifstream::binary);std::string buffer((std::istreambuf_iterator<char>(infile)),std::istreambuf_iterator<char>());
另一种方法是确保输出和输入中不存在NulL:
string Encoder::getPublicKey() { string encodedKey; HexEncoder sink(new StringSink(encodedKey)); Encryptor.GetPublicKey().Save(sink); return encodedKey;}
和:
voID Encoder::setPublicKey(const string& encodedKey) { StringSource source(encodedKey,new HexDecoder()); Encryptor.AccesspublicKey().Load(source);}
上面的代码使用StringSource和StringSink,因此它可以在内存中运行.如果您确实需要磁盘上的中间文件,请使用fileSource和fileSink.
总结以上是内存溢出为你收集整理的c – ECIES公钥序列化全部内容,希望文章能够帮你解决c – ECIES公钥序列化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)