OpenSSL AES-CBC encryption is implemented via C + +

Source: Internet
Author: User

Encryption is required for recent work, and symmetric encryption is used for data within the program, so AES encryption is employed. Nonsense not much to say, directly on the code

Bob_aes.h

#ifndef bob_aes_h#define bob_aes_h#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <cstdlib> #include <openssl/aes.h>class bobaes{public:bobaes (); ~bobaes (); STD: : String Aes_encrypt (std::string msg), std::string aes_decrypt (std::string msg);p rivate:int Msg_len;char Key[AES_BLOCK _size];}; #endif

Bob_aes.cpp

#include   "Bob_aes.h" Bobaes::bobaes ():  msg_len (0) {for (int i = 0; i <  aes_block_size; i++) {key[i] = 32 + i;}} Bobaes::~bobaes () {}std::string bobaes::aes_encrypt (std::string msg) {int i = msg.size ( )  / 1024; msg_len =  ( i + 1 )  * 1024;//msg_len = msg.size ()  +  16;char in[msg_len];char out[msg_len+16];memset ((char*) in,0,msg_len); memset (char*) out,0,MSG_ LEN+16); strncpy ((char*) in,msg.c_str (), msg.size ()); unsigned char iv[aes_block_size]; // Encrypted initialization vector for (int j = 0; j < aes_block_size; ++j) {iv[j] = 0;} Aes_key aes;if (Aes_set_encrypt_key ((unsigned char*) key, 128, &aes)  < 0) { Return null;} Int len = msg.size (); Aes_cbc_encrypt ((unsigned char*) in, (unsigned char*) out,len,&aes,iv,aes_encrypt); std::string  Encrypt_msg (&out[0],&out[msg_len+16]); for (int i= 0;out[i];i++) {         printf ("%x", (Unsigned char) out[i]);         //std::cout << dstStringTemp[i];    }std::cout <<  Std::endl;return encrypt_msg;} Std::string bobaes::aes_decrypt (std::string msg) {msg_len = msg.size (); Char in[MSG_LEN ];char out[msg_len+16];memset ((char*) in,0,msg_len); memset ((char*) out,0,msg_len+16); strncpy ((char*) in, Msg.c_str (), msg.size ()); for (int i= 0;in[i];i++) {         printf ("%x", (Unsigned char) in[i]);        //std::cout < < dststringtemp[i];    }std::cout << std::endl;unsigned char  iv[aes_block_size]; //encrypted initialization vector for (int j = 0; j < aes_block_sizE;&NBSP;++J) {iv[j] = 0;} Aes_key aes;if (Aes_set_decrypt_key ((unsigned char*) key, 128, &aes)  < 0) { Return null;} Int len = msg.size (); Aes_cbc_encrypt ((unsigned char*) in, (unsigned char*) out,len,&aes,iv,aes_decrypt); std::string  decrypt_msg = out;return decrypt_msg;}

This code still has some problems, for the byte within 96 of the string can be encrypted decryption succeeds, but 96 and more bytes of string encryption decryption will appear garbled, for this aspect I still novice, ask the great gods to correct a lot.

OpenSSL AES-CBC encryption is implemented via C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.