DES CBC Encryption Instance

Source: Internet
Author: User

Here is an example of a des CBC-based encryption form that I've tuned based on the STM32+POLARSSL encryption library. So that you can use it to do the right thing.

1. Use the DES-CBC encryption method and populate it with the Pkcs5padding method

2. Clear text for (string): <xml>123232</xml>

3. Generated ciphertext (hex data): 305f62576a76b23d6822b53fc537014058360a0d1696610b

4. Key is (string): rw4fef48

5. Initial Vector IV is: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

Pkcs5padding's explanation is as follows:

In pkcs5padding, it is clear that the size of the block is 8 bits, whereas in the pkcs7padding definition, the size of the block is indeterminate and can be between 1-255 (the length of the block is beyond 255), and the algorithm for populating the values is the same:
Value=k-(l mod k), k= block size, l= data length, if l=8, you need to populate additional 8 byte 8
The algorithm of the other side is:
Data to be padded
Pading = 8-(_raw_size% 8);
*_dst_size = _raw_size + pading;
for (i = _raw_size; i < *_dst_size; i++)
{
_raw_ptr[i] = pading;

}

The basic test procedure based on Polarssl is as follows:

unsigned char deskey1[9] = {"Rw4fef48"};
unsigned char pt[] = {' < ', ' x ', ' m ', ' l ', ' > ', ' 1 ', ' 2 ', ' 3 ', ' 2 ', ' 3 ', ' 2 ', ' < ', '/', ' X ', ' m ', ' l ', ' > '};
unsigned char cryt[64] = {0};
unsigned char iv[8] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
Des_context CTX;
    
for (unsigned char i = 0; i < sizeof (Pt); i++)
{
    Cryt[i] = Pt[i];
}
unsigned char Index = sizeof (Pt);
unsigned char pading;
Pading = 8-(Index% 8);
for (unsigned char i = 0; i < pading; i++) 
{
    Cryt[index+i] = pading;
}
Des_setkey_enc (&ctx, (unsigned char *) DESKey1);
DES_CRYPT_CBC (&ctx, Des_encrypt, Index+pading, IV, Cryt, CRYT);
    
printf ("\ r \ n");
For (unsigned short i = 0; i < index+pading; i++)
{
    printf ("%02x", Cryt[i]);
}
printf ("\ r \ n");
    
memset (iv, 0, sizeof (iv));
Des_setkey_dec (&ctx, (unsigned char *) DESKey1);
DES_CRYPT_CBC (&ctx, Des_decrypt, Index+pading, IV, Cryt, CRYT);

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.