char *中的填充零 [英] padding zeros in char *

查看:152
本文介绍了char *中的填充零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上帝和程序员!


我在C中使用strlen()时遇到了一个奇怪的问题。


当前项目,我需要AES_decrypt()一些字符串。到现在为止,我是使用libssl(openssl)API获得



为了使这个AES_decrypt()工作,我需要拆分编码输入

成16字节的片段。这可以按预期工作。


唯一的问题是一行中的每一个最后一块,它可能更短

比16字节(正如你已经假设的那样,这个不同介于1和15之间。

根据手册,我需要用零填充该片,直到它为
达到16字节的长度。


嗯,这是我的问题:我不能用\0填充,因为C在第一次出现时切断了这个

''字符串'' 0.


我几乎尝试了所有东西:strcat,membcpy,直接写入

地址......没有...最后一段编码数据停留在我的审判之前,只要




同样,我的问题不是AES_decrypt()。我的问题是使用尾随的\0字节填充

输入。


void AES_decrypt(const unsigned char * in,unsigned char * out ,

^^^^ const AES_KEY

* key);

这需要是strlen()= 16 ------ ---''


如果有人知道答案,请告诉我。


提前致谢。


Stephan Seitz

< s。***** @ netz-haut.de>

hi gods and programmers!

i''ve got a weird problem with strlen() in C.

in a current project, i need to AES_decrypt() some string. By now, i''m
using the libssl (openssl) API.

To get this AES_decrypt() work, i needed to split the encoded input
into pieces of 16 byte. This works as expected.

Only problem is every LAST piece in a line, which is propably shorter
than 16 byte (as you already assumed, this differs between 1 and 15).
According to the manual, i''ll need to pad the piece with zeros until it
reaches the length of 16 byte.

Well, this is my problem: I can''t pad with \0 since C cuts this
''string'' at the first occurency of \0.

I tried almost everything: strcat, membcpy, writing directly to the
address ... nothing... the last piece of encoded data stays as short as
before my trials.

Again, my problem is NOT the AES_decrypt(). My problem is padding the
input with trailing \0 bytes.


void AES_decrypt(const unsigned char *in, unsigned char *out,
^^^^ const AES_KEY
*key);
this needs to be strlen()=16 ---------''

Please, if someone knows an answer, let me know.

Thanks in advance.

Stephan Seitz
<s.*****@netz-haut.de>

推荐答案

s.*****@netz-haut.de écrit:
我几乎尝试了所有东西:strcat,membcpy,直接写入
地址......没有...最后一段编码数据在我的试验之前仍然像
一样短。
I tried almost everything: strcat, membcpy, writing directly to the
address ... nothing... the last piece of encoded data stays as short as
before my trials.




它可能是strncpy()的工作。请仔细阅读本手册。



It might a job for strncpy(). Read the manual carefully.


[剪掉一些空白区域]
s。***** @ netz-haut.de 写道:
[snipped some white space]
s.*****@netz-haut.de wrote:
嗨神和程序员!

我已经在当前项目中使用strlen()得到了一个奇怪的问题。

在当前项目中,我需要AES_decrypt()一些字符串。到现在为止,我正在使用libssl(openssl)API。

为了使这个AES_decrypt()工作,我需要将编码输入分成16个部分。字节。这可以按预期工作。

唯一的问题是一行中的每个最后一个部分,它比16个字节更短(正如您已经假设的,这在1和15之间不同)。 />根据手册,我需要用零填充它,直到它达到16字节的长度。

嗯,这是我的问题:我可以'' t填充\0,因为C在第一次出现\0时切断了这个''字符串'。

我几乎尝试了所有东西:strcat,membcpy,直接写入
地址......没有...在我的试验之前,最后一段编码数据仍然像
一样短。

同样,我的问题不是AES_decrypt()。我的问题是使用尾随的\0字节填充
输入。

void AES_decrypt(const unsigned char * in,unsigned char * out,
^^^^ const AES_KEY
* key);
这需要是strlen()= 16 ---------''

如果有人知道答案,请告诉我。
hi gods and programmers!

i''ve got a weird problem with strlen() in C.

in a current project, i need to AES_decrypt() some string. By now, i''m
using the libssl (openssl) API.

To get this AES_decrypt() work, i needed to split the encoded input
into pieces of 16 byte. This works as expected.

Only problem is every LAST piece in a line, which is propably shorter
than 16 byte (as you already assumed, this differs between 1 and 15).
According to the manual, i''ll need to pad the piece with zeros until it
reaches the length of 16 byte.

Well, this is my problem: I can''t pad with \0 since C cuts this
''string'' at the first occurency of \0.

I tried almost everything: strcat, membcpy, writing directly to the
address ... nothing... the last piece of encoded data stays as short as
before my trials.

Again, my problem is NOT the AES_decrypt(). My problem is padding the
input with trailing \0 bytes.

void AES_decrypt(const unsigned char *in, unsigned char *out,
^^^^ const AES_KEY
*key);
this needs to be strlen()=16 ---------''
Please, if someone knows an answer, let me know.




strlen()不是测量数组长度的合适工具

因为它会停在第一个零/''\\ \\ 0''字节。无论如何

更多关注。

您的规范可能只需要一系列无符号字符。

所以没有什么可担心的案件。填上你的

last" string"根据需要使用''\0''你会没事的。

strlen()不会显示填充,你可以

只检查零值将它们打印为整数

值。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



strlen() is not the right tool to measure an array length
as it will stop at the first zero/''\0'' byte. Regardless how
many more follow.
Your spec probably only demanded an array of unsigned char.
So there is nothing to worry about in this case. Pad your
last "string" with ''\0'' as needed and you will be fine.
The padding will not be visible to strlen() and you can
only inspect the zero values by printing them out as integer
values.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


s。***** @ netz-haut.de 写道:
嗨神和程序员!

我已经在当前项目中使用strlen()得到了一个奇怪的问题。

在当前项目中,我需要AES_decrypt()一些字符串。到现在为止,我正在使用libssl(openssl)API。

为了使这个AES_decrypt()工作,我需要将编码输入分成16个部分。字节。这可以按预期工作。

唯一的问题是一行中的每个最后一个部分,它比16个字节更短(正如您已经假设的,这在1和15之间不同)。 />根据手册,我需要用零填充它,直到它达到16字节的长度。

嗯,这是我的问题:我可以'' t填充\0,因为C在第一次出现\0时切断了这个''字符串'。

我几乎尝试了所有东西:strcat,membcpy,直接写入
地址......没有......在我的试验之前,最后一段编码数据仍然像
一样短。
hi gods and programmers!

i''ve got a weird problem with strlen() in C.

in a current project, i need to AES_decrypt() some string. By now, i''m
using the libssl (openssl) API.

To get this AES_decrypt() work, i needed to split the encoded input
into pieces of 16 byte. This works as expected.

Only problem is every LAST piece in a line, which is propably shorter
than 16 byte (as you already assumed, this differs between 1 and 15).
According to the manual, i''ll need to pad the piece with zeros until it
reaches the length of 16 byte.

Well, this is my problem: I can''t pad with \0 since C cuts this
''string'' at the first occurency of \0.

I tried almost everything: strcat, membcpy, writing directly to the
address ... nothing... the last piece of encoded data stays as short as
before my trials.




如果AES_decrypt需要一个数组16个字符,可能是零填充在

右边,没有什么可以阻挡你的方式。字符串没有任何东西可以用它做b $ b。有很多方法可以做到这一点。这是一个步行者:

#include< stdio.h>

#include< ctype.h>


#define AESsize 16

void pseudo_AES_decrypt(char * s)

{

int i;

printf(" Received array:");

for(i = 0; i< AESsize; i ++){

if(isprint(s [i] ))

putchar(s [i]);

else

printf(" \\%03o",(unsigned) s [i]);

}

putchar(''\ n'');

}

int main(void)

{

char * tst =" abcedefghijklmnopqrstuvwxyz"

" ABCDEFGHHIJKLMNOPQRSTUVWXYZ";

char buf [AESsize],* t,* b;

int i;

for(t = tst; * t;){

for(i = 0; b = buf,i< AESsize; i ++)

b [i] =(* t)? * t ++:* t;

pseudo_AES_decrypt(buf);

}

返回0;

}


收到的数组:abcedefghijklmno

收到的数组:pqrstuvwxyzABCDE

收到的数组:FGHHIJKLMNOPQRST

收到的数组:UVWXYZ \ 000\000\000\000\000\000\000\000\000\\\



If AES_decrypt expects an array of 16 chars, possibly zero-padded on the
right, there is nothing to stand in your way. Strings have nothing to
do with it. There are many ways to do this. Here is a pedestrian one:
#include <stdio.h>
#include <ctype.h>

#define AESsize 16

void pseudo_AES_decrypt(char *s)
{
int i;
printf("Received array: ");
for (i = 0; i < AESsize; i++) {
if (isprint(s[i]))
putchar(s[i]);
else
printf("\\%03o", (unsigned) s[i]);
}
putchar(''\n'');
}
int main(void)
{
char *tst = "abcedefghijklmnopqrstuvwxyz"
"ABCDEFGHHIJKLMNOPQRSTUVWXYZ";
char buf[AESsize], *t, *b;
int i;
for (t = tst; *t;) {
for (i = 0; b = buf, i < AESsize; i++)
b[i] = (*t) ? *t++ : *t;
pseudo_AES_decrypt(buf);
}
return 0;
}

Received array: abcedefghijklmno
Received array: pqrstuvwxyzABCDE
Received array: FGHHIJKLMNOPQRST
Received array: UVWXYZ\000\000\000\000\000\000\000\000\000\000


这篇关于char *中的填充零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