Thanks for the examples. :-)
You wrote:
> In CBC mode, your encrypted data can be bigger for one block (for padding).
But how will I know how big the Buffer
A) must be before encryption
B) and will be filled after encryption?
Example: I want to encrypt 133 Bytes. So I need 9*16 Byte plus maybe one for
padding. I give AES_cbc_encrypt a 160 Byte Buffer. But after encryption I dont
know if 144 Bytes are used or 160 Bytes. Is there a formula?
#include <stdio.h>
#include <string.h>
#include <openssl/aes.h>
#include <cmath>
#include <cstdlib>
unsigned char key32[] =
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31};
void OpenSSLEncAes(
const unsigned char *ToEncrypt,
const unsigned int InLength,
unsigned char **Encrypted,
unsigned int *OutLength) {
AES_KEY aeskey;
unsigned int BufferLen;
unsigned char iv[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
// How big must be the buffer? My solution:
// Round up to fill the last 16 Byte block AND add one additional block
for padding:
BufferLen = (unsigned int)(16*ceil(1+InLength/16.0)); // 16 = AES block
size
AES_set_encrypt_key(key32, 32*8, &aeskey);
*Encrypted = new unsigned char[BufferLen];
AES_cbc_encrypt(ToEncrypt, *Encrypted, BufferLen, &aeskey, iv,
AES_ENCRYPT);
fwrite(*Encrypted, 1, 16, stdout);
}
int main() {
unsigned char *Encrypted=0;
unsigned int OutLen=0;
unsigned char *ToEncrypt=(unsigned char *)"marek";
OpenSSLEncAes(ToEncrypt, strlen((char *)ToEncrypt), &Encrypted,
&OutLen);
return 0;
}
--
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser
______________________________________________________________________
OpenSSL Project http://www.openssl.org
User Support Mailing List [email protected]
Automated List Manager [EMAIL PROTECTED]