Encryption using CBC mode of the crypto++ Library (ii)

Source: Internet
Author: User

There is already an article about the encryption implemented using the crypto++ library, but the code is not completely considered, so it re-sent a second

C + + Encapsulation:

#include"Zyaes.h"#include<string.h>#include<stdio.h>using namespaceCryptopp; Czyaes::czyaes () {byteBytekey[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,                 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; byteByteiv[] = {0x01,0x02,0x03,0x04,0x01,0x02,0x03,0x04,0x01,0x02,0x03,0x04,0x01,0x02,0x03,0x04}; memcpy (M_arrbytekey, Bytekey,sizeof(byte) * +); memcpy (M_arrbyteiv, Byteiv,sizeof(byte) * -); M_nkeylen= +;} Czyaes::~Czyaes () {}//set key and IVvoidCzyaes::setkey (std::stringStrkey, std::stringStriv) {    intNkeylen =0; intNivlen =0; memset (M_arrbytekey,0,sizeof(byte) * +); memset (M_arrbyteiv,0,sizeof(byte) * -); if(Strkey.length () >= +) {Nkeylen= +; }    Else{Nkeylen=strkey.length (); } memcpy (M_arrbytekey, Strkey.c_str (),sizeof(byte) *Nkeylen); if(!Striv.empty ()) {        if(Striv.length () >= -) {Nivlen= -; }            Else{Nivlen=striv.length (); } memcpy (M_arrbyteiv, Striv.c_str (),sizeof(byte) *Nivlen); }}//EncryptSTD::stringCzyaes::encrypt (ConstSTD::string&StrText) {std::stringStrcipher; Cbc_mode<AES>:: Encryption Aesencryptor (M_arrbytekey, M_nkeylen, M_ARRBYTEIV); Stringsource (StrText,true,NewStreamtransformationfilter (Aesencryptor,NewStringsink (Strcipher)); STD::stringstrencoded; Stringsource S2 (Strcipher,true,        NewHexencoder (NewStringsink (strencoded))//Hexencoder);//Stringsource    returnstrencoded;}//DecryptSTD::stringCzyaes::D Ecrypt (ConstSTD::string&strcipher) {std::stringstrdecoded; Stringsource S2 (Strcipher,true,        NewHexdecoder (NewStringsink (strdecoded))//Hexencoder);//Stringsourcestd::stringStrText; Cbc_mode<AES>::D ecryption aesencryptor (M_arrbytekey, M_nkeylen, M_ARRBYTEIV); Stringsource (strdecoded,true,NewStreamtransformationfilter (Aesencryptor,NewStringsink (StrText)); returnStrText;}
implementation file

Header file

C Package:

//*****************************************************************************//@FileName: Aes256.cpp//@Version: v1.0.0//@Author: Xiaoc//@Date: 2015/03/13//*****************************************************************************#include"Zyaes.h"#include"zyaes256.h"using namespacestd;stringEncrypt (Const string&strtext,Const string&strkey/* = "" */) {std::stringStrcipher; intNlen =0;        Czyaes AES; if(!Strkey.empty ()) {AES. Setkey (strkey,""); } strcipher=AES.    Encrypt (StrText); returnStrcipher;}stringDecrypt (Const string&strcipher,Const string&strkey/* = "" */) {std::stringStrText; intNlen =0;        Czyaes AES; if(!Strkey.empty ()) {AES. Setkey (strkey,""); } StrText=AES.    Decrypt (Strcipher); returnStrText;}
implementation file
//*****************************************************************************//@FileName: Zyaes256.h:the C Interface of class Zyaes//@Version: v1.0.0//@Author: Xiaoc//@Date: 2015/03/13//*****************************************************************************#ifndef _zyaes256_h_#define_zyaes256_h_#include<string>//*****************************************************************************//@strText: Data that needs to be encrypted//@strKey: Encryption key//@return: Returns the data after encryption//*****************************************************************************STD::stringEncrypt (ConstSTD::string&strtext,ConstSTD::string&strkey ="");//*****************************************************************************//@strText: Data that needs to be decrypted//@strKey: Decryption key//@return: Returns the data after decryption//*****************************************************************************STD::stringDecrypt (ConstSTD::string&strcipher,ConstSTD::string&strkey ="");#endif //_zyaes256_h_
header File

This modification was largely due to the fact that the transmission of encrypted data in the network was not considered in the previous release because the encrypted data was no longer a simple ASCII character, so it could not exist in the char array, which caused the loss of data because 0 of the encrypted data caused the string to be truncated.

So now it's time to convert the encrypted data to 16 binary. Then the transmission, the decryption side to do the opposite processing can.

One of the questions is that the Cryptopp encryption library will have encrypted data in a string, but why can a string store non-ASCII characters?

Encryption using CBC mode of the crypto++ Library (ii)

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.