微信小程序解密encryptedData--获取手机号码

  • binGe博客
  • 开发笔记
  • 2024/1/25 17:10:55
  • 人已阅读
简介
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DeYi.NuGet.Other
{
    public class WxHelper
    {
        /// <summary>
        /// 微信小程序解密encryptedData
        /// </summary>
        /// <param name="encryptedData"></param>
        /// <param name="sessionKey"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static string Decrypt(string encryptedData, string sessionKey, string iv)
        {
            try
            {
                //创建解密器生成工具实例
                var aes = Aes.Create();
                //设置解密器参数
                aes.Mode = CipherMode.CBC;
                aes.BlockSize = 128;
                aes.Padding = PaddingMode.PKCS7;
                //格式化待处理字符串
                byte[] byte_encryptedData = Convert.FromBase64String(encryptedData);
                byte[] byte_iv = Convert.FromBase64String(iv);
                byte[] byte_sessionKey = Convert.FromBase64String(sessionKey);

                aes.IV = byte_iv;
                aes.Key = byte_sessionKey;
                //根据设置好的数据生成解密器实例
                ICryptoTransform transform = aes.CreateDecryptor();

                //解密
                byte[] final = transform.TransformFinalBlock(byte_encryptedData, 0, byte_encryptedData.Length);
                //生成结果
                string result = Encoding.UTF8.GetString(final);
                return result;
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLog(ex.Message, "DecryptError");
            }
            return string.Empty;
        }
    }
}

文章评论

评论
  • 消灭零回复
Top