crypto++ compile using __crypto++

Source: Internet
Author: User
Brief

crypto++ is a free encrypted library written in C + +, including: password, message authentication code, one-way hash function, public key cryptosystem, key agreement scheme and deflate compression.

Brief download Use more references

Download

Enter crypto++®library, download the corresponding version (i downloaded the Original cryptopp565.zip)

After decompression, we will see that it contains a large number of header files, source files, and engineering files. With VS2015 open Cryptest.sln, you will see 4 items: Cryptest, Cryptlib, Cryptopp, Dlltest.

Execute: Clean up solution-> rebuild solution. When the compilation is complete, Cryptest.exe and Cryptlib.lib are generated. Use

Create a new WIN32 console application testcrypto++. Right-click Properties, select: Configure the properties-> C + +-> general, additional include directory E:\cryptopp565. Selection: Linker-> general, additional library directory E:\cryptopp565\Win32\Output\Debug. Select: C/-> code generation, the runtime selects multithreaded Debugging (/MTD) (corresponding to "multithreading (/MT)" in release mode).

At this point, use the crypto++ development environment to build the example in crypto++ User Guide.

Testcrypto++.cpp: Defines the entry point for a console application. #include "stdafx.h" #include <aes.h> #include <Hex.h>//Streamtransformationfilter #include    
;modes.h>//Cfb_mode #include <iostream>//Std:cerr #include <sstream>//Std::stringstream
#include <string> using namespace std;
using namespace Cryptopp; #pragma comment (lib, "Cryptlib.lib") std::string ecb_aesencryptstr (std::string sKey, const char *plaintext) {std:

    : String outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?


    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);

    Aes::encryption aesencryption ((BYTE *) key, aes::max_keylength);
    Ecb_mode_externalcipher::encryption ecbencryption (aesencryption);
    Streamtransformationfilter Ecbencryptor (ecbencryption, New Hexencoder (New Stringsink (OUTSTR))); Ecbencryptor.put ((BYTE *) plaintext, strlen (plaintext));

    Ecbencryptor.messageend ();
return outstr;

    } std::string ecb_aesdecryptstr (std::string sKey, const char *ciphertext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);

    Ecb_mode<aes::D ecryption ecbdecryption ((BYTE *) key, aes::max_keylength);
    Hexdecoder decryptor (New Streamtransformationfilter (Ecbdecryption, New Stringsink (OUTSTR))); Decryptor.
    Put ((BYTE *) ciphertext, strlen (ciphertext)); Decryptor.

    Messageend ();
return outstr;

    } std::string cbc_aesencryptstr (std::string sKey, std::string sIV, const char *plaintext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength? memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, SKey.C_str (), aes::max_keylength);
    Filling IV byte iv[aes::blocksize];
    Memset (iv, 0x30, aes::blocksize); Siv.size () <= aes::blocksize?

    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize);

    Aes::encryption aesencryption ((BYTE *) key, aes::max_keylength);

    Cbc_mode_externalcipher::encryption cbcencryption (Aesencryption, iv);
    Streamtransformationfilter Cbcencryptor (cbcencryption, New Hexencoder (New Stringsink (OUTSTR)));
    Cbcencryptor.put ((BYTE *) plaintext, strlen (plaintext));

    Cbcencryptor.messageend ();
return outstr;

    } std::string cbc_aesdecryptstr (std::string sKey, std::string sIV, const char *ciphertext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);
    Filling IV byte iv[aes::blocksize]; Memset (iv, 0X30, Aes::bloCksize); Siv.size () <= aes::blocksize?


    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize);

    Cbc_mode<aes::D ecryption cbcdecryption ((BYTE *) key, Aes::max_keylength, IV);
    Hexdecoder decryptor (New Streamtransformationfilter (Cbcdecryption, New Stringsink (OUTSTR))); Decryptor.
    Put ((BYTE *) ciphertext, strlen (ciphertext)); Decryptor.

    Messageend ();
return outstr;

    } std::string cbc_cts_aesencryptstr (std::string sKey, std::string sIV, const char *plaintext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);
    Filling IV byte iv[aes::blocksize];
    Memset (iv, 0x30, aes::blocksize); Siv.size () <= aes::blocksize?

    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize); Aes::encryption aesencryption((BYTE *) key, aes::max_keylength);

    Cbc_cts_mode_externalcipher::encryption cbcctsencryption (Aesencryption, iv);
    Streamtransformationfilter Cbcctsencryptor (cbcctsencryption, New Hexencoder (New Stringsink (OUTSTR)));
    Cbcctsencryptor.put ((BYTE *) plaintext, strlen (plaintext));

    Cbcctsencryptor.messageend ();
