各位大大好,目前在研究AES256加密上遇到問題
需求是在網頁的Javascript進行資料加密
傳給下一個網頁,由後台(VB.Net進行解密)
為了達成上面的需求,
先將Javascript和VB.Net各自加密同一個字串(Hello Word!!)
但結果不相同,不曉得是什麼問題,請大大們協助
(不太會排版,請大大們海涵)
Javascript
var mKey = '1234567812345678', mIV = '1234567812345678';
function Encrypt() {
var word = "Hello World!!";
var key = CryptoJS.enc.Utf8.parse(mKey);
var iv = CryptoJS.enc.Utf8.parse(mIV);
var srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });
return encrypted.ciphertext.toString().toUpperCase();
}
結果是 : AF938836C04E84558A4963C491E42B72
VB.Net
Private mSecretKey As String = "1234567812345678"
Private mIVKey As String = "1234567812345678"
Public Function EncryptAES256(ByVal source As String) As String
Dim _source As String="Hello World!!"
Dim _aes = New RijndaelManaged()
_aes.Key = Encoding.UTF8.GetBytes(mSecretKey)
_aes.IV = Encoding.UTF8.GetBytes(mIVKey)
_aes.Mode = CipherMode.CBC
_aes.Padding = PaddingMode.PKCS7
Dim _trans As ICryptoTransform = _aes.CreateEncryptor()
Dim _srcBytes As Byte() = Encoding.UTF8.GetBytes(_source)
Dim _outputData As Byte() = _trans.TransformFinalBlock(_srcBytes, 0, _srcBytes.Length)
Return Convert.ToBase64String(_outputData)
End Function
結果是 : r5OINsBOhFWKSWPEkeQrcg==
如果以VB.Net為主的話,Javascript要如何修正呢?
VB端用:
BitConverter.ToString(_outputData).Replace("-", "");//轉成十六進位字串
js端用:
encrypted.toString();//輸出base64格式字串
看你要用那個。不過要注意樓上大大問你的問題,因為在瀏覽器上,你的密碼大家都看得到。