How to decrypt / encrypt using libgcrypt ? (ARC4)
Howdy ,..
I'm reaading libgcrypt manual but
I cannot understand what values i should use in encrypt /decrypt when using gcry_cipher_encrypt :
for example when i run this code :
Code:
#include <stdio.h>
#include <gcrypt.h>
#include <assert.h>
int main()
{
gcry_cipher_hd_t handle;
gcry_error_t err = 0;
char * plain_text ;
char * out ;
char * deout ;
size_t size_of_plain = sizeof(char) * 6;
size_t size_of_crypt = sizeof(char) * 601;
plain_text = malloc (size_of_plain);
out = malloc (size_of_crypt);
deout = malloc (size_of_crypt);
assert(plain_text);
assert(out);
assert(deout);
strcpy(plain_text , "Secret");
gcry_check_version (NULL);
gcry_control( GCRYCTL_DISABLE_SECMEM_WARN );
gcry_control( GCRYCTL_INIT_SECMEM, 16384, 0 );
{
err = gcry_cipher_open (&handle, GCRY_CIPHER_ARCFOUR,GCRY_CIPHER_MODE_STREAM,0);
if (err)
{
fprintf (stderr, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
fprintf (stdout, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
}
err = gcry_cipher_setkey (handle, "This is really strange , i read the documentation i googled for two days and nothing , i can't encrypt && decrypt",128);
if (err)
{
fprintf (stderr, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
fprintf (stdout, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
}
}
err = gcry_cipher_encrypt (handle,
(unsigned char *)out, sizeof(out), (const unsigned char *)plain_text,sizeof plain_text);
if (err)
{
fprintf (stderr, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
fprintf (stdout, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
}
printf(" out :%s ||| plain text %s\n",out, plain_text);
err = gcry_cipher_encrypt (handle,
(unsigned char *)deout, sizeof deout, (const unsigned char *)out,sizeof out);
if (err)
{
fprintf (stderr, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
fprintf (stdout, "Failure: %s/%s\n",
gcry_strsource (err),
gcry_strerror (err));
}
printf("deout: %s ||| out: %s ||| plain text: %s\n",deout, out,plain_text);
free(plain_text);
free(out);
free(deout);
gcry_cipher_close(handle);
return 0;
}
i get the next output :
Code:
��g�zB�QK]o
��A�R���� 2�9�
while i should get according to wikipedia :
Code:
45A01F645FC35B383552544B9BF5
Attack at down
What should i do to fix it ?
Example AES gcrypt gcry_cipher_encrypt code
I was searching for a simple AES encryption example using the gcrypt library and surprisingly this is the only page I found. The example code given here was totally inadequate for my needs so I had to write my own. It would have been nice if I could have found a good example so I thought others may like to see my example now that it is done. The output in CBC mode is:
Code:
gcry_cipher_open worked
gcry_cipher_setkey worked
gcry_cipher_setiv worked
gcry_cipher_encrypt worked
gcry_cipher_setiv worked
gcry_cipher_decrypt worked
keyLength = 16
blkLength = 16
txtLength = 64
aesSymKey = one test AES key
iniVector = a test ini value
txtBuffer = 123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ
encBuffer = A2096693C727CB6114907E15F1C2CA66A77D482DA6191C5DACB57F77DE10B65
CC53F7FF2AB1C9F665B5722789DD57416C9B6105E67C32EFA9F02C9E1D7747F87
outBuffer = 123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ
The output in ECB mode is:
Code:
gcry_cipher_open worked
gcry_cipher_setkey worked
gcry_cipher_setiv worked
gcry_cipher_encrypt worked
gcry_cipher_setiv worked
gcry_cipher_decrypt worked
keyLength = 16
blkLength = 16
txtLength = 64
aesSymKey = one test AES key
iniVector = a test ini value
txtBuffer = 123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ
encBuffer = 72DE8D0017E14AA35C8716B33355925CAC22D29373B01A64FED5D7D0D3F07CD
227CE13D4E2400948CAB715ABA86ECF9FA5F71131A95F72167D3BCAD9892A051A
outBuffer = 123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ
And here is the simple test code:
Code:
static void aesTest(void)
{
#define GCRY_CIPHER GCRY_CIPHER_AES128 // Pick the cipher here
#define GCRY_C_MODE GCRY_CIPHER_MODE_ECB // Pick the cipher mode here
gcry_error_t gcryError;
gcry_cipher_hd_t gcryCipherHd;
size_t index;
size_t keyLength = gcry_cipher_get_algo_keylen(GCRY_CIPHER);
size_t blkLength = gcry_cipher_get_algo_blklen(GCRY_CIPHER);
char * txtBuffer = "123456789 abcdefghijklmnopqrstuvwzyz ABCDEFGHIJKLMNOPQRSTUVWZYZ";
size_t txtLength = strlen(txtBuffer)+1; // string plus termination
char * encBuffer = malloc(txtLength);
char * outBuffer = malloc(txtLength);
char * aesSymKey = "one test AES key"; // 16 bytes
char * iniVector = "a test ini value"; // 16 bytes
gcryError = gcry_cipher_open(
&gcryCipherHd, // gcry_cipher_hd_t *
GCRY_CIPHER, // int
GCRY_C_MODE, // 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, aesSymKey, keyLength);
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, blkLength);
if (gcryError)
{
printf("gcry_cipher_setiv failed: %s/%s\n",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_cipher_setiv worked\n");
gcryError = gcry_cipher_encrypt(
gcryCipherHd, // gcry_cipher_hd_t
encBuffer, // void *
txtLength, // size_t
txtBuffer, // const void *
txtLength); // size_t
if (gcryError)
{
printf("gcry_cipher_encrypt failed: %s/%s\n",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_cipher_encrypt worked\n");
gcryError = gcry_cipher_setiv(gcryCipherHd, iniVector, blkLength);
if (gcryError)
{
printf("gcry_cipher_setiv failed: %s/%s\n",
gcry_strsource(gcryError),
gcry_strerror(gcryError));
return;
}
printf("gcry_cipher_setiv worked\n");
gcryError = gcry_cipher_decrypt(
gcryCipherHd, // gcry_cipher_hd_t
outBuffer, // void *
txtLength, // size_t
encBuffer, // 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("keyLength = %d\n", keyLength);
printf("blkLength = %d\n", blkLength);
printf("txtLength = %d\n", txtLength);
printf("aesSymKey = %s\n", aesSymKey);
printf("iniVector = %s\n", iniVector);
printf("txtBuffer = %s\n", txtBuffer);
printf("encBuffer = ");
for (index = 0; index<txtLength; index++)
printf("%02X", (unsigned char)encBuffer[index]);
printf("\n");
printf("outBuffer = %s\n", outBuffer);
// clean up after ourselves
gcry_cipher_close(gcryCipherHd);
free(encBuffer);
free(outBuffer);
}
Burt Wagner - LinkedIn
Senior Firmware Engineer
Deadline Specialists, Inc.
Post Office Box 18985
Boulder, CO 80308 USA