return outstr;

    } std::string cbc_cts_aesdecryptstr (std::string sKey, std::string sIV, const char *ciphertext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);
    Filling IV byte iv[aes::blocksize];
    Memset (iv, 0x30, aes::blocksize); Siv.size () <= aes::blocksize?


    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize);

    Cbc_cts_mode<aes::D ecryption cbcctsdecryption ((BYTE *) key, Aes::max_keylength, IV); HexdecoderDecryptor (New Streamtransformationfilter (Cbcctsdecryption, New Stringsink (OUTSTR))); Decryptor.
    Put ((BYTE *) ciphertext, strlen (ciphertext)); Decryptor.

    Messageend ();
return outstr;

    } std::string cfb_aesencryptstr (std::string sKey, std::string sIV, const char *plaintext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);
    Filling IV byte iv[aes::blocksize];
    Memset (iv, 0x30, aes::blocksize); Siv.size () <= aes::blocksize?

    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize);

    Aes::encryption aesencryption ((BYTE *) key, aes::max_keylength);

    Cfb_mode_externalcipher::encryption cfbencryption (Aesencryption, iv);
    Streamtransformationfilter Cfbencryptor (cfbencryption, New Hexencoder (New Stringsink (OUTSTR))); Cfbencryptor.put ((bYte *) plaintext, strlen (plaintext));

    Cfbencryptor.messageend ();
return outstr;

    } std::string cfb_aesdecryptstr (std::string sKey, std::string sIV, const char *ciphertext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);
    Filling IV byte iv[aes::blocksize];
    Memset (iv, 0x30, aes::blocksize); Siv.size () <= aes::blocksize?


    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize);

    Cfb_mode<aes::D ecryption cfbdecryption ((BYTE *) key, Aes::max_keylength, IV);
    Hexdecoder decryptor (New Streamtransformationfilter (Cfbdecryption, New Stringsink (OUTSTR))); Decryptor.
    Put ((BYTE *) ciphertext, strlen (ciphertext)); Decryptor.

    Messageend ();
return outstr; } std::string ofb_aesencryptstr (std::string sKey, std::string SIV, const CHar *plaintext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);
    Filling IV byte iv[aes::blocksize];
    Memset (iv, 0x30, aes::blocksize); Siv.size () <= aes::blocksize?

    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize);

    Aes::encryption aesencryption ((BYTE *) key, aes::max_keylength);

    Ofb_mode_externalcipher::encryption ofbencryption (Aesencryption, iv);
    Streamtransformationfilter Ofbencryptor (ofbencryption, New Hexencoder (New Stringsink (OUTSTR)));
    Ofbencryptor.put ((BYTE *) plaintext, strlen (plaintext));

    Ofbencryptor.messageend ();
return outstr;

    } std::string ofb_aesdecryptstr (std::string sKey, std::string sIV, const char *ciphertext) {std::string outstr;
   Fill key Secbyteblock key (Aes::max_keylength); memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);
    Filling IV byte iv[aes::blocksize];
    Memset (iv, 0x30, aes::blocksize); Siv.size () <= aes::blocksize?


    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize);

    Ofb_mode<aes::D ecryption ofbdecryption ((BYTE *) key, Aes::max_keylength, IV);
    Hexdecoder decryptor (New Streamtransformationfilter (Ofbdecryption, New Stringsink (OUTSTR))); Decryptor.
    Put ((BYTE *) ciphertext, strlen (ciphertext)); Decryptor.

    Messageend ();
return outstr;

    } std::string ctr_aesencryptstr (std::string sKey, std::string sIV, const char *plaintext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);    Filling IV byte iv[aes::blocksize];
    Memset (iv, 0x30, aes::blocksize); Siv.size () <= aes::blocksize?

    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize);

    Aes::encryption aesencryption ((BYTE *) key, aes::max_keylength);

    Ctr_mode_externalcipher::encryption ctrencryption (Aesencryption, iv);
    Streamtransformationfilter Ctrencryptor (ctrencryption, New Hexencoder (New Stringsink (OUTSTR)));
    Ctrencryptor.put ((BYTE *) plaintext, strlen (plaintext));

    Ctrencryptor.messageend ();
return outstr;

    } std::string ctr_aesdecryptstr (std::string sKey, std::string sIV, const char *ciphertext) {std::string outstr;
    Fill key Secbyteblock key (Aes::max_keylength);
    memset (Key, 0x30, Key.size ()); Skey.size () <= aes::max_keylength?

    memcpy (Key, Skey.c_str (), Skey.size ()): memcpy (Key, Skey.c_str (), aes::max_keylength);
    Filling IV byte iv[aes::blocksize];
    Memset (iv, 0x30, aes::blocksize); Siv.size () <= AEs::blocksize?


    memcpy (iv, SIV.C_STR (), Siv.size ()): memcpy (iv, SIV.C_STR (), aes::blocksize);

    Ctr_mode<aes::D ecryption ctrdecryption ((BYTE *) key, Aes::max_keylength, IV);
    Hexdecoder decryptor (New Streamtransformationfilter (Ctrdecryption, New Stringsink (OUTSTR))); Decryptor.
    Put ((BYTE *) ciphertext, strlen (ciphertext)); Decryptor.

    Messageend ();
return outstr; int main () {string plaintext = "This program shows" "The Use ECB, CBC, Cbc_cts, CFB, OFB and CTR mode of AES in C
    rypto++. "; String aeskey = "0123456789abcdef0123456789abcdef";//256bits, also can be 128 bits or 192bits string aesiv = "ABCDEF
        0123456789 ";//128 bits string ecb_encryptedtext, Ecb_decryptedtext, Cbc_encryptedtext, Cbc_decryptedtext, Cbc_cts_encryptedtext, Cbc_cts_decryptedtext, Cfb_encryptedtext, Cfb_decryptedtext, OFB_EncryptedTex

    T, Ofb_decryptedtext, Ctr_encryptedtext, Ctr_decryptedtext; ECB Ecb_encryptedtext = ECb_aesencryptstr (Aeskey, Plaintext.c_str ());//ECB Encryption ecb_decryptedtext = ECB_AESDECRYPTSTR (AesKey, ECB_EncryptedText . C_STR ())//ECB decryption//CBC Cbc_encrypte Dtext = Cbc_aesencryptstr (Aeskey, Aesiv, Plaintext.c_str ());//CBC Encrypt cbc_decryptedtext = Cbc_aesdecryptstr (AesKey, AE  

                                                                                    SIV, Cbc_encryptedtext.c_str ());//CBC decryption Cbc_cts Cbc_cts_encryptedtext = Cbc_cts_aesencryptstr (Aeskey, Aesiv, Plaintext.c_str ());//cbc_cts cipher CBC  

                                                                                                _cts_decryptedtext = Cbc_cts_aesdecryptstr (Aeskey, Aesiv, Cbc_cts_encryptedtext.c_str ());//cbc_cts decryption CFB Cfb_encryptedtext = cfb_aesencry Ptstr (Aeskey, Aesiv, Plaintext.c_str ())//CFB Encryption Cfb_decryptedtext = CFB_AESDECRYPTSTR (Aeskey, AesIV, CFB_EncryptedTe  

  Xt.c_str ());//CFB decryption                                                                                  OFB Ofb_encryptedtext = Ofb_aese Ncryptstr (Aeskey, Aesiv, Plaintext.c_str ())//OFB Encryption Ofb_decryptedtext = OFB_AESDECRYPTSTR (Aeskey, AesIV, OFB_Encrypt  
    Edtext.c_str ());//OFB decryption//ctr Ctr_encryptedtext = Ctr_aesencryptstr (Aeskey, Aesiv, Plaintext.c_str ());//ctr Encryption Ctr_decryptedtext = CTR_AESDecryptS
    TR (Aeskey, Aesiv, Ctr_encryptedtext.c_str ()),//ctr decryption cout << "crypto++ AES-256 encryption Test" << Endl;
    cout << "Use ECB,CBC, CBC_CTR,CFB,OFB and CTR mode respectively" << Endl;
    cout << "Encryption key:" << Aeskey << Endl;
    cout << "Key length:" << aes::max_keylength * 8 << "bits" << Endl;
    cout << "IV:" << aesiv << Endl;

    cout << Endl;
    cout << "ECB test" << Endl;
    cout << "Original:" << plaintext << Endl;cout << "redaction:" << ecb_encryptedtext << Endl;

    cout << "restore Clear text:" << ecb_decryptedtext << endl << Endl;
    cout << "CBC Test" << Endl;
    cout << "Original:" << plaintext << Endl;
    cout << "redaction:" << cbc_encryptedtext << Endl;

    cout << "restore Clear text:" << cbc_decryptedtext << endl << Endl;
    cout << "cbc_cts test" << Endl;
    cout << "Original:" << plaintext << Endl;
    cout << "redaction:" << cbc_cts_encryptedtext << Endl;

    cout << "restore Clear text:" << cbc_cts_decryptedtext << endl << Endl;
    cout << "CFB Test" << Endl;
    cout << "Original:" << plaintext << Endl;
    cout << "redaction:" << cfb_encryptedtext << Endl;

    cout << "restore Clear text:" << cfb_decryptedtext << endl << Endl;
    cout << "OFB test" << Endl;
cout << "Original:" << plaintext << Endl;    cout << "redaction:" << ofb_encryptedtext << Endl;

    cout << "restore Clear text:" << ofb_decryptedtext << endl << Endl;
    cout << "Ctr Test" << Endl;
    cout << "Original:" << plaintext << Endl;
    cout << "redaction:" << ctr_encryptedtext << Endl;

    cout << "restore Clear text:" << ctr_decryptedtext << endl << Endl;
    GetChar ();
return 0; }
More ReferencesCryptopp using the introduction compiling and integrating crypto++ into the Microsoft Visual C + + environment How to build C + + cryptographic L Ibrary, crypto++

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.