我需要用Serpent和Twofish加密一个无符号字符数组(16字节长)。我在c-中使用了两个来自官方网站的库-write。我尝试使用它们,但我将获得的密文不正确。为了检查,我使用了这个site。我不明白哪里出了错。
我的代码是:
keyInstance keyTwofish;
cipherInstance cipherTwofish;
unsigned char *buffer_out_twofish;
char path_file_twofish[DIM];
...
//Twofish
if(cipherInit(&cipherTwofish,MODE_ECB,NULL)!=TRUE)
{
perror("[ERROR] Function module02: makeKey.\n");
return 0;
}
//key_dim*8=128 and key is an unsigned char key [16] that contain key
makeKey(&keyTwofish, DIR_ENCRYPT, key_dim*8, key);
…
//Buffer in is unsigned char buffer_in[16] contain plaintext
//byte_for_file=16 than byte_for_file*8=128
//Buffer out is unsigned char buffer_out_twofish[16]
blockEncrypt(&cipherTwofish, &keyTwofish, buffer_in, byte_for_file*8, buffer_out_twofish);For Serpent是相同的实现。
当我打印变量类型的sizeof时,结果是:
Size of Char: 1
Size of Integer: 4
Size of Long: 8当我这样做的时候:
printf("\nPrint Char Array: ");
for (int j = 0; j < byte_for_file; ++j)
printf("%x ", buffer_in[j]);
printf("\nPrint long Array: ");
test(buffer_in, byte_for_file);哪里
void test (unsigned long * a, int b)
{
for (int i = 0; i < b/sizeof(long); ++i)
printf("%lx ", a[i]);
}我明白了:
Print Char Array: 87 ae bd 58 71 75 1d 36 43 ab 1d ef 69 f5 54 d7
Print long Array: 361d757158bdae87 d754f569ef1dab43 发布于 2015-04-18 02:25:34
您使用的实现需要一个key作为keyLen/4ASCII字符,表示key的十六进制值。
因此,您应该更改行:
makeKey(&keyTwofish, DIR_ENCRYPT, key_dim*8, key);转到
makeKey(&keyTwofish, DIR_ENCRYPT, strlen(key)*4, key);因为十六进制值就像。0x30313233343536373839不解释为0123456789,而是解释为0x030003010302030303040405050306030703080309,键0123456789将解释为0x00010203040506070809。
https://stackoverflow.com/questions/29417414
复制相似问题