2

我找不到一个很好的例子,所以我创建了一个你可以更改 mykey 的例子,并使用其中一个 gcrypt 函数使 iv 随机。

样本

#include <iostream>
#include <gcrypt.h>

typedef unsigned char byte;
using namespace std;

#define BLOCK 16
const byte mykey []= {0x2d,0x65,0x20,0x74,0x69,0x6D,0x65,0x20,0x66,0x6F,0x72};
const byte end1[] ={0x0};
const byte message [] = {0x4E,0x6F,0x77,0x20,0x69,0x73,0x20,0x74,0x68,0x65,0x20,0x74,0x69,0x6D,0x65,0x20,0x66,0x6F,0x72,0x20,0x61,0x6C,0x6C,0x20,0x67,0x6F,0x6F,0x64,0x20,0x6D,0x65,0x6E,0x20,0x74,0x6F,0x20,0x63,0x6F,0x6D,0x65,0x20,0x74,0x6F,0x20,0x74,0x68,0x65,0x20,0x61,0x69,0x64,0x65,0x20,0x6F,0x66,0x20,0x74,0x68,0x65,0x20,0x63,0x6F,0x75,0x6E,0x74,0x72,0x79,0x2E,0x00 };
const byte end[] = {0x00};

// encrypt 
//  Encrypt data in BLOCK in place if EDFLAG is zero; otherwise decrypt
//     block in place.

#define roundup(x,n) \
n *((sizeof(x))/n + (((sizeof(x))%n)+1)/(((sizeof(x))%n)+1))

void verify ( gcry_error_t err, const char* msg ) { 
if (err) {
 cout << msg << "   " << gcry_strsource(err)<< " / " <<             gcry_strerror(err) << endl;
 exit(1);
}
}

/* PROGMA BEGIN 
 *  encrypt decrypt 
 *  using the GCRYPT     */
int main (){

gcry_error_t err = 0;

// init the lib .. 
if (!gcry_check_version (GCRYPT_VERSION))
{     cout << "gcrypt: library version mismatch" << endl; exit(1); }
err = gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
err |= gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
err |= gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
err |= gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
verify (err,"gcrypt: failed initialization");
//init the operation mode

#define   GCRY_CIPHER  GCRY_CIPHER_AES128
#define   GCRY_MODE    GCRY_CIPHER_MODE_ECB
//#define GCRY_FLAG  GCRY_CIPHER_ENABLE_SYNC
#define GCRY_FLAG 0
gcry_cipher_hd_t handle;

err = gcry_cipher_open( &handle, GCRY_CIPHER, GCRY_MODE, GCRY_FLAG);
verify ( err, "gcry_cipher_open" ) ;

byte iv[BLOCK];
memset(iv,0x23,BLOCK); // just for example, we should get it from the cipher word, first 16 bytes
memset(iv, 0x89 , BLOCK -4);
err = gcry_cipher_setiv(handle,iv,BLOCK);
verify( err, "set iv ");
cout <<"iv seT: "<< BLOCK << ":"<< iv << endl;

err = gcry_cipher_setkey(handle, mykey, sizeof(mykey));
verify( err, "set KeY ");
cout << "KeY set: "<< sizeof(mykey) <<":";// << mykey << endl;
for ( int i = 0 ; i <sizeof(mykey) ; ++i ) cout << mykey[i]; 
cout << endl;
// init done

// in = message 
// out - new 
unsigned char out[ roundup(message,BLOCK) ];
memset(out,0,roundup(message,BLOCK) );
//memcpy( out, message, sizeof(message)); 
size_t outsize = sizeof(out);

err = gcry_cipher_encrypt( handle, out, outsize, message, outsize);
verify( err, "encrypt " );
cout <<endl<< "encrypt in: " << sizeof(message) << ":" << message << endl ;
cout << "encrypt out: " << outsize  << ":"<< out << endl<<endl;


unsigned char decrypt[roundup(out,BLOCK)]; // to out 
memset(decrypt, 0 , roundup(out,BLOCK) );
size_t dsize= sizeof(decrypt);
err = gcry_cipher_decrypt( handle, decrypt, dsize, out, outsize);
verify( err, "decrypt " );
cout << "decrypt in: " << outsize  << ":";
for ( int i = 0 ; i < outsize ; ++i ) cout << out[i]; 
cout << endl;
cout << "decrypt out: " << dsize  << ":"<< decrypt << endl;

gcry_cipher_close(handle);

return 0;
}



/* g++ -std=c++11 xxx.cpp -L/usr/lib64 -lgcrypt -lgpg-error*/

欢迎任何评论

我知道它看起来不太好,尽量简洁

4

0 回答 0