使用libsodium加密

编程入门 行业动态 更新时间:2024-10-18 21:18:02
本文介绍了使用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加密

本文发布于:2023-11-08 20:15:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:libsodium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!