Crypto ++ Getting Started (1) -AES Block Encryption

xiaoxiao2021-03-06  59

The Crypto Homepage provides the link to the Crypto User Guide. This guide belongs to the information you have to see, but unfortunately, this is the only guide to find: (. User Guide has some examples, where The description of the AES usage is not very straightforward, and the AES's symmetric encryption algorithm is more common, so I wrote an example of AES here. The example is to encrypt a block directly, AES's data block size is 128 bits, key The length can be selected for 128 bits, 192 or 256. It is very useful to encrypt a block directly, because we usually have data encrypted with any length, need to select encryption mode such as CFB. But directly block encryption is the foundation of symmetrical encryption. .

#include using namespace std; #include using namespace cryptopp; int Main () {// AES is used in the ENUM data type defined in class AES, not Member function or variable / / So you need to use :: symbol to index cout << "AES parameters:" << end1; cout << "algorithm name: << AES :: staticgorithmname () << Endl; // Crypto library Generally, the number of bytes is used to represent the length, not the common byte number cout << "block size:" << AES :: blocksize * 8 << endl; cout << "min key length: << AES: : Min_keylength * 8 << endl; cout << "max key length: << AES :: max_keylength * 8 << Endl; // AES contains only some fixed data, and the function of encrypting decryption is from AESencryption and AesDecryption. Completion // encryption process AESENCRYPTION AESENCRYPTOR; // Enterprise Unsigned CHAR AESKEY [AES :: Default_Keylength]; // Key Unsigned Char Inblock [AES :: Blocksize] = "123456789"; // To encrypt data block unsigned char Outblock [AES :: Blocksize]; // Encrypted Ciphen Unsigned Char XORBLOCK [AES :: Blocksize]; // must be set It is scheduled to be a total MEMSET (xorblock, 0, ap :: blocksize); // Set zero aesencryptor.setkey (Aeskey, AES :: default_keylength); // Set the encryption key AESENCRYPTOR.PROCESSANDXORBLOCK (Inblock, Xorblock, Outblock); // Encrypted // Data for the encrypted data in 16 binary (int i = 0; i <16; i ) {cout << Hex << (int) Outblock [i] << "";} cout << Endl; // Decryption AESDECRYPTION AESDECRYPTOR; unsigned char PLAINTEXT [AES :: Blocksize];

AESDECRYPTOR.SETKEY (AESKEY, AES :: Default_KeyLength); Aesdecryptor.ProcessandXorblock (Outblock, XORBLOCK, PLAINTEXT); for (int i = 0; i <16; i ) {cout << plaintext [i];} cout << Endl Return 0;} There are several places to pay attention to this: AES is not a class, but a TypedEf of the class Rijndael. Although Rijndael is a class, its usage and namespace are very similar to those who have no member functions and member variables, just define a series of categories and data types (Enum), which can really encrypt the decrypted AESencryption and AesDecryption. They are all defined in this class. AESENCRYPTION and AESDECryption In addition to the setKey () function setting key, the key, parameters, and setKey () are set in the constructor. Processandxorblock () may make people more confused, the function name means ProcessBlock and xorblock, ProcessBlock is encrypted or decrypted with blocks, XORBLock is useful in various encryption mode, here we do not need application mode, so use to XOR operation XORBLOCK is set to 0, then xor operation does not work.

转载请注明原文地址:https://www.9cbs.com/read-112848.html

New Post(0)