0
아래 첨부 된 코드에서 gcry_cipher_encrypt를 사용합니다. 코드의 끝에서 encBuffer의 내용을 16 진수 값 문자열로 출력합니다. char [], char *, 또는 string과 같은 변수를 만들어서 사용해야합니다.C++, gcrypt 라이브러리의 gcry_cipher_encrypt 반환 값을 변수에 복사하는 방법은 무엇입니까?
gcrypt, encBuffer 핸드북에 따르면 함수의 두 번째 항목은 unsigned 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;
}
내가 정말 감사드립니다. 하지만 실제로 encBuffer에 저장되는 것은 무엇입니까? 왜 txtLength * 2 + 1 길이의 메모리를 할당할까요? –
encBuffer에는 사용자의 메서드가 저장되어 있습니다 (내 추측 : 일부 암호화 된 데이터 바이트). txtLength == 101 (encBuffer의 바이트 길이), 이제는 동일한 정보를 16 진수 문자열로 나타 내기 위해 문자열마다 2 개의 chars와 하나의 final null char이 필요합니다. txtLength * 2 + 1입니다. – WPomier
오, 알았어요. 고마워요! –