CppSecurity  1.1.1.0
C++ Security Library
cipher_encrypt.cpp

Cipher encrypt example

#include "string/encoding.h"
#include <iostream>
int main(int argc, char** argv)
{
// Show the password
std::password password = (argc > 1) ? argv[1] : "passw0rd";
std::cout << "Password: " << password << std::endl;
// Show the salt
std::string salt = (argc > 2) ? argv[2] : "12345678";
std::cout << "Salt: " << salt << std::endl;
// Initialize the cipher with a password and salt
cipher.Initialize(password, salt);
std::cout << "Cipher name: " << cipher.name() << std::endl;
std::cout << "Cipher iterations: " << cipher.iterations() << std::endl;
std::cout << "Please enter strings to encrypt. Enter '0' to exit..." << std::endl;
// Perform text input
std::string line;
while (getline(std::cin, line))
{
if (line.empty() || (line == "0"))
break;
std::string encrypted = cipher.Encrypt(line);
std::string encoded = CppCommon::Encoding::Base64Encode(encrypted);
// Show the encoded string
std::cout << encoded << std::endl;
}
return 0;
}
Cipher definition.
void Initialize(std::string_view secret)
Initialize the cipher with the given secret key.
Definition: cipher.cpp:86
const std::string & name() const noexcept
Get the cipher algorithm name.
Definition: cipher.h:77
size_t iterations() const noexcept
Get the count of key hashing iterations.
Definition: cipher.h:79
std::string Encrypt(std::string_view str)
Encrypt the given string.
Definition: cipher.cpp:117
Password string.
Definition: password.h:53