首页 > 上网技巧 > 电脑小技巧 > C#实现微信AES-128-CBC加密数据的解密

C#实现微信AES-128-CBC加密数据的解密

时间:2018-04-19 09:09 作者:QQ地带 我要评论

1.微信小程序登录获取用户的openid信息
 
需要使用AES-128-CBC解密处理
 
 
/// <summary>  
/// ASE加解密  
/// </summary>  
public class AESHelper  
{  
    /// <summary>  
    /// 秘钥  
    /// </summary>  
    public static string AesKey;  
    /// <summary>  
    /// 16位初始向量  
    /// </summary>  
    public static string AesIV;  
    /// <summary>  
    /// AES解密  
    /// </summary>  
    /// <param name="text"></param>  
    /// <param name="password"></param>  
    /// <param name="iv"></param>  
    /// <returns></returns>  
    public static string AESDecrypt(string text)  
    {  
        try  
        {  
            //16进制数据转换成byte  
            byte[] encryptedData = Convert.FromBase64String(text);  // strToToHexByte(text);  
            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.Default.GetString(plainText);  
            return result;  
        }  
        catch (Exception ex)  
        {  
            throw ex;  
        }  
    }  
  
}  
测试代码:
 
[csharp] view plain copy
AESHelper.AesIV = "r7BXXKkLb8qrSNn05n0qiA==";  
AESHelper.AesKey = "tiihtNczf5v6AKRyjwEUhQ==";  
string text =  
    "CiyLU1Aw2KjvrjMdj8YKliAjtP4gsMZM" +  
    "QmRzooG2xrDcvSnxIMXFufNstNGTyaGS" +  
    "9uT5geRa0W4oTOb1WT7fJlAC+oNPdbB+" +  
    "3hVbJSRgv+4lGOETKUQz6OYStslQ142d" +  
    "NCuabNPGBzlooOmB231qMM85d2/fV6Ch" +  
    "evvXvQP8Hkue1poOFtnEtpyxVLW1zAo6" +  
    "/1Xx1COxFvrc2d7UL/lmHInNlxuacJXw" +  
    "u0fjpXfz/YqYzBIBzD6WUfTIF9GRHpOn" +  
    "/Hz7saL8xz+W//FRAUid1OksQaQx4CMs" +  
    "8LOddcQhULW4ucetDf96JcR3g0gfRK4P" +  
    "C7E/r7Z6xNrXd2UIeorGj5Ef7b1pJAYB" +  
    "6Y5anaHqZ9J6nKEBvB4DnNLIVWSgARns" +  
    "/8wR2SiRS7MNACwTyrGvt9ts8p12PKFd" +  
    "lqYTopNHR1Vf7XjfhQlVsAJdNiKdYmYV" +  
    "oKlaRv85IfVunYzO0IKXsyl7JCUjCpoG" +  
    "20f0a04COwfneQAGGwd5oa+T8yO5hzuy" +  
    "Db/XcxxmK01EpqOyuxINew==";  
string s = AESHelper.AESDecrypt(text);  
Console.WriteLine(s);  

标签: 微信 C#
顶一下
(0)
0%
踩一下
(0)
0%

Google提供的广告