0

我正在尝试移植一个使用 scrypt 生成密码的 python 脚本。

import pylibscrypt
scrypt = pylibscrypt.scrypt

password = 'test123'.encode("utf_16_be", "replace")
salt = '384eaed91035763e'.decode('hex')

key = scrypt(password, salt, 16384, 8, 1, 32)
print (key.encode('hex'))

结果应该是:9f2117416c7b2de9419a81c4625a28e40c16ac92aaa3000bb2c35844b3839eb1

在 C 中,我使用 libsodium crypto_pwhash_scryptsalsa208sha256_ll() 函数。

如果我尝试从该变量传递密码:

const uint8_t password[] = "test123"; 

我没有得到相同的密钥。我试图从 python 脚本中删除 ".encode("utf_16_be", "replace")" ,然后得到相同的结果。

那么如何将我的 C 变量转换为与 python 变量相同的变量呢?

编辑:原始软件是 Java 并且似乎将字符串编码为 UTF-16 大端。现在我需要将 C 字符串编码为相同的格式。

4

1 回答 1

0

我找到了一个解决方案,灵感来自:Create UTF-16 string from char*

 size_t uint8_t_UTF_16(uint8_t *input, uint8_t *output, size_t inlen) {
  size_t outlen = 0.0;

  for (size_t i = 0; i < inlen; i++) {
    if (isascii(input[i])) {
      output[i] = 0;
      output[i + 1] = input[i / 2];
      i++;
      outlen += 2;
    } else {
      output[i] = input[i];
      outlen += 0.5;
    }
  }

  return outlen;
}

它确实有效,但我不确定这个解决方案是否安全。

于 2017-02-15T18:11:50.680 回答