使用libsodium加密 [英] Encryption using libsodium
问题描述
我一直在使用 libsodium 中的crypto_secretbox_easy()来加密/解密某些数据.我似乎找不到有关此用法的任何好的文档.
I have been struggling to encrypt/decrypt some data using crypto_secretbox_easy() in libsodium. I can't seem to find any good documentation on the usage.
我想从用户那里获得密码,使用它以某种方式创建密钥,然后使用该密码对数据进行加密/解密.
I want to get a password from the user, use that to somehow make a key, then encrypt/decrypt the data using that.
我在下面发布的玩具代码的问题是crypto_secretbox_open_easy()从verify_16.c内部返回-1.有谁知道在哪里可以找到显示如何使用此界面的源或出了什么问题的想法?谢谢!
The problem with the toy code that I have posted below is that the crypto_secretbox_open_easy() returns -1 from within verify_16.c. Does anyone have any idea where I could find source showing how to use this interface or what could be going wrong? Thanks!
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文件
(在钠源代码中)显示了如何使用它:
The test/secretbox_easy2.c file
(in the sodium source code) shows how to use it:
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);
为了从密码派生密钥,sodium提供了 crypto_pwhash_scryptsalsa208sha256
.
In order to derive a key from a password, sodium provides crypto_pwhash_scryptsalsa208sha256
.
这篇关于使用libsodium加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!