簡體   English   中英

用PKCS7填充[C]

[英]padding with PKCS7 [C]

我想用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");

我得到:

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

但我想要 :

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

我該如何修改我的代碼?

提前致謝。

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

您使用十六進制( %x )打印,而使用

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

(十進制)16 =>(六)10 ,所以您正在顯示正確的東西。

您可能會在打印中顯示16而不是10,但是我認為這不是您要搜索的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM