简体   繁体   中英

padding with PKCS7 [C]

I want to do padding with PKCS7 :

char *test1 = "azertyuiopqsdfgh";
char *test2 = malloc(32*sizeof(char));

memcpy(test2, test1, strlen(test1));

char pad = (char)(32-strlen(test1));
printf("pad = %d\n", pad);

for(int i = strlen(test1) ; i < 32 ; i++) {
    test2[i] = pad;
}
for (int i = 0 ; i < 32 ; i++)
    printf("%x ", test2[i]);
printf("\n");

I obtain :

pad = 16

61 7a 65 72 74 79 75 69 6f 70 71 73 64 66 67 68 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10

But i want :

pad = 16

61 7a 65 72 74 79 75 69 6f 70 71 73 64 66 67 68 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

How can i modify my code ?

Thanks in advance.

With

printf("%x ", test2[i]);

You are printing in hexadecimal ( %x ) whereas with

printf("pad = %d\n", pad);` you are printing in decimal (%d). 

And (decimal) 16 => (hexa) 10 , So you are displaying the right thing.

You could probably play a bit with your printing to display 16 instead of 10 but I don't think this is what you are searching for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM