所以我尝试使用 cbc_crypt 来尝试加密和解密不同的字符串。到目前为止,这是我的程序。
int main(int argc, char *argv[])
{
unsigned char key[8] = {0x00, 0x00, 0x00, 0x68, 0x67, 0x01, 0x75, 0x19};
unsigned char iv[8] = {0xe0, 0x3d, 0x36, 0x0c, 0x55, 0xfc, 0xe4, 0x0f};
unsigned char ciphertext[96] = {0xb2, 0xc0, 0x11, 0xd6, 0x58, 0xce, 0x4b, 0x3\
c, 0xa6, 0x05, 0x93, 0x4d, 0x4f, 0x4c, 0x80, 0x19, 0x3f, 0xb5, 0xa8, 0xd6, 0x03\
, 0xec, 0xf9, 0xbc, 0xb0, 0xea, 0x36, 0xe9, 0x8f, 0x3d, 0x94, 0x95, 0x02, 0xa8,\
0xc0, 0x48, 0x23, 0xba, 0x51, 0xab, 0x65, 0x7f, 0xc2, 0xb8, 0xff, 0xfc, 0x3e, \
0x40, 0xdd, 0xfc, 0xd3, 0xe9, 0x32, 0x43, 0xd2, 0xf2, 0x37, 0x47, 0xab, 0x0d, 0\
x30, 0xba, 0xd0, 0x9e, 0x88, 0x66, 0x2b, 0x22, 0xdf, 0xed, 0x06, 0x76, 0xdb, 0x\
3a, 0xd5, 0x79, 0x7e, 0x22, 0x28, 0xcf, 0x3c, 0x5e, 0xbb, 0xc5, 0x85, 0xa0, 0x3\
f, 0x21, 0xab, 0xa8, 0xbe, 0x74, 0x37, 0xae, 0x59, 0xe9};
unsigned char short_cipher[8] = {0xb2, 0xc0, 0x11, 0xd6, 0x58, 0xce, 0x4b, 0x\
3c};
unsigned char text[8] = {0x12, 0x34, 0x56, 0xab, 0xba, 0xfe, 0xfe, 0xfe};
printf("Text before is %u \n", text);
// cbc_crypt(key, text, 8, 1, iv);
printf("Text after is %u \n", text);
return;
}
请注意,cbc_crypt 已被注释掉。这就是当我在 cbc_crypt 被注释掉时运行它时发生的情况。
[dorset:usabledir] 247) gcc guessdes.c
[dorset:usabledir] 248) ./a.out
Text before is 73623220
Text after is 73623220
[dorset:usabledir] 249) ./a.out
Text before is 9cf73030
Text after is 9cf73030
[dorset:usabledir] 249) ./a.out
Text before is f46e1bc0
Text after is f46e1bc0
[dorset:usabledir] 249) ./a.out
Text before is 674ed540
Text after is 674ed540
我无法弄清楚为什么每次打印时文本都会发生变化。我什至没有尝试运行加密,我只是打印出我初始化的 unsigned char。任何帮助,将不胜感激。
编辑:所以显然我应该使用 %s。如果我这样做,它现在会持续打印。
[dorset:usabledir] 256) ./a.out
Text before is 4V???????%<
Text after is 4V???????%<
[dorset:usabledir] 256) ./a.out
Text before is 4V???????%<
Text after is 4V???????%<
[dorset:usabledir] 256) ./a.out
Text before is 4V???????%<
Text after is 4V???????%<
有人可以解释为什么它之前打印地址吗?另外,有没有办法以十六进制打印它?(我真的不明白 4V 和所有问号的来源)。
编辑 2:好的,所以我现在像 Scott 建议的那样打印
for(i = 0; i < 8; i++){
printf("%u", text[i]);
}
然后它将文本打印为 185286171186254254254
我试图弄清楚它如何与我的原始十六进制代码 {0x12、0x34、0x56、0xab、0xba、0xfe、0xfe、0xfe} 匹配。
编辑:我意识到我应该使用 %x 而不是 %u。现在它的打印 123456abbafefefe 正确!多谢你们。