2 回答

TA貢獻1848條經驗 獲得超10個贊
在PHP代碼中,密鑰不是十六進制解碼的,而是一個大小為32字節的二進制字符串,因此對于AES-128來說太大了。
PHP/OpenSSL 通過僅考慮前 16 個字節來隱式截斷密鑰。
在 Go 中,只需使用key := []byte("bc7316929fe1545b")
在節點JS中:獲取 PHP 結果。const key = Buffer.from("bc7316929fe1545b", "utf8")
相反,在 PHP 代碼中,密鑰也可以使用 進行十六進制解碼。hex2bin()

TA貢獻1826條經驗 獲得超6個贊
這里的 c/c++ 版本,但輸出與 golang/nodejs 還不一樣。
#include <openssl/aes.h>
#include <stdio.h>
#include <string.h>
int main() {
AES_KEY aes_key;
unsigned char key[AES_BLOCK_SIZE] = {0xbc, 0x73, 0x16, 0x92, 0x9f, 0xe1, 0x54, 0x5b, 0xf0, 0xb9, 0x8d, 0x11, 0x4e, 0xe3, 0xec, 0xb8 };
unsigned char iv[AES_BLOCK_SIZE] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
unsigned char ecount[AES_BLOCK_SIZE]; memset( ecount, 0, 16 );
unsigned int num = 0 ;
const char *x = "aaa";
unsigned char out[16];
AES_set_encrypt_key(key, 128, &aes_key);
AES_ctr128_encrypt( (const unsigned char *) x, out, AES_BLOCK_SIZE, &aes_key, iv, ecount, &num);
for (int k = 0; k < 3; k++) printf("%02x", out[k]); // c9aa18
printf( "\n");
return 0;
}
// g++ -o aes-simple aes-simple.cpp -I/usr/local/ssl/include -L/usr/local/ssl/lib -g -lcrypto
- 2 回答
- 0 關注
- 100 瀏覽
添加回答
舉報