Created
July 27, 2017 11:49
-
-
Save herpiko/753af958d6f615ecd092138b2f2c1bcb to your computer and use it in GitHub Desktop.
AES-CBC Base64 Encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "crypto.h" | |
using namespace std; | |
int main() { | |
std::cout << "yo\n"; | |
// preparation | |
std::string keystr = "hiduplah indonesia raya"; | |
unsigned char *key = new unsigned char[keystr.length() + 1]; | |
strcpy((char*)key, keystr.c_str()); | |
std::string datastr = "AOIRESNAIROESRAIONRAIOESNAORESNORIAFULJWULFPJWFULPJFWYULPJFWYUPLJFWUYPLJFWYULPJFWULPJWFYUPLJFWYULPJFWULPJFWYULJPYUWFJPYUFWLJPUYFWLJPUTHNRISTHRSIETNHRIENSTHENISRHTNIERSHTIENRHTEISRNHTESRNHTRSIENTHSRIETNHRISENTHYFULPJWYULJPFWULJPYUFWLPJYWFULPJFWYULJSRTHRIESTNHRSIECUYFLPYFWULPYFWUPLYFWLPUFWLJPFUWLJPUFLYWJPYULFWJPUYFLJPUYFWLJPUFWLJPUFWLJPYUFWIETSNRHEINTHRIESTNHRIESTNHRISETNHRESINTHISTHIERNHTIERSNTHRISETNHIRESNHTIESRTHIRSETNHIRESNTHRIESTNHIERSNTHIRESNHTIRESNTHRIESNTHRSIENTHYUFWLJPUFLJPYULJPUWFLPJUWFLJPYUWFLJPYUWFLJPUYWFLJPYWUFJPH<ETNHIERSNTHSRHDTENSHDTENSRHDTNESRHDUFYULPJYWFUJPYUFLJPYUFWLJPYUFLJPYUFJTESRNHTESINTHNRESDTESRNHDESRNHTDNSRHDTRENDFLUWJGPLWFJPGWULFJGPULWFJPGFLUWJGPLFJGLWFJGPLFWJGPLJWGPLFWGPULFWJGPLUFWGPLUFWJDTNHESRDTENRSHDTNERSHDTERNSHTFULPJGUWLFJGULJPGLFWUPARINSRAISHEANRHSEAIRNHSAIER NSHIEARNSHEAIRSNHAREISNHARESNHDRAENSGULJWFGWULJQFGWQULJFGLWUJGFLUWJQFGLUQWJFGUWQLJFRHSDERNAHDNERHADSNEARHSKBRVCSKBRVACSKBRAVCSKBRAVCNRASTNHRDSTURALJSURLAJGSULJRAGSULRAJGSULARJGSULRAJGSARSJGALJURSGULARJSGDURALJSDRAHNVSRABKVCSKBRAVCSKBRAVCSKBRAVCSKBRACSKRBAVCSKBRAVCSKBRAVCSKRABSCANRHDSTRALDTSARGSURALJGSULJARGSULARJGSULARJGSULRAJGSDARNHSVRBKAVNJGSULJARGDSRAJTSVJRAULGVSJLARVSLJRADVSLRAJDVSLRAJDVSLRASVDLRSNAIROESRAIONRAIOESNAORESNORIAFULJWULFPJWFULPJFWYULPJFWYUPLJFWUYPLJFWYULPJFWULPJWFYUPLJFWYULPJFWULPJFWYULJPYUWFJPYUFWLJPUYFWLJPUTHNRISTHRSIETNHRIENSTHENISRHTNIERSHTIENRHTEISRNHTESRNHTRSIENTHSRIETNHRISENTHYFULPJWYULJPFWULJPYUFWLPJYWFULPJFWYULJSRTHRIESTNHRSIECUYFLPYFWULPYFWUPLYFWLPUFWLJPFUWLJPUFLYWJPYULFWJPUYFLJPUYFWLJPUFWLJPUFWLJPYUFWIETSNRHEINTHRIESTNHRIESTNHRISETNHRESINTHISTHIERNHTIERSNTHRISETNHIRESNHTIESRTHIRSETNHIRESNTHRIESTNHIERSNTHIRESNHTIRESNTHRIESNTHRSIENTHYUFWLJPUFLJPYULJPUWFLPJUWFLJPYUWFLJPYUWFLJPUYWFLJPYWUFJPH<ETNHIERSNTHSRHDTENSHDTENSRHDTNESRHDUFYULPJYWFUJPYUFLJPYUFWLJPYUFLJPYUFJTESRNHTESINTHNRESDTESRNHDESRNHTDNSRHDTRENDFLUWJGPLWFJPGWULFJGPULWFJPGFLUWJGPLFJGLWFJGPLFWJGPLJWGPLFWGPULFWJGPLUFWGPLUFWJDTNHESRDTENRSHDTNERSHDTERNSHTFULPJGUWLFJGULJPGLFWUPARINSRAISHEANRHSEAIRNHSAIER NSHIEARNSHEAIRSNHAREISNHARESNHDRAENSGULJWFGWULJQFGWQULJFGLWUJGFLUWJQFGLUQWJFGUWQLJFRHSDERNAHDNERHADSNEARHSKBRVCSKBRVACSKBRAVCSKBRAVCNRASTNHRDSTURALJSURLAJGSULJRAGSULRAJGSULARJGSULRAJGSARSJGALJURSGULARJSGDURALJSDRAHNVSRABKVCSKBRAVCSKBRAVCSKBRAVCSKBRACSKRBAVCSKBRAVCSKBRAVCSKRABSCANRHDSTRALDTSARGSURALJGSULJARGSULARJGSULARJGSULRAJGSDARNHSVRBKAVNJGSULJARGDSRAJTSVJRAULGVSJLARVSLJRADVSLRAJDVSLRAJDVSLRASVDLRSNAIROESRAIONRAIOESNAORESNORIAFULJWULFPJWFULPJFWYULPJFWYUPLJFWUYPLJFWYULPJFWULPJWFYUPLJFWYULPJFWULPJFWYULJPYUWFJPYUFWLJPUYFWLJPUTHNRISTHRSIETNHRIENSTHENISRHTNIERSHTIENRHTEISRNHTESRNHTRSIENTHSRIETNHRISENTHYFULPJWYULJPFWULJPYUFWLPJYWFULPJFWYULJSRTHRIESTNHRSIECUYFLPYFWULPYFWUPLYFWLPUFWLJPFUWLJPUFLYWJPYULFWJPUYFLJPUYFWLJPUFWLJPUFWLJPYUFWIETSNRHEINTHRIESTNHRIESTNHRISETNHRESINTHISTHIERNHTIERSNTHRISETNHIRESNHTIESRTHIRSETNHIRESNTHRIESTNHIERSNTHIRESNHTIRESNTHRIESNTHRSIENTHYUFWLJPUFLJPYULJPUWFLPJUWFLJPYUWFLJPYUWFLJPUYWFLJPYWUFJPH<ETNHIERSNTHSRHDTENSHDTENSRHDTNESRHDUFYULPJYWFUJPYUFLJPYUFWLJPYUFLJPYUFJTESRNHTESINTHNRESDTESRNHDESRNHTDNSRHDTRENDFLUWJGPLWFJPGWULFJGPULWFJPGFLUWJGPLFJGLWFJGPLFWJGPLJWGPLFWGPULFWJGPLUFWGPLUFWJDTNHESRDTENRSHDTNERSHDTERNSHTFULPJGUWLFJGULJPGLFWUPARINSRAISHEANRHSEAIRNHSAIER NSHIEARNSHEAIRSNHAREISNHARESNHDRAENSGULJWFGWULJQFGWQULJFGLWUJGFLUWJQFGLUQWJFGUWQLJFRHSDERNAHDNERHADSNEARHSKBRVCSKBRVACSKBRAVCSKBRAVCNRASTNHRDSTURALJSURLAJGSULJRAGSULRAJGSULARJGSULRAJGSARSJGALJURSGULARJSGDURALJSDRAHNVSRABKVCSKBRAVCSKBRAVCSKBRAVCSKBRACSKRBAVCSKBRAVCSKBRAVCSKRABSCANRHDSTRALDTSARGSURALJGSULJARGSULARJGSULARJGSULRAJGSDARNHSVRBKAVNJGSULJARGDSRAJTSVJRAULGVSJLARVSLJRADVSLRAJDVSLRAJDAVSRAVDSLHARDVSG"; | |
std::cout << "Text to be encrypted : \n"; | |
std::cout << datastr << "\n"; | |
unsigned char *data = new unsigned char[datastr.length() + 1]; | |
strcpy((char*)data, datastr.c_str()); | |
unsigned char *iv = (unsigned char *)"01234556789012345"; | |
unsigned char ciphertext[5000]; | |
unsigned char decryptedtext[5000]; | |
int ciphertext_len, decryptedtext_len; | |
ERR_load_crypto_strings(); | |
OpenSSL_add_all_algorithms(); | |
OPENSSL_config(NULL); | |
Crypto crypto; | |
ciphertext_len = crypto.encrypt(data, datastr.length(), key, iv, ciphertext); | |
std::cout << "Encrypted text len :\n"; | |
std::cout << ciphertext_len << "\n"; | |
std::cout << "Encrypted text :\n"; | |
std::cout << reinterpret_cast<const char*>(ciphertext) << "\n"; | |
unsigned char *iv2 = (unsigned char *)"01234556789012345"; | |
decryptedtext_len = crypto.decrypt(ciphertext, ciphertext_len, key, iv2, decryptedtext); | |
std::cout << decryptedtext_len << "\n"; | |
std::cout << "Decrypted text :\n"; | |
std::cout << reinterpret_cast<const char*>(decryptedtext) << "\n"; | |
std::cout << "Convert the unsigned to base64" << std::endl; | |
std::string b64 = crypto.base64Encode(ciphertext, ciphertext_len); | |
std::cout << b64 << std::endl; | |
std::cout << "Convert back to unsigned " << std::endl; | |
unsigned char* encrypteduc = crypto.base64Decode(b64); | |
std::cout << reinterpret_cast<const char*>(encrypteduc) << "\n"; | |
std::cout << "Decrypt from unsigned b64 " << std::endl; | |
std::cout << "Encrypted text len :\n"; | |
std::cout << ciphertext_len << "\n"; | |
unsigned char decryptedtext2[5000]; | |
int decryptedtext_len2; | |
unsigned char *iv3 = (unsigned char *)"01234556789012345"; | |
decryptedtext_len2 = crypto.decrypt(encrypteduc, ciphertext_len, key, iv3, decryptedtext2); | |
std::cout << "Decrypted text len :\n"; | |
std::cout << decryptedtext_len2 << "\n"; | |
std::cout << "Decrypted text :\n"; | |
std::cout << reinterpret_cast<const char*>(decryptedtext) << "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
include "crypto.h" | |
#include <openssl/sha.h> | |
#include <iostream> | |
void Crypto::handleErrors(void) { | |
ERR_print_errors_fp(stderr); | |
//abort(); | |
} | |
/* | |
std::string Crypto::hashSHA256(std::string str) { | |
unsigned char hash[SHA256_DIGEST_LENGTH]; | |
char output[65]; | |
SHA256_CTX sha256; | |
SHA256_Init(&sha256); | |
SHA256_Update(&sha256, str, strlen(str)); | |
SHA256_Final(hash, &sha256); | |
int i = 0; | |
for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { | |
sprintf(output + (i*2), "%02x", hash[i]); | |
} | |
return output; | |
} | |
*/ | |
int Crypto::encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, | |
unsigned char *iv, unsigned char *ciphertext) { | |
EVP_CIPHER_CTX *ctx; | |
int len, ciphertext_len; | |
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); | |
if(1 != EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) | |
handleErrors(); | |
if(1 != EVP_EncryptUpdate(ctx, ciphertext, &len, plaintext, plaintext_len)) | |
handleErrors(); | |
ciphertext_len = len; | |
if(1 != EVP_EncryptFinal_ex(ctx, ciphertext + len, &len)) handleErrors(); | |
ciphertext_len += len; | |
EVP_CIPHER_CTX_free(ctx); | |
return ciphertext_len; | |
} | |
int Crypto::decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, | |
unsigned char *iv, unsigned char *plaintext) | |
{ | |
EVP_CIPHER_CTX *ctx; | |
int len, plaintext_len; | |
//ciphertext_len = 16; | |
if(!(ctx = EVP_CIPHER_CTX_new())) handleErrors(); | |
if(1 != EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv)) | |
handleErrors(); | |
if(1 != EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, ciphertext_len)) | |
handleErrors(); | |
plaintext_len = len; | |
if(1 != EVP_DecryptFinal_ex(ctx, plaintext + len, &len)) handleErrors(); | |
plaintext_len += len; | |
EVP_CIPHER_CTX_free(ctx); | |
return plaintext_len; | |
} | |
/* | |
std::string Crypto::randomString() | |
{ | |
int len = rand() % 1000 + 1; | |
srand(time(0)); | |
std::string str = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; | |
std::string newstr; | |
int pos; | |
while(newstr.size() != len) { | |
pos = ((rand() % (str.size() - 1))); | |
newstr += str.substr(pos,1); | |
} | |
return newstr: | |
} | |
*/ | |
bool Crypto::isBase64(unsigned char c) { | |
return (isalnum(c) || (c == '+') || (c == '/')); | |
} | |
std::string Crypto::base64Encode(unsigned char const* bytes_to_encode, unsigned int in_len) { | |
const std::string base64_chars = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
"abcdefghijklmnopqrstuvwxyz" | |
"0123456789+/"; | |
std::string ret; | |
int i = 0; | |
int j = 0; | |
unsigned char char_array_3[3]; | |
unsigned char char_array_4[4]; | |
while (in_len--) { | |
char_array_3[i++] = *(bytes_to_encode++); | |
if (i == 3) { | |
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; | |
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); | |
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); | |
char_array_4[3] = char_array_3[2] & 0x3f; | |
for(i = 0; (i <4) ; i++) | |
ret += base64_chars[char_array_4[i]]; | |
i = 0; | |
} | |
} | |
if (i) | |
{ | |
for(j = i; j < 3; j++) | |
char_array_3[j] = '\0'; | |
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; | |
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); | |
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); | |
char_array_4[3] = char_array_3[2] & 0x3f; | |
for (j = 0; (j < i + 1); j++) | |
ret += base64_chars[char_array_4[j]]; | |
while((i++ < 3)) | |
ret += '='; | |
} | |
return ret; | |
} | |
unsigned char *Crypto::base64Decode(std::string const& encoded_string) { | |
const std::string base64_chars = | |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
"abcdefghijklmnopqrstuvwxyz" | |
"0123456789+/"; | |
int in_len = encoded_string.size(); | |
int i = 0; | |
int j = 0; | |
int in_ = 0; | |
unsigned char char_array_4[4], char_array_3[3]; | |
std::string ret; | |
while (in_len-- && ( encoded_string[in_] != '=') && this->isBase64(encoded_string[in_])) { | |
char_array_4[i++] = encoded_string[in_]; in_++; | |
if (i ==4) { | |
for (i = 0; i <4; i++) | |
char_array_4[i] = base64_chars.find(char_array_4[i]); | |
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); | |
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); | |
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; | |
for (i = 0; (i < 3); i++) | |
ret += char_array_3[i]; | |
i = 0; | |
} | |
} | |
if (i) { | |
for (j = i; j <4; j++) | |
char_array_4[j] = 0; | |
for (j = 0; j <4; j++) | |
char_array_4[j] = base64_chars.find(char_array_4[j]); | |
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); | |
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); | |
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; | |
for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; | |
} | |
unsigned char *result = new unsigned char[ret.length()+1]; | |
strcpy((char*)result, ret.c_str()); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef CRYPTO_H | |
#define CRYPTO_H | |
#include <openssl/conf.h> | |
#include <openssl/evp.h> | |
#include <openssl/err.h> | |
#include <string.h> | |
#include <iostream> | |
#define AES_ROUNDS 5 | |
#define AES_KEY_LEN 32 | |
class Crypto | |
{ | |
public: | |
void handleErrors(void); | |
int encrypt(unsigned char *plaintext, int plaintext_len, unsigned char *key, unsigned char *iv, unsigned char *ciphertext); | |
int decrypt(unsigned char *ciphertext, int ciphertext_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext); | |
unsigned char *base64Decode(std::string const& encoded_string); | |
std::string base64Encode(unsigned char const* bytes_to_encode, unsigned int in_len); | |
bool isBase64(unsigned char c); | |
}; | |
#endif // CRYPTO_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
g++ -Wall -g -std=c++11 -lssl -lcrypto aes.cpp crypto.cpp -o aes && ./aes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment