On 02/03/2016 at 09:28, xxxxxxxx wrote:
Hello,
it seems that the documentation (and the pgp.cpp example) are outdated; some of the information in the documentation is no longer correct.
From what I could learn I created this example, I hope it will help you.
// plain text
const char plainText[] = "O brave new world That has such people in!";
const Int32 length = sizeof(plainText);
GePrint("Plain Text: " + String(plainText));
// secret key
char key[16];
MemCopy(key, "7c30e00bb6271495", 16);
const Int32 keyLength = sizeof(key) * 8; // size in bit
// check key length
if (!((keyLength == 128) || (keyLength == 192) || (keyLength == 256)))
return false;
// block size in bit
const Int32 blockSize = 256;
// memory to store the encrypted data
void* data = nullptr;
Int dataSize = 0;
// encrypt
{
// get size of the memory needed to store the encrypted data
dataSize = AES::CalcEncryptedDataSize(blockSize, length);
// allocate needed memory
data = NewMemClear(char, dataSize);
if (!data)
return false;
// copy the given text into the memory
CopyMem(plainText, data, length);
AutoAlloc<AES> aes;
if (aes && aes->Init(blockSize, keyLength))
{
aes->Encrypt(data, dataSize, key);
}
}
// decrypt
{
AutoAlloc<AES> aes;
if (data && aes && aes->Init(blockSize, keyLength))
{
if (aes->Decrypt(data, dataSize, key))
{
GePrint("Decrypted Data: " + String((char* )data));
}
}
}
DeleteMem(data);
AES is a block cipher. This means for every bit you put in you get one bit out. If only a part of the given (decrypted) data is usable data you could store the size of the usable data within the data block itself or you use something like a null-terminated string (as in the example).
Best wishes,
Sebastian