我需要用Serpent和Twofish来加密一个unsigned char数组(16个字节长)。我在官方网站上使用了两个库-crite。我尝试使用它们,但我将获得的密文不正确。为了检查我使用这个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);
对于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
答案 0 :(得分:0)
您使用的实现需要一个键作为keyLen / 4 ASCII字符,表示该键的十六进制值。
所以你应该换行:
makeKey(&keyTwofish, DIR_ENCRYPT, key_dim*8, key);
到
makeKey(&keyTwofish, DIR_ENCRYPT, strlen(key)*4, key);
因为Hexvalues就像例如。 0x30313233343536373839不会被解释为0123456789,而是被解释为0x0300030103020303030403050306030703080309,密钥0123456789将被解释为0x00010203040506070809。