微信小程序裏獲取用戶信息encryptedData的解碼方法的C#版本(2017.8.14網上搜了半天找到的,親測有用,上傳記錄下來)
一、先要經過wx.login方法取得一個session_key串
二、而後經過wx.getUserInfo方法取得一個iv串和encryptedData,這裏特別只有在wx.getUserInfo方法下的withCredentials: true時纔有encryptedData數據回傳回來
三、而後用下面的方法進行解碼
小程序
/// <summary> /// AES解密 /// </summary> /// <param name="inputdata">輸入的數據encryptedData</param> /// <param name="AesKey">key</param> /// <param name="AesIV">向量128</param> /// <returns name="result">解密後的字符串</returns> public static string AESDecrypt2(string inputdata, string AesKey, string AesIV) { try { AesIV = AesIV.Replace(" ", "+"); AesKey = AesKey.Replace(" ", "+"); inputdata = inputdata.Replace(" ", "+"); byte[] encryptedData = Convert.FromBase64String(inputdata); RijndaelManaged rijndaelCipher = new RijndaelManaged(); rijndaelCipher.Key = Convert.FromBase64String(AesKey); // Encoding.UTF8.GetBytes(AesKey); rijndaelCipher.IV = Convert.FromBase64String(AesIV);// Encoding.UTF8.GetBytes(AesIV); rijndaelCipher.Mode = CipherMode.CBC; rijndaelCipher.Padding = PaddingMode.PKCS7; ICryptoTransform transform = rijndaelCipher.CreateDecryptor(); byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length); string result = Encoding.UTF8.GetString(plainText); return result; } catch (Exception) { return null; } }