[問題] AES字串加解密(跨平台)

看板Linux作者 (rtt)時間12年前 (2013/05/15 12:40), 編輯推噓3(301)
留言4則, 2人參與, 最新討論串1/1
小弟利用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; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 210.200.169.82

05/15 14:10, , 1F
aes_xxx 是你自己寫的函數嗎?
05/15 14:10, 1F

05/15 14:11, , 2F
另外 secret_key 的長度根本不到 32,會根據系統的
05/15 14:11, 2F

05/15 14:13, , 3F
memory layout 之後的內容不同,所以應該會壞掉
05/15 14:13, 3F

05/15 21:31, , 4F
Memcpy 複製不該複製的資料了,超過了。
05/15 21:31, 4F
文章代碼(AID): #1Han77x1 (Linux)