Idealmente, si potrebbe utilizzare uno strumento già esistente, come ccrypt
, ma qui va:
#include <openssl/aes.h>
/* ... */
{
int bytes_read, bytes_written;
unsigned char indata[AES_BLOCK_SIZE];
unsigned char outdata[AES_BLOCK_SIZE];
/* ckey and ivec are the two 128-bits keys necesary to
en- and recrypt your data. Note that ckey can be
192 or 256 bits as well */
unsigned char ckey[] = "thiskeyisverybad";
unsigned char ivec[] = "dontusethisinput";
/* data structure that contains the key itself */
AES_KEY key;
/* set the encryption key */
AES_set_encrypt_key(ckey, 128, &key);
/* set where on the 128 bit encrypted block to begin encryption*/
int num = 0;
while (1) {
bytes_read = fread(indata, 1, AES_BLOCK_SIZE, ifp);
AES_cfb128_encrypt(indata, outdata, bytes_read, &key, ivec, &num,
AES_ENCRYPT);
bytes_written = fwrite(outdata, 1, bytes_read, ofp);
if (bytes_read < AES_BLOCK_SIZE)
break;
}
}
decrittografia è fatto chiamando AES_cfb128_encrypt
con AES_DECRYPT
come ultimo parametro. Si noti che questo codice non ha ricevuto nulla di più del più elementare dei test e che è necessario utilizzare i dati casuali appropriati di 8 bit per ckey e ivec.
EDIT: Sembra AES_cfb128_encrypt
accetta i dati di lunghezza arbitraria, in modo che non sei tenuto a crittografare in blocchi di AES_BLOCK_SIZE
(16) byte.
fonte
2009-06-18 06:09:37
Una domanda con un esempio simile http://stackoverflow.com/questions/3141860/aes-ctr-256-encryption-mode-of- operation-on-openssl – enthusiasticgeek
Dovresti usare le funzioni 'EVP_ *'. Le funzioni 'EVP_ *' usano l'hardware, come AES-NI (se disponibile). Vedi [Codifica simmetrica EVP e decrittografia] (https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption) sul wiki OpenSSL.In effetti, probabilmente dovresti utilizzare la crittografia autenticata perché fornisce * sia * riservatezza e autenticità. Vedi [Crittografia e decodifica autenticata EVP] (https://wiki.openssl.org/index.php/EVP_Authenticated_Encryption_and_Decryption) sul wiki OpenSSL. – jww