微信小程序里获取用户信息encryptedData的解码方法的C#版本(2017.8.14网上搜了半天找到的,亲测有用,上传记录下来)
1、先要通过wx.login方法取得一个session_key串
2、然后通过wx.getUserInfo方法取得一个iv串和encryptedData,这里特别只有在wx.getUserInfo方法下的withCredentials: true时才有encryptedData数据回传回来
3、然后用下面的方法进行解码
/// <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;
}
}