我一直在努力使用crypto_secretbox_easy()在利伯钠中加密/解密一些数据。我似乎找不到任何关于使用的好文件。
我想从用户那里得到一个密码,用它来做一个密钥,然后用它加密/解密数据。
下面我发布的玩具代码的问题是,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);发布于 2014-05-14 05:07:57
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);为了从密码中派生密钥,钠提供了crypto_pwhash_scryptsalsa208sha256。
发布于 2014-03-24 14:45:13
密码的大小应该比MAC字节的消息大16字节,因此分配16个字节,在open_easy上只需向message_len添加+ 16。
另外,您对calloc的调用实际上分配了比所需的内存更多的内存,因为calloc在方法中执行乘法操作。
https://stackoverflow.com/questions/22450518
复制相似问题