80 lines
2.6 KiB
C++
80 lines
2.6 KiB
C++
#include "helper_funcs.h"
|
|
#include <cryptopp/aes.h>
|
|
#include <cryptopp/base64.h>
|
|
#include <cryptopp/filters.h>
|
|
#include <cryptopp/modes.h>
|
|
#include <iomanip>
|
|
#include <iostream>
|
|
#include <nlohmann/json.hpp>
|
|
#include <string>
|
|
|
|
using nlohmann::json;
|
|
|
|
int encrypt() {
|
|
// Key and IV (Initialization Vector) for AES
|
|
CryptoPP::byte key[CryptoPP::AES::DEFAULT_KEYLENGTH] = {
|
|
'k', 'e', 'y', '1', '2', '3', '4', '5',
|
|
'6', '7', '8', '9', '1', '0', '1', '1'};
|
|
CryptoPP::byte iv[CryptoPP::AES::BLOCKSIZE];
|
|
for (int i = 0; i < 16; i++) {
|
|
iv[i] = static_cast<char>(get_random_num(33, 126));
|
|
}
|
|
|
|
// Message to be encrypted
|
|
std::string plainText = "Hello, AES!";
|
|
std::string cipherText; // Declare cipherText variable
|
|
|
|
// Encrypt using AES in CBC mode
|
|
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption encryptor(key, sizeof(key), iv);
|
|
CryptoPP::StringSource(plainText, true,
|
|
new CryptoPP::StreamTransformationFilter(
|
|
encryptor, new CryptoPP::StringSink(cipherText)));
|
|
|
|
// Encode the encrypted message in Base64 format
|
|
std::string base64Encoded;
|
|
CryptoPP::StringSource(
|
|
cipherText, true,
|
|
new CryptoPP::Base64Encoder(new CryptoPP::StringSink(base64Encoded)));
|
|
|
|
// Display the Base64 encoded encrypted message
|
|
std::cout << "Encrypted Text (Base64): " << base64Encoded << std::endl;
|
|
|
|
return 0;
|
|
}
|
|
|
|
int decrypt() {
|
|
// Key and IV (Initialization Vector) for AES
|
|
CryptoPP::byte key[CryptoPP::AES::DEFAULT_KEYLENGTH] = {
|
|
'k', 'e', 'y', '1', '2', '3', '4', '5',
|
|
'6', '7', '8', '9', '1', '0', '1', '1'};
|
|
CryptoPP::byte iv[CryptoPP::AES::BLOCKSIZE] = {'i', 'v', '1', '2', '3', '4',
|
|
'5', '6', '7', '8', '9', '1',
|
|
'0', '1', '1', '1'};
|
|
|
|
// Base64 encoded encrypted message (replace with actual encrypted message)
|
|
std::string base64EncodedCipherText = "PgKnjjHY2xz/LRWgg1ob1Q==";
|
|
std::string cipherText;
|
|
|
|
// Decode the Base64 encoded message
|
|
CryptoPP::StringSource(
|
|
base64EncodedCipherText, true,
|
|
new CryptoPP::Base64Decoder(new CryptoPP::StringSink(cipherText)));
|
|
|
|
// Decrypt using AES in CBC mode
|
|
std::string decryptedText;
|
|
CryptoPP::CBC_Mode<CryptoPP::AES>::Decryption decryptor(key, sizeof(key), iv);
|
|
|
|
CryptoPP::StringSource(
|
|
cipherText, true,
|
|
new CryptoPP::StreamTransformationFilter(
|
|
decryptor, new CryptoPP::StringSink(decryptedText)));
|
|
|
|
// Display the decrypted message
|
|
std::cout << "Decrypted Text: " << decryptedText << std::endl;
|
|
|
|
return 0;
|
|
}
|
|
int main() {
|
|
decrypt();
|
|
return 0;
|
|
} |