C语言实现明文文组尾部填充
时间: 2023-08-20 09:28:44 浏览: 55
明文文本尾部填充通常使用的是PKCS#5或PKCS#7填充方式,这里给出使用PKCS#7填充方式的C语言代码示例:
```c
#include <stdio.h>
#include <string.h>
int PKCS7Padding(unsigned char *in, int inlen, unsigned char *out, int block_size) {
int pad_len = block_size - (inlen % block_size);
int i;
memcpy(out, in, inlen);
for (i = inlen; i < inlen + pad_len; i++) {
out[i] = pad_len;
}
return inlen + pad_len;
}
int main() {
unsigned char in[] = "hello world";
unsigned char out[16];
int inlen = strlen((char *)in);
int outlen = PKCS7Padding(in, inlen, out, 16);
for (int i = 0; i < outlen; i++) {
printf("%02x ", out[i]);
}
printf("\n");
return 0;
}
```
其中,`in`为输入明文文本,`inlen`为输入明文文本长度,`out`为输出填充后的文本,`block_size`为块大小(通常为加密算法的分组大小)。函数返回填充后的文本长度。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)