加密/解密-Libsodium(初级)中经过密钥验证的加密

时间:2018-08-10 12:07:37

标签: c encryption libsodium

我正在尝试按照此处的示例Here

来加密和解密简单的字符串
#define MESSAGE ((const unsigned char *) "test")
#define MESSAGE_LEN 4
#define CIPHERTEXT_LEN (crypto_secretbox_MACBYTES + MESSAGE_LEN)

unsigned char key[crypto_secretbox_KEYBYTES];
unsigned char nonce[crypto_secretbox_NONCEBYTES];
unsigned char ciphertext[CIPHERTEXT_LEN];

crypto_secretbox_keygen(key);
randombytes_buf(nonce, sizeof nonce);
crypto_secretbox_easy(ciphertext, MESSAGE, MESSAGE_LEN, nonce, key);

unsigned char decrypted[MESSAGE_LEN];
if (crypto_secretbox_open_easy(decrypted, ciphertext, CIPHERTEXT_LEN, nonce, 
key) != 0) {
    /* message forged! */
}

我的问题是:如何找回原始文本? 'test'在这种情况下? Libsodium似乎始终使用unsigned char*。所有Libsodium示例似乎都以"Message forged!"结尾。后续问题,如何将密文转换为可序列化的格式?

1 个答案:

答案 0 :(得分:1)

首先,libsodium有据可查,因此易于理解,尽管事实上受测者有时可能会有些棘手,所以是不错的选择。

我已经在您的代码中添加了注释,以阐明其工作方式。我还添加了转储功能以简化代码理解。

#include <stdlib.h>
#include <stdio.h>
#include <sodium.h>

#define MESSAGE ((const unsigned char *) "test")
#define MESSAGE_LEN 4
#define CIPHERTEXT_LEN (crypto_secretbox_MACBYTES + MESSAGE_LEN)

void dump_hex_buff(unsigned char buf[], unsigned int len)
{
    int i;
    for (i=0; i<len; i++) printf("%02X ", buf[i]);
    printf("\n");
}

int main(int argc, char *argv[])
{
    /* Variable declaration */
    unsigned char key[crypto_secretbox_KEYBYTES];
    unsigned char nonce[crypto_secretbox_NONCEBYTES];
    unsigned char ciphertext[CIPHERTEXT_LEN];
    unsigned char decrypted[MESSAGE_LEN];

    /* Generating a random key */
    crypto_secretbox_keygen(key);
    printf("secret key generated:\n");
    dump_hex_buff(key, crypto_secretbox_KEYBYTES);

    /* Using random bytes for a nonce buffer (a buffer used only once) */
    randombytes_buf(nonce, sizeof nonce);
    printf("nonce:\n");
    dump_hex_buff(nonce, sizeof nonce);

    /* Encrypt MESSAGE using key and nonce
       Encrypted message is stored in ciphertext buffer */
    crypto_secretbox_easy(ciphertext, MESSAGE, MESSAGE_LEN, nonce, key);
    printf("ciphertext:\n");
    dump_hex_buff(ciphertext, CIPHERTEXT_LEN);



    /* Decrypt ciphertext buffer using key and nounce
       Decrypted message is stored in decrypted buffer */
    if (crypto_secretbox_open_easy(decrypted, ciphertext, CIPHERTEXT_LEN, nonce, key) != 0) {
        /* message forged!, meaning decryption failed */

    } else {
        /* Successful decryption */
        printf("decrypted data (hex):\n");
        dump_hex_buff(decrypted, MESSAGE_LEN);
        printf("decrpyted data (ascii):%.4s\n", decrypted);
    }

    return 0;
}

当您说需要序列化解密的数据时,需要弄清想要的内容。该库使用标准C数组。

如果您使用的是Linux,则可以使用以下代码编译代码:

gcc -o pgm main.c /usr/local/lib/libsodium.a

代码的输出结果:

secret key generated:
87 28 B9 43 0E DA B5 37 CD 3A 67 A3 DB 4A 31 24 67 4E E3 81 AE 03 FB 81 B7 60 2E 1A F3 6A A6 F4 
nonce:
7B 11 84 24 55 24 98 7A 6B 0B 23 34 66 48 8F 1C C3 4E 20 3E 42 31 02 9B 
ciphertext:
00 BC C0 86 61 37 00 0D 90 76 46 C3 17 39 A5 00 E3 F8 A8 9D 
decrypted data (hex):
74 65 73 74 
decrpyted data (ascii):test