首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Serpent和Twofish库C

Serpent和Twofish库C
EN

Stack Overflow用户
提问于 2015-04-03 00:02:10
回答 1查看 335关注 0票数 1

我需要用Serpent和Twofish加密一个无符号字符数组(16字节长)。我在c-中使用了两个来自官方网站的库-write。我尝试使用它们,但我将获得的密文不正确。为了检查,我使用了这个site。我不明白哪里出了错。

我的代码是:

代码语言:javascript
复制
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时,结果是:

代码语言:javascript
复制
Size of Char: 1
Size of Integer: 4
Size of Long: 8

当我这样做的时候:

代码语言:javascript
复制
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);

哪里

代码语言:javascript
复制
void test (unsigned long * a, int b)
{
   for (int i = 0; i < b/sizeof(long); ++i)
      printf("%lx ", a[i]);
}

我明白了:

代码语言:javascript
复制
Print Char Array: 87 ae bd 58 71 75 1d 36 43 ab 1d ef 69 f5 54 d7 
Print long Array: 361d757158bdae87 d754f569ef1dab43 
EN

回答 1

Stack Overflow用户

发布于 2015-04-18 02:25:34

您使用的实现需要一个key作为keyLen/4ASCII字符,表示key的十六进制值。

因此,您应该更改行:

代码语言:javascript
复制
makeKey(&keyTwofish, DIR_ENCRYPT, key_dim*8, key);

转到

代码语言:javascript
复制
makeKey(&keyTwofish, DIR_ENCRYPT, strlen(key)*4, key);

因为十六进制值就像。0x30313233343536373839不解释为0123456789,而是解释为0x030003010302030303040405050306030703080309,键0123456789将解释为0x00010203040506070809。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29417414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档