애플릿에서 사용자 휴대폰 번호 서버측 코드 가져오기 (C#)
2639 단어 애플릿
using System;
using System.Net;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using LitJson;
public class AverageHandler : IHttpHandler
{
public bool IsReusable
{ get { return true; } }
public void ProcessRequest(HttpContext ctx)
{
ctx.Response.ContentType = "application/json";
HttpRequest Request = ctx.Request;
string text = Request["encryptedData"];
string IV = Request["iv"];
// appid appsecret
string appid = " ";
string secret = " ";
string code = Request["code"];//
Stream s_re = WebRequest.Create("https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code").GetResponse().GetResponseStream();
StreamReader sr = new StreamReader(s_re, Encoding.UTF8);
string strLine = sr.ReadToEnd();
sr.Close();
try
{
JsonData jo = JsonMapper.ToObject(strLine);
string Key = jo["session_key"].ToString();
//string weixinID = jo["openid"].ToString();
string str= AES_decrypt(text,Key,IV);
ctx.Response.Write(str);
}
catch (Exception ex)
{
//return "";
}
}
public string AES_decrypt(string encryptedDataStr, string key, string iv)
{
RijndaelManaged rijalg = new RijndaelManaged();
//-----------------
// cipher AES-128-CBC
rijalg.KeySize = 128;
rijalg.Padding = PaddingMode.PKCS7;
rijalg.Mode = CipherMode.CBC;
rijalg.Key = Convert.FromBase64String(key);
rijalg.IV = Convert.FromBase64String(iv);
byte[] encryptedData= Convert.FromBase64String(encryptedDataStr);
//
ICryptoTransform decryptor = rijalg.CreateDecryptor(rijalg.Key, rijalg.IV);
string result;
using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
result = srDecrypt.ReadToEnd();
}
}
}
return result;
}
}