gpt4 book ai didi

使用 libsodium 加密

转载 作者:行者123 更新时间:2023-12-04 15:34:16 37 4
gpt4 key购买 nike

我一直在努力使用 crypto_secretbox_easy() 在 libsodium 中加密/解密一些数据| .我似乎找不到关于使用的任何好的文档。

我想从用户那里获取密码,用它来以某种方式制作 key ,然后使用它加密/解密数据。

我在下面发布的玩具代码的问题是 crypto_secretbox_open_easy() 从 verify_16.c 中返回 -1。有谁知道在哪里可以找到显示如何使用此界面的源代码或可能出现什么问题?谢谢!

 unsigned char * cipher;
unsigned char * decoded;
unsigned char * message;
unsigned long long message_len = 32;
size_t noncelen = sizeof(char) * crypto_secretbox_noncebytes();
size_t keylen = sizeof(char) * crypto_secretbox_keybytes();
unsigned char * nonce = calloc(noncelen, noncelen);
unsigned char * key = calloc(keylen, keylen);

message = calloc(32*sizeof(char), sizeof(char) * 32);
cipher = calloc(32*sizeof(char), sizeof(char) * 32);
decoded = calloc(32*sizeof(char), sizeof(char) * 32);

crypto_secretbox_easy((unsigned char *)cipher, (const unsigned char *)message,
message_len, nonce, key);

crypto_secretbox_open_easy((unsigned char *)decoded, (const unsigned char *) cipher,
message_len, nonce, key);

最佳答案

test/secretbox_easy2.c file (在钠源代码中)展示了如何使用它:

randombytes_buf(nonce, sizeof nonce);
crypto_secretbox_easy(c, m, mlen, nonce, k);
crypto_secretbox_open_easy(decoded, c, mlen + crypto_secretbox_MACBYTES,
nonce, k);

为了从密码中推导出 key ,sodium 提供了 crypto_pwhash_scryptsalsa208sha256 .

关于使用 libsodium 加密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22450518/

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