gpt4 book ai didi

python - 将 C uint8_t 转换为 python utf_16_be

转载 作者:行者123 更新时间:2023-11-30 16:50:16 25 4
gpt4 key购买 nike

我正在尝试移植一个使用 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"; 

我没有得到相同的 key 。我尝试从 python 脚本中删除“.encode(“utf_16_be”,“replace”)”,然后得到相同的结果。

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

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

最佳答案

我找到了一个解决方案,灵感来自: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;
}

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

关于python - 将 C uint8_t 转换为 python utf_16_be,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42253073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com