首页
学习
活动
专区
圈层
工具
发布

using加密
EN

Stack Overflow用户
提问于 2014-03-17 09:19:26
回答 2查看 3.5K关注 0票数 6

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

我想从用户那里得到一个密码,用它来做一个密钥,然后用它加密/解密数据。

下面我发布的玩具代码的问题是,crypto_secretbox_open_easy()从verify_16.c内部返回-1。有没有人知道我在哪里可以找到源代码,说明如何使用这个界面,或者可能出了什么问题?谢谢!

代码语言:javascript
复制
 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);
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-14 05:07:57

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

代码语言:javascript
复制
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

票数 3
EN

Stack Overflow用户

发布于 2014-03-24 14:45:13

密码的大小应该比MAC字节的消息大16字节,因此分配16个字节,在open_easy上只需向message_len添加+ 16。

另外,您对calloc的调用实际上分配了比所需的内存更多的内存,因为calloc在方法中执行乘法操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22450518

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档