【问题标题】:Serpent and Twofish library CSerpent 和 Twofish 库 C
【发布时间】:2015-06-07 16:06:59
【问题描述】:

我需要用 Serpent 和 Twofish 加密一个无符号字符数组(16 字节长)。我使用官方网站上的两个库-write in c-。我尝试使用它们,但我将获得的密文不正确。为了检查我使用这个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 

【问题讨论】:

  • 您正在使用参考实现并使用随机网站对其进行测试?你不认为这是错误的方法吗?与TwofishSerpent 测试向量进行比较呢?
  • @Laura Renoldi:不确定您使用哪种实现,但在这里:cpansearch.perl.org/src/MLEHMANN/Crypt-Twofish2-1.02/twofish.c,例如 - 它说 makeKey 应该接收编码为十六进制字符串的密钥。因此,如果您的密钥是 255 255 255,您应该传递“FFFFFF”。
  • 请注意,如果我在网站上将所有内容都输入为十六进制(您应该这样做),我确实会为给定的测试向量得到正确的值,无论是对于 twofish 还是对于 serpent(128 位密钥)。请注意,serpent 的键以 8 而不是 0 开头。
  • @MaartenBodewes 我认为我的密文不仅对于该站点是错误的,还因为对于大量纯文本,所有密文都是这样的:xxxx xxxx 0000 0000 xxxx xxxx 0000 0000,其中 xx 是十六进制数。
  • @Giorgi 是的,我的密钥被编码为十六进制字符串。

标签: c cryptography twofish


【解决方案1】:

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

所以你应该换行:

makeKey(&keyTwofish, DIR_ENCRYPT, key_dim*8, key);

进入

makeKey(&keyTwofish, DIR_ENCRYPT, strlen(key)*4, key);

因为十六进制值,例如。 0x30313233343536373839 不会被解释为 0123456789 而是 0x0300030103020303030403050306030703080309 和密钥 0123456789 将被解释为 0x00010203040506070809。

【讨论】:

    猜你喜欢
    • 2016-01-09
    • 2016-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    相关资源
    最近更新 更多