C ++, gcrypt 라이브러리에서 gcry_cipher_encrypt의 반환 값을 변수에 복사하는 방법은 무엇입니까?

유 리우

아래에 첨부 한 코드에서는 gcry_cipher_encrypt를 사용합니다. 코드 끝에서 encBuffer의 내용을 16 진 값 문자열로 출력합니다. char [], char * 또는 string과 같은 변수로 만들고 사용해야합니다.

gcrypt, encBuffer 핸드북에 따르면 함수의 두 번째 항목은 unsigned char * 유형 변수 여야합니다. 서명되지 않은 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;
}
WPomier

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!

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관