Openssl AES Plus decryption routines further

Source: Internet
Author: User
Tags openssl aes

Original link: http://blog.csdn.net/itmes/article/details/7718427

We used the aes256 symmetric encryption algorithm of OpenSSL to test the encryption and decryption of 16 bytes of memory, and now further, add and decrypt memory blocks of a given size.

First make clear that AES is a packet encryption algorithm, and each encrypted memory block is 16 bytes, so we need to encrypt the memory block must be 16 bytes of integer times, if not, it needs to be filled.

The common symmetric encryption and decryption algorithm RC2,RC4 are stream encryption, that is, in bytes to add decryption, and aes,des,3des,idea,blowfish,towfish, these are packet encryption, all require the encrypted data block byte alignment.

For a memory block that is larger than 16 bytes, the cryptographic operation is nothing more than a circular call to a 16-byte cryptographic operation.

Refer to the following routines:

#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <openssl/aes.h>
#pragma comment (lib, "Libeay32.lib")
int main (int argc, char **argv)
{
unsigned char buf[512];
unsigned char buf2[512];
unsigned char buf3[512];
unsigned char aes_keybuf[32];
memset (buf,1,sizeof (BUF));
memset (buf,0,sizeof (BUF2));
memset (buf,0,sizeof (BUF3));
memset (aes_keybuf,0,sizeof (AES_KEYBUF));


Aes_key Aeskey;
Aes_set_encrypt_key (Aes_keybuf,256,&aeskey);
for (int i=0;i<sizeof (BUF); i+=16)
Aes_encrypt (Buf+i,buf2+i,&aeskey);


Aes_set_decrypt_key (Aes_keybuf,256,&aeskey);
for (int i=0;i<sizeof (BUF); i+=16)
Aes_decrypt (Buf2+i,buf3+i,&aeskey);


if (memcmp (buf,buf3,sizeof (BUF)) ==0)
printf ("Test success\r\n");
Else
printf ("Test fail\r\n");
}

The example here is actually using the AES encryption algorithm of the ECB mode, in addition to the CBC,CFB,OFB three modes, in short, the following three modes are actually let the last 16 bytes of data block encryption results in the next 16-byte block encryption operation, So in the ECB mode to encrypt the same content of the data block encryption results are the same, the use of CBC,CFB,OFB three mode three mode when the result of encryption is not only related to the content of the source data block, but also related to the order of encryption.

Finally, the use of packet encryption algorithm, because the smallest unit of encrypted data is the size of the packet, such as AES 16 bytes, des 8 bytes, in the encrypted file needs to be byte-padded, so the results of the encrypted file may be as large or slightly larger than the original file. In the decryption operation, it is necessary to remove the completed bytes after decryption, in order to actually restore the original file.

Openssl AES Plus decryption routines further

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.