Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. /*
  2.  * AESDemoClass.cpp
  3.  *
  4.  *  Created on: Sep 13, 2019
  5.  *      Author: yulia
  6.  */
  7.  
  8. #include <iostream>
  9. #include <fstream>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <vector>
  14. #include <openssl/aes.h>
  15. #include "AESDemoClass.h"
  16.  
  17. using namespace std;
  18.  
  19. AESDemoClass::AESDemoClass() {
  20.  
  21. }
  22.  
  23. AESDemoClass::~AESDemoClass() {
  24. }
  25.  
  26. /* AES key for Encryption and Decryption */
  27. const static unsigned char aes_key[]={0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF};
  28.  
  29. void AESDemoClass::aesDemo(){
  30.     /* Input data to encrypt */
  31.     vector<char> input = readAllBytes("input.txt");
  32.  
  33.     int n = input.size();
  34.  
  35.     unsigned char aes_input[n];
  36.     for (int i = 0; i < n; i++)
  37.         aes_input[i] = input[i];
  38.  
  39. //  unsigned char aes_input[n] = arr;
  40.  
  41.     /* Init vector */
  42.     unsigned char iv[AES_BLOCK_SIZE];
  43.     memset(iv, 0x00, AES_BLOCK_SIZE);
  44.  
  45.     /* Buffers for Encryption and Decryption */
  46.     unsigned char enc_out[sizeof(aes_input)];
  47.     unsigned char dec_out[sizeof(aes_input)];
  48.  
  49.     /* AES-128 bit CBC Encryption */
  50.     AES_KEY enc_key, dec_key;
  51.     AES_set_encrypt_key(aes_key, sizeof(aes_key)*8, &enc_key);
  52.     AES_cbc_encrypt(aes_input, enc_out, sizeof(aes_input), &enc_key, iv, AES_ENCRYPT);
  53.     /* AES-128 bit CBC Decryption */
  54.     memset(iv, 0x00, AES_BLOCK_SIZE); // don't forget to set iv vector again, else you can't decrypt data properly
  55.     AES_set_decrypt_key(aes_key, sizeof(aes_key)*8, &dec_key); // Size of key is in bits
  56.     AES_cbc_encrypt(enc_out, dec_out, sizeof(aes_input), &dec_key, iv, AES_DECRYPT);
  57.  
  58.     /* Printing and Verifying */
  59.     print_data("\n Original ",aes_input, sizeof(aes_input)); // you can not print data as a string, because after Encryption its not ASCII
  60.  
  61.     print_data("\n Encrypted",enc_out, sizeof(enc_out));
  62.  
  63.     print_data("\n Decrypted",dec_out, sizeof(dec_out));
  64.  
  65. }
  66.  
  67. void AESDemoClass::print_data(const char *title, const void* data, int len)
  68. {
  69.     printf("%s : ",title);
  70.     const unsigned char * p = (const unsigned char*)data;
  71.     int i = 0;
  72.  
  73.     for (; i<len; ++i)
  74.         printf("%02X ", *p++);
  75.  
  76.     printf("\n");
  77. }
  78.  
  79. std::vector<char> AESDemoClass::readAllBytes(char const* filename){
  80.     ifstream ifs(filename, ios::binary|ios::ate);
  81.     ifstream::pos_type pos = ifs.tellg();
  82.  
  83.     std::vector<char> input(pos);
  84.  
  85.     ifs.seekg(0, ios::beg);
  86.     ifs.read(&input[0], pos);
  87.  
  88.     return input;
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement