st->op->op1->op2->op3->op4->e
```
/// sessionKey 是用户登陆code
/// text 加密内容
/// iv 加密配置
public IActionResult DecryptWXString(string sessionKey, string text, string iv)
{
string openid = "";
string url = @"https://api.weixin.qq.com/sns/jscode2session?appid=填写自己的&secret=填写自己的&js_code="+ sessionKey + "&grant_type=authorization_code";
// 简单的http 请求自己写
string str = HttpRequestHelp.RequestUrl.GetUrlAsync(url).Result;
JObject json1 = (JObject)JsonConvert.DeserializeObject(str);
if (json1["session_key"] == null || json1["openid"] == null)
{
ro.code = "3";
ro.msg = "获取session_key失败!";
}
else
{
sessionKey = json1["session_key"].ToString();
openid = json1["openid"].ToString();
}
if (!string.IsNullOrEmpty(sessionKey) && !string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(iv))
{
// 传输的数据有变化,必须处理,不然FromBase64String 报错
text = text.Replace("%", "").Replace(",", "").Replace(" ", "+");
sessionKey = sessionKey.Replace("%", "").Replace(",", "").Replace(" ", "+");
iv = iv.Replace("%", "").Replace(",", "").Replace(" ", "+");
string result = "";
using (Aes aesAlg = Aes.Create())
{
try
{
aesAlg.Key = Convert.FromBase64String(sessionKey);
aesAlg.IV = Convert.FromBase64String(iv);
aesAlg.Mode = CipherMode.CBC;
aesAlg.Padding = PaddingMode.PKCS7;
//aesAlg.BlockSize = 128; //这里记得千万不安装官网的说明加上,不然解析出乱码
ICryptoTransform decryptor = aesAlg.CreateDecryptor();
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(text)))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
result = srDecrypt.ReadToEnd();
JObject json3 = (JObject)JsonConvert.DeserializeObject(result);
if (json3["purePhoneNumber"] != null)
{
ro.code = "0";
ro.data = new
{
phone = json3["purePhoneNumber"].ToString(),
openid = openid
};
return Json(ro);
}
}
}
}
}
catch(Exception ex)
{
ro.code = "4";
ro.msg = "系统异常!";
if (aesAlg != null)
aesAlg.Clear();
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
}
}
return Json(ro);
}
微信小程序后台获取用户手机,openid(C#) --markdown
原文:https://www.cnblogs.com/duchyaiai/p/11573592.html