我正在將 Web 應(yīng)用程序從 PHP 遷移到基于 JS 的框架。該應(yīng)用程序使用mcrypt_encryptbase64 進行加密。我嘗試mcrypt在 Javascript 中使用該模塊,但沒有得到相同的結(jié)果。原來的 PHP 函數(shù)是這樣的function safe_b64encode($string) { $data = base64_encode($string); $data = str_replace(array('+', '/', '='), array('-', '_', ''), $data); return $data;}function encrypt($value) { $text = $value; $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB); $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND); $crypttext = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, ENCRYPTION_KEY, $text, MCRYPT_MODE_ECB, $iv); return trim(safe_b64encode($crypttext));}我的 JS 版本是這樣的const MCrypt = require('mcrypt').MCryptconst rijndael128Ecb = new MCrypt('rijndael-128', 'ecb')const iv = rijndael128Ecb.generateIv()rijndael128Ecb.validateKeySize(false)rijndael128Ecb.open(ENCRYPTION_KEY, iv)let cipherText = rijndael128Ecb.encrypt('sometext')cipherText = Buffer.concat([iv, cipherText]).toString('base64')cipherText = cipherText.replace('+','-').replace('/','_').replace('=','')
- 1 回答
- 0 關(guān)注
- 172 瀏覽
添加回答
舉報
0/150
提交
取消