【问题标题】:C++, how to copy the return value of gcry_cipher_encrypt in gcrypt library to a variable?C++,如何将gcrypt库中gcry_cipher_encrypt的返回值复制到变量中?
【发布时间】:2016-11-18 03:53:27
【问题描述】:

在我下面附加的代码中,它使用了 gcry_cipher_encrypt。在代码的末尾,它将 encBuffer 中的内容输出为十六进制值字符串。我需要在 char[]、char* 或 string 之类的变量中创建它并使用它。

根据gcrypt手册encBuffer,函数的第二项应该是一个无符号char*类型的变量。我认为它应该指向一个无符号字符数组。但是当我这样做时:

for(int i = 0; i < txtLength-1;i++){
   cout<<encBuffer[i];
}

我收到大量代码。请问如何从 encBuffer 获取可读内容?非常感谢。

#include <stdio.h>
#include <gcrypt.h>

int main () {
gcry_error_t     gcryError;
gcry_cipher_hd_t gcryCipherHd;
size_t           index;
char * salsaKey = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; // 32 bytes
char * iniVector = "AAAAAAAA"; // 8 bytes

gcryError = gcry_cipher_open(
    &gcryCipherHd, // gcry_cipher_hd_t *
    GCRY_CIPHER_SALSA20,   // int
    GCRY_CIPHER_MODE_STREAM,   // int
    0);            // unsigned int
if (gcryError)
{
    printf("gcry_cipher_open failed:  %s/%s\n",
           gcry_strsource(gcryError),
           gcry_strerror(gcryError));
    return;
}
printf("gcry_cipher_open worked\n");

gcryError = gcry_cipher_setkey(gcryCipherHd, salsaKey, 32);
if (gcryError)
{
    printf("gcry_cipher_setkey failed:  %s/%s\n",
           gcry_strsource(gcryError),
           gcry_strerror(gcryError));
    return;
}
printf("gcry_cipher_setkey worked\n");

gcryError = gcry_cipher_setiv(gcryCipherHd, iniVector, 8);
if (gcryError)
{
    printf("gcry_cipher_setiv failed:  %s/%s\n",
           gcry_strsource(gcryError),
           gcry_strerror(gcryError));
    return;
}
printf("gcry_cipher_setiv worked\n");

size_t txtLength = 101;
char * encBuffer = malloc(txtLength);
char * textBuffer = malloc(txtLength);
memset(textBuffer, 0, 101);

gcryError = gcry_cipher_encrypt(
    gcryCipherHd, // gcry_cipher_hd_t
    encBuffer,    // void *
    txtLength,    // size_t
    textBuffer,    // const void *
    txtLength);   // size_t
if (gcryError)
{
    printf("gcry_cipher_decrypt failed:  %s/%s\n",
           gcry_strsource(gcryError),
           gcry_strerror(gcryError));
    return;
}
printf("gcry_cipher_decrypt worked\n");

printf("encBuffer = ");
for (index = 0; index<txtLength-1; index++)
    printf("%02X", (unsigned char)encBuffer[index]);
printf("\n");
return 0;
}

【问题讨论】:

    标签: c++ c++11 libgcrypt


    【解决方案1】:

    我使用 malloc 制作了以下函数(因为您的代码确实如此)。

    char *buffer2hex(char *encBuffer, int txtLength){
        char *encHexText = (char *)malloc(txtLength*2+1), 
             *eht = encHexText;
        for (int i = 0; i < txtLength; i++){
            int c = (unsigned char)encBuffer[i];
            #define tohex(n) ((n)>9?(n)-10+'A':(n)+'0')
            *eht++ = tohex(c>>4);
            *eht++ = tohex(c&0xf);
            #undef tohex
        }
        *eht = '\0';
        return encHexText; 
    }
    

    在你的方法结束时,你可以这样调用它:

        char *hex = buffer2hex(encBuffer, txtLength);
        printf("%s\n", hex); // Use it
        free(hex);           // and free it!
    

    【讨论】:

    • 我真的很感激。但是真正存储在 encBuffer 中的是什么?以及为什么要分配长度为 txtLength*2+1 的内存?
    • encBuffer 中存储了您的任何方法(我的猜测:一些加密数据字节)。 txtLength == 101(encBuffer 的字节长度),现在以十六进制字符串表示相同的信息,每个字节需要 2 个字符加上字符串最后一个空字符,这就是为什么 txtLength*2+1。
    • 哦,我明白了。非常感谢!
    猜你喜欢
    • 2016-09-26
    • 2020-01-11
    • 2020-04-08
    • 2014-11-22
    • 2011-08-06
    • 2012-09-08
    • 2015-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多