gpt4 book ai didi

c++ - 使用带有安全字符串的openssl加密文件

转载 作者:行者123 更新时间:2023-12-02 10:27:50 27 4
gpt4 key购买 nike

我正在尝试使用c++中的openssl加密文件。
我在evp-encrypt.cxx中使用examplecode,其中将一种称为secure_sting的新数据类型用于加密。
我的问题是我不知道如何以仍然可以使用secure_string数据类型进行加密的方式读取文件。
重要的代码部分是:

template <typename T>
struct zallocator
{
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;

pointer address (reference v) const {return &v;}
const_pointer address (const_reference v) const {return &v;}

pointer allocate (size_type n, const void* hint = 0) {
if (n > std::numeric_limits<size_type>::max() / sizeof(T))
throw std::bad_alloc();
return static_cast<pointer> (::operator new (n * sizeof (value_type)));
}

void deallocate(pointer p, size_type n) {
OPENSSL_cleanse(p, n*sizeof(T));
::operator delete(p);
}

size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof (T);
}

template<typename U>
struct rebind
{
typedef zallocator<U> other;
};

void construct (pointer ptr, const T& val) {
new (static_cast<T*>(ptr) ) T (val);
}

void destroy(pointer ptr) {
static_cast<T*>(ptr)->~T();
}

#if __cpluplus >= 201103L
template<typename U, typename... Args>
void construct (U* ptr, Args&& ... args) {
::new (static_cast<void*> (ptr) ) U (std::forward<Args> (args)...);
}

template<typename U>
void destroy(U* ptr) {
ptr->~U();
}
#endif
};

typedef unsigned char byte;
typedef std::basic_string<char, std::char_traits<char>, zallocator<char> > secure_string;
using EVP_CIPHER_CTX_free_ptr = std::unique_ptr<EVP_CIPHER_CTX, decltype(&::EVP_CIPHER_CTX_free)>;

void gen_params(byte key[KEY_SIZE], byte iv[BLOCK_SIZE]);
void aes_encrypt(const byte key[KEY_SIZE], const byte iv[BLOCK_SIZE], const secure_string& ptext, secure_string& ctext);
void aes_decrypt(const byte key[KEY_SIZE], const byte iv[BLOCK_SIZE], const secure_string& ctext, secure_string& rtext);

// g++ -Wall -std=c++11 evp-encrypt.cxx -o evp-encrypt.exe -lcrypto
int main(int argc, char* argv[])
{
// Load the necessary cipher
EVP_add_cipher(EVP_aes_256_cbc());

// plaintext, ciphertext, recovered text
secure_string ptext = "secret_text";
secure_string ctext, rtext;

byte key[KEY_SIZE], iv[BLOCK_SIZE];
gen_params(key, iv);

aes_encrypt(key, iv, ptext, ctext);
aes_decrypt(key, iv, ctext, rtext);
我想要类似的东西:
    ...
// Load the necessary cipher
EVP_add_cipher(EVP_aes_256_cbc());

// Read file
ifstream f("plain.txt");
string str;
if(f){
ostringstream aa;
aa << f.rdbuf();
str = aa.str();
}

// plaintext, ciphertext, recovered text
secure_string ptext = str;
secure_string ctext, rtext;
...
但是这样我得到以下错误:
 error: conversion from ‘std::__cxx11::string {aka std::__cxx11::basic_string<char>}’ to non-scalar type ‘secure_string {aka std::__cxx11::basic_string<char, std::char_traits<char>, zallocator<char> >}’ requested
使用 secure_string时如何读取文件,或者是没有 secure_string的更好的解决方案?

最佳答案

ptext = str.c_str();解决了问题

关于c++ - 使用带有安全字符串的openssl加密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63538976/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com