加密/解密时加密++明确的破坏? [英] Crypto++ explicit destruction during encryption/decryption?
问题描述
我在wiki中发现,传入函数时的一些对象被销毁,但没有示例我确实使用的是那里,所以我只想确定。
CryptoPP :: AutoSeededRandomPool prng ;
//密钥生成
字节密钥[AES :: DEFAULT_KEYLENGTH];
prng.GenerateBlock(key,sizeof(key));
// IV代
byte iv [AES :: BLOCKSIZE];
prng.GenerateBlock(iv,sizeof(iv));
//打印键
encoded.clear();
StringSource(key,sizeof(key),true,new HexEncoder(new StringSink(encoded)));
cout<<< key:<<编码< ENDL;
cout<<< 键尺寸:<< sizeof(key)<< ENDL;
// print iv
encoded.clear();
StringSource(iv,sizeof(iv),true,new HexEncoder(new StringSink(encoded)));
cout<<< iv:<<编码< ENDL;
cout<<< 尺寸iv:< (iv) ENDL;
//查看下面的函数
encrypt_file(inFile,outFile,key,iv,err);
inFile.close();
outFile.close();
在此函数中,由于某些原因,字节数组被截断
Encrypt_file
bool encrypt_file(std :: ifstream& inFile,
std :: ofstream& outFile,
const byte * key,const byte * iv,
std :: string& errMsg)
{
std :: string encoded;
// print key
encoded.clear();
StringSource(key,sizeof(key),true,new HexEncoder(new StringSink(encoded)));
cout<<< key:<<编码< ENDL;
cout<<< 键尺寸:<< sizeof(key)<< ENDL;
// print iv
encoded.clear();
StringSource(iv,sizeof(iv),true,new HexEncoder(new StringSink(encoded)));
cout<<< iv:<<编码< ENDL;
cout<<< 尺寸iv:< (iv) ENDL;
try {
CryptoPP :: CBC_Mode< CryptoPP :: AES> :: Encryption e;
e.SetKeyWithIV(key,sizeof(key),iv);
CryptoPP :: FileSource(inFile,true,新的CryptoPP :: StreamTransformationFilter(e,新的CryptoPP :: FileSink(outFile)));
inFile.close();
outFile.close();
}
catch(CryptoPP :: Exception& e){
errMsg = e.GetWhat();
返回false;
}
返回true;
}
输出:
键:6574D7BDFD0DD3BC59CD3846D4A196A8
键尺寸:16
iv:1B4ED692F91A32246B41F63F6B8C6EAA
尺寸iv:16
键:6574D7BDFD0DD3BC
尺寸的关键:8
iv:1B4ED692F91A3224
iv:8
不,你没有。您创建的对象具有自动存储持续时间 ,这意味着它们的析构函数将在其范围结束时自动调用。此外,您使用 new
传递的参数将由Crypto ++对象拥有,并且其相应的析构函数将为您释放内存。它们属于接收器或过滤器的类别,并且您也可以传递所有权。详情请见:
https: //www.cryptopp.com/wiki/Pipelining#Ownership
基本上这是会发生什么(超简化例子):
#include< iostream>
struct Foo {};
class X
{
Foo * p_;
public:
X(Foo * p):p_(p){}
//我们还需要一个副本ctor和复制赋值运算符,这里忽略
〜X ()
{
std :: cout<<< 释放记忆... \\\
;
删除p_;
}
};
int main()
{
X x(new Foo()); //下沉,没有内存泄漏
}
我有说这是迄今为止我最不喜欢的软件设计风格。可以使用模板和混合来实现类似的操作(请参阅基于策略的设计) ,没有指针浮动,没有明确的所有权。
I wrote some wrapper functions to encrypt/decrypt files using crypto++. I tried looking in the wiki but could find my answer. I am wondering if I need to explicitly destroy my objects that are created?
I found in the wiki that some objects when passed into functions are destroyed for you, but no examples of my exact use were there so I just wanted to be sure.
CryptoPP::AutoSeededRandomPool prng;
//Key generation
byte key[AES::DEFAULT_KEYLENGTH];
prng.GenerateBlock(key, sizeof(key));
//IV generation
byte iv[AES::BLOCKSIZE];
prng.GenerateBlock(iv, sizeof(iv));
//print key
encoded.clear();
StringSource(key, sizeof(key), true, new HexEncoder(new StringSink(encoded)));
cout << "key: " << encoded << endl;
cout << "Size of key: " << sizeof(key) << endl;
//print iv
encoded.clear();
StringSource(iv, sizeof(iv), true, new HexEncoder(new StringSink(encoded)));
cout << "iv: " << encoded << endl;
cout << "Size of iv: " << sizeof(iv) << endl;
//See function below
encrypt_file(inFile, outFile, key, iv, err);
inFile.close();
outFile.close();
Once in this function the bytes arrays are truncated for some reason
Encrypt_file
bool encrypt_file(std::ifstream& inFile,
std::ofstream& outFile,
const byte* key, const byte* iv,
std::string& errMsg)
{
std::string encoded;
//print key
encoded.clear();
StringSource(key, sizeof(key), true, new HexEncoder(new StringSink(encoded)));
cout << "key: " << encoded << endl;
cout << "Size of key: " << sizeof(key) << endl;
//print iv
encoded.clear();
StringSource(iv, sizeof(iv), true, new HexEncoder(new StringSink(encoded)));
cout << "iv: " << encoded << endl;
cout << "Size of iv: " << sizeof(iv) << endl;
try {
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption e;
e.SetKeyWithIV(key, sizeof(key), iv);
CryptoPP::FileSource(inFile, true, new CryptoPP::StreamTransformationFilter(e, new CryptoPP::FileSink(outFile)));
inFile.close();
outFile.close();
}
catch (CryptoPP::Exception& e) {
errMsg = e.GetWhat();
return false;
}
return true;
}
Output:
key: 6574D7BDFD0DD3BC59CD3846D4A196A8
Size of key: 16
iv: 1B4ED692F91A32246B41F63F6B8C6EAA
Size of iv: 16
key: 6574D7BDFD0DD3BC
Size of key: 8
iv: 1B4ED692F91A3224
Size of iv: 8
No, you don't. The objects you create have automatic storage duration, which means their destructor will be automatically invoked at the end of their scope. Moreover, the arguments that you pass with new
will be owned by the Crypto++ objects, and their corresponding destructor will release the memory for you. They fall into the category of a sink or a filter, and it turns out that you also pass the ownership. For more details see:
https://www.cryptopp.com/wiki/Pipelining#Ownership
Basically this is what happens (super simplified example):
#include <iostream>
struct Foo{};
class X
{
Foo *p_;
public:
X(Foo* p): p_(p) {}
// we'd also need a copy ctor and copy assignment operator, ignored here
~X()
{
std::cout << "Releasing the memory...\n";
delete p_;
}
};
int main()
{
X x(new Foo()); // sinking, no memory leak
}
I have to say that this is by far my least favourite style of software design. One can use templates and mixins to probably achieve similar things (read about policy based design), without pointers floating around with no clear ownership.
这篇关于加密/解密时加密++明确的破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!