Зашифровать данные на C++

Хочу реализовать систему зашифровки текста через C++. Есть ли какая-нибудь функция по этому поводу? Если нет, какой алгоритм предложите?


Ответы (1 шт):

Автор решения: Sheridan

Вам поможет Crypto++. Сам пользовался, довольно удобно. К сожалению свой код показать не могу (закрытый проект), но пример брал отсюда:

#include <iostream>
 
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/base64.h>
 
std::string encrypt(const std::string& str_in, const std::string& key)
{
    std::string str_out;
    CryptoPP::ECB_Mode<CryptoPP::AES>::Encryption encryption((byte*)key.c_str(), key.length());
 
    CryptoPP::StringSource encryptor(str_in, true, 
            new CryptoPP::StreamTransformationFilter(encryption, 
                new CryptoPP::Base64Encoder(
                    new CryptoPP::StringSink(str_out),
                    false // do not append a newline
                )
            )
    );
    return str_out;
}
 
std::string decrypt(const std::string& str_in, const std::string& key)
{
    std::string str_out;
 
    CryptoPP::ECB_Mode<CryptoPP::AES>::Decryption decryption((byte*)key.c_str(), key.length());
 
    CryptoPP::StringSource decryptor(str_in, true, 
        new CryptoPP::Base64Decoder(
                new CryptoPP::StreamTransformationFilter(decryption, 
                    new CryptoPP::StringSink(str_out)
                )
        )
    );
    return str_out;
}
 
int main(int argc, char *argv[])
{
    std::string str = "hello";
    std::string key = "0123456789123456";
    
    std::string str_encrypted, str_decrypted;
    
    try {
        str_encrypted = encrypt(str, key);
        str_decrypted = decrypt(str_encrypted, key);
    }
    catch (const CryptoPP::Exception& e) {
       std::cerr << e.what() << std::endl;
    }
    
    std::cout << "str_encrypted: " << str_encrypted << std::endl;
    std::cout << "str_decrypted: " << str_decrypted << std::endl;
}

→ Ссылка