Re: How to do encryption using AES in Openssl
On Thu, Mar 29, 2012 at 5:40 AM, Prashanth kumar N prashanth.kuma...@gmail.com wrote: Thanks Ken for pointing out the mistake... after changing to AES_Decrypt(), it worked but i still see issue when i print the decrypted output as it has extra non-ascii characters in it. Below is the input unsigned char text[]=test12345678abc2; After decryption, i get the following string: Decrypted o/p: test12345678abc2Ȳu�z�B��� ��A��S�� You didn't encrypt the terminating NUL, so the decrypt is unterminated... Few questions... 1. If we use AES, will decrypted files have same number of bytes as encrypted file? (I assume it should be same) 2. When i did Google and found few examples on AES using CBC mode, many of them add extra buffer while decrypting ie., sample eg: unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 10 unsigned char iv[] = {1,2,3,4,5,6,7,8}; 11 unsigned char outbuf[1024]; 12 unsigned char decrebuf[1024]; 13 int outlen,outlen2, tmplen; 14 unsigned char text[]=test12345678abc2; 15 char outfile[]= encfile; if(!EVP_EncryptUpdate(ctx, outbuf, outlen, intext, strlen(intext))) 26 { 27 /* Error */ 28printf(\n Error:EVP_EncryptUpdate ); 29return 0; 30} 31 32if(!EVP_EncryptFinal_ex(ctx, outbuf + outlen, tmplen)) 33 { 34 /* Error */ 35 printf(\n Error: EVP_EncryptFinal_ex); 36 return 0; 37 } EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv); 45 46 if(!EVP_DecryptUpdate(ctx, decrebuf, outlen2, outbuf, outlen)) 47 { 48 printf(\n Error : EVP_DecryptUpdate); 49 return 0; 50 } EVP_DecryptFinal_ex(ctx, decrebuf + outlen2, tmplen ) Here i see even thought decrebuf is 1024, we still offset it by outlen and pass the address to Decrytpion function? 3. Why is it like we have to choose 1024 as array size... when i know my encryption text is only 16bytes. Any reasons? -Prashanth On Wed, Mar 28, 2012 at 7:29 PM, Ken Goldman kgold...@us.ibm.com wrote: On 3/28/2012 3:01 AM, Prashanth kumar N wrote: Here is the modified program [snip] 18 AES_KEY ectx; 19 AES_KEY dectx; 20 21 AES_set_encrypt_key(key, 256, ectx); 22 AES_encrypt(text, out, ectx); 23 24 printf(encryp data = %s\n, out); 25 26 AES_set_encrypt_key(key, 256, dectx); AES_set_decrypt_key() 27 AES_decrypt(out, decout, dectx); __**__** __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager majord...@openssl.org
[no subject]
Hi , I am new to this openssl libraries. I am facing a issue in the below code. When I encrypt, it is giving all zeroes as output. And when I decrypt I am not getting the exact message. Please, I need help in this. #define BUFSIZE 1024 int main(int argc, char *argv[]) { int i; unsigned char out[BUFSIZE]={0}; unsigned char in[]={0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00}; for(i=0;i8;i++) printf(%2X\n,in[i]); cryptWrapper(in, out); printf(Encrypted/Decrypted string is: [%s]\n, out); return 0; } int cryptWrapper(unsigned char *in, unsigned char *out) { unsigned char EncrOut[BUFSIZE]={0}; unsigned char DecrOut[BUFSIZE]={0}; int ret; unsigned char tdesKey1[8] ={0xEF,0x38,0xE0,0xE0,0x58,0xA7,0x08,0x8C}; unsigned char tdesKey2[8] ={0x51,0x01,0x37,0xFE,0xBC,0xE0,0x4C,0x85}; unsigned char initVector[8] ={0x38,0x37,0x36,0x35,0x34,0x33,0x32,0x31};// The initializationvector int i; des_key_schedule kSched1, kSched2; des_cblock iv; memcpy(iv,initVector,sizeof(initVector)); // set the initialization vector ret=DES_set_key_checked(tdesKey1, kSched1); printf(ret %d\n,ret);// set the key schedule DES_set_key_checked(tdesKey2, kSched2); // set the key schedule //DES_set_key_checked(tdesKey3, kSched3); // set the key schedule for(i=0;i8;i++) printf(%2X\n,in[i]); memset(out, 0, BUFSIZE); des_ede2_cbc_encrypt(in, EncrOut, (long)strlen((char*)in), kSched1,kSched2, iv, DES_ENCRYPT); // Encrypt for(i=0;i8;i++) printf(Encrypted %X\n,EncrOut[i]); memcpy(iv,initVector,sizeof(initVector)); DES_set_key_checked(tdesKey1, kSched1); DES_set_key_checked(tdesKey2, kSched2); //DES_set_key_checked(tdesKey3, kSched3); des_ede2_cbc_encrypt((unsigned char*)EncrOut, DecrOut,(long)strlen((char*)EncrOut), kSched1, kSched2, iv,DES_DECRYPT); for(i=0;i8;i++) printf(Decrypted %X\n,DecrOut[i]); strcpy((char *) out, (char *) DecrOut); return 0; } Regards, ChandraSekhar.
Re: No Subject
What do you think strlen(in) will return? You are mixing up variable length C strings (nul terminated) with binary data - always pass the true data length Carl On Thu 29/03/12 12:58 PM , Chandrasekhar chandrasek...@evolute-sys.com sent: Hi , I am new to this openssl libraries. I am facing a issue in the below code. When I encrypt, it is giving all zeroes as output. And when I decrypt I am not getting the exact message. Please, I need help in this. #define BUFSIZE 1024 int main(int argc, char *argv[]) { int i; unsigned char out[BUFSIZE]={0}; unsigned char in[]={0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00}; for(i=0;i __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager majord...@openssl.org
Error cross-compiling openssl 1.X on mingw-w64
Hello to all. I'm using mingw-w64 (targeting win32) to cross compile openssl. I'm using a current snapshot of mingw-w64 (gcc-4.7.0) and openssl-1.0.1 (but I had the same error with 1.0.0) and tried in two different machines, one with ubuntu 11.04 32-bit and another with kubuntu 11.10 64-bit. The error is the same. The Configure parameters I'm using is: --prefix=$BUILD_PATH no-shared threads mingw32:gcc --cross-compile-prefix=i686-w64-mingw32- I also tried to use mingw64:gcc but I had the same results. The error happens in ocsp.h:157 and I couldn't understand why it happens. Maybe some obscure #macro... The (tentative of) compilation output can be found in: http://pastebin.com/UVstPwQZ Thanks in advance -- Atenciosamente, Leandro __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager majord...@openssl.org
Re: Error cross-compiling openssl 1.X on mingw-w64
Hello to all. I'm using mingw-w64 (targeting win32) to cross compile openssl. I'm using a current snapshot of mingw-w64 (gcc-4.7.0) and openssl-1.0.1 (but I had the same error with 1.0.0) and tried in two different machines, one with ubuntu 11.04 32-bit and another with kubuntu 11.10 64-bit. The error is the same. The Configure parameters I'm using is: --prefix=$BUILD_PATH no-shared threads mingw32:gcc --cross-compile-prefix=i686-w64-mingw32- I also tried to use mingw64:gcc but I had the same results. The error happens in ocsp.h:157 and I couldn't understand why it happens. Maybe some obscure #macro... The (tentative of) compilation output can be found in: http://pastebin.com/UVstPwQZ Thanks in advance -- Atenciosamente, Leandro __ OpenSSL Project http://www.openssl.org User Support Mailing Listopenssl-users@openssl.org Automated List Manager majord...@openssl.org