3

Working on some encryption that requires unsigned char's in the functions, but want to convert to a char for use after it's been decrypted. So, I have:

unsigned char plaintext[16];
char *plainchar;
int plainint;

... Code now populates plaintext with data that happens to all be plain text

Now at this point, let's say plaintext is actually a data string of "0123456789". How can I get the value of plaintext into plainchar as "012456789", and at the same time plainint as 123456789?

-- Edit --

Doing this when plaintext is equal to "AAAAAAAAAA105450":

unsigned char plaintext[16];
char *plainchar;
int plainint;

... Code now populates plaintext with data that happens to all be plain text

plainchar = (char*)plaintext;

Makes plainchar equal to "AAAAAAAAAA105450╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠┤∙7" with a sizeof = 51. The encryption code is the rijndael example code, so it should be working fine.

Thanks, Ben

4

3 回答 3

5

您的纯文本字符串未终止。所有字符串都必须有一个额外的字符来表示字符串的结尾。这个字符是'\0'

plaintext将变量声明为

unsigned char plaintext[17];

在你完成解密后添加这个

plaintext[last_pos] = '\0';

更改last_pos为解密文本的最后一个位置,默认为16(数组的最后一个索引)。

于 2012-09-24T06:03:03.907 回答
1

对于 unsigned char 到 char*

plainchar = (char*)plaintext;

对于无符号到 int

sscanf( plainchar, "%d", &plainint );
于 2012-09-24T02:35:35.767 回答
1

我认为它很简单

plainchar = (char*)plaintext;
sscanf( plainchar, "%d", &plainint );
于 2012-09-24T02:14:04.390 回答