AES字串加解密(跨平台) - Linux

Table of Contents

小弟利用AES作字串加解密,在windows和Linux測試成功

但是,跨平台卻無法正確解密,附上source code,麻煩各位大大給小弟一些可能的問題

Encrypt in Windows:

aes_context ctx;
char *rawData = "Hello world!";
char *secret_key = "SECRET_KEY";

unsigned char buf[16];
unsigned char key[32];
memset(buf,0,16);
memset(key,0,32);
memcpy( buf, rawData, 16);

/* Set the key */
memcpy( key, secret_key, 32);
aes_set_key( &ctx, key, 128);
/* Encrypt and save to file */
aes_encrypt( &ctx, buf, buf );
FILE *fp;
fp = fopen("SSL", "wb+");
fwrite(buf,1,16,fp);
fclose(fp);


Decrypt in Linux

aes_context ctx;
char *decryptData;
char *secret_key = "SECRET_KEY ";

unsigned char buf[16];
unsigned char key[32];
memset(buf,0,16);
memset(key,0,32);
/* Set the key */
memcpy( key, secret_key, 32);
aes_set_key( &ctx, key, 128);
/* Decrypt from file*/
FILE *fp;
fp = fopen("SSL", "rb+");
fread(buf, 1, 16, fp);
fclose(fp);

aes_decrypt( &ctx,buf,buf);

decryptData = (char*)buf;
cout<<decryptData<<endl;

--

All Comments

Hedy avatarHedy2013-05-20
aes_xxx 是你自己寫的函數嗎?
Emily avatarEmily2013-05-22
另外 secret_key 的長度根本不到 32,會根據系統的
Andy avatarAndy2013-05-24
memory layout 之後的內容不同,所以應該會壞掉
Edith avatarEdith2013-05-27
Memcpy 複製不該複製的資料了,超過了。