AES Plus decryption for C + +

Source: Internet
Author: User

Recently the company project is going to be a WPF program, but the underlying encryption part is implemented in C + +. Search all kinds of information through the Internet, the address has not been remembered, did not post out! below to see how to add the decryption! Paste the code First ....

1  stringTKey (SKey);2  stringTIV (SIV);3  stringoutstr;4  stringPT =StringToUTF8 (plaintext);5Secbyteblock Key (Const byte*) Tkey.data (), tkey.size ());6Secbyteblock IV (TIV = =""? (Const byte*)0x00: (Const byte*) Tiv.data (), TIV = =""? -: Tiv.size ());7Aes::encryption Aesencryption ((byte*) Key, TIV = =""?Aes::max_keylength:aes::D efault_keylength);8 cbc_mode_externalcipher::encryption cbcencryption (Aesencryption, iv);9Stringsource (PT,true,Ten   NewStreamtransformationfilter (cbcencryption, OneIshex? Dynamic_cast<bufferedtransformation*> (NewHexencoder (NewStringsink (OUTSTR))) : ADynamic_cast<bufferedtransformation*> (NewBase64encoder (NewStringsink (OUTSTR))), - streamtransformationfilter::P kcs_padding)); -strcpy_s (Outtext, outstr.size () +1, Outstr.c_str ());

Encrypted AES uses the CBC PKCS7,128-256-bit encryption method. This is involved in the time when there is no need for offsets, so use Secbyteblock IV (TIV = = ""? (const byte*) 0x00: (const byte*) tiv.data (), TIV = = ""? 16:tiv.size ()); To give a default value, because the minimum required is 16 bytes, so give the value 16. Hexencoder is whether the encrypted value is a hexadecimal string, and Base64encoder is a string that encrypts whether it is Base64. strcpy_s is to copy the string into the returned char*, because to give the C # callback, so here to use, C # Yonder must use StringBuilder to receive, with string is not worth. There is also a StringToUTF8 because the default encoding for string is gb2312, so if the string to be encrypted is utf-8 encoding it needs to be converted.

1  stringTKey (SKey);2  stringTIV (SIV);3  stringoutstr;4Secbyteblock Key (Const byte*) Tkey.data (), tkey.size ());5Secbyteblock IV (TIV = =""? (Const byte*)0x00: (Const byte*) Tiv.data (), TIV = =""? -: Tiv.size ());6AES::D ecryption aesdecryption (byte*) Key, TIV = =""?Aes::max_keylength:aes::D efault_keylength);7 cbc_mode_externalcipher::D ecryption cbcdecryption (Aesdecryption, iv);8Bufferedtransformation *decryptor = Ishex?9Dynamic_cast<bufferedtransformation*> (NewHexdecoder (NewStreamtransformationfilter (Cbcdecryption,NewStringsink (OUTSTR)))) :TenDynamic_cast<bufferedtransformation*> (NewBase64decoder (NewStreamtransformationfilter (Cbcdecryption,NewStringsink (OUTSTR) )); OneDecryptor->put ((byte*) ciphertext, strlen (ciphertext)); ADecryptor->messageend (); -OUTSTR =utf8tostring (outstr); -strcpy_s (Outtext, outstr.size () +1, Outstr.c_str ());

Similarly, decryption is roughly the same as encryption. Utf8tostring utf-8 to gb2312 encoded format.

The above is the C + + CBC encryption and decryption method. A third-party open Source Library, crypto++, is used here.

Document Address

Open Source Address

AES Plus decryption for 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.