예제 #1
0
파일: MiniDecrypt.cs 프로젝트: litesz/WFw
        /// <summary>
        /// 解码用户信息
        /// </summary>
        /// <param name="key"></param>
        /// <param name="encryptedData"></param>
        /// <param name="iv"></param>
        /// <returns></returns>
        public static T DecryptWxMsg <T>(this IEncryptedDataInfo encryptedData, string sessionKey) where T : class
        {
            if (string.IsNullOrWhiteSpace(encryptedData.EncryptedData))
            {
                throw new WFwException(Results.OperationResultType.ParamIsEmpty, "解码内容", "");
            }

            if (string.IsNullOrWhiteSpace(sessionKey))
            {
                throw new WFwException(Results.OperationResultType.ParamIsEmpty, "解码密钥", "");
            }

            if (string.IsNullOrWhiteSpace(encryptedData.Iv))
            {
                throw new WFwException(Results.OperationResultType.ParamIsEmpty, "初始向量", "");
            }

            try
            {
                using (Aes aesAlg = Aes.Create())
                {
                    aesAlg.Mode      = CipherMode.CBC;
                    aesAlg.BlockSize = 128;
                    aesAlg.Padding   = PaddingMode.PKCS7;
                    aesAlg.Key       = Convert.FromBase64String(sessionKey);
                    aesAlg.IV        = Convert.FromBase64String(encryptedData.Iv);
                    ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
                    using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(encryptedData.EncryptedData)))
                    {
                        using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                        {
                            using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                            {
                                return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(srDecrypt.ReadToEnd()));
                            }
                        }
                    }
                }
            }
            catch
            {
                //throw new WFwException(Results.OperationResultType.IsErr, "解码失败", $"Data={encryptedData.EncryptedData};Iv={encryptedData.Iv};sessionKey:{sessionKey}");
                throw new WFwException(Results.OperationResultType.IsErr, "解码失败", nameof(encryptedData.EncryptedData), encryptedData.EncryptedData, nameof(encryptedData.Iv), encryptedData.Iv, nameof(sessionKey), sessionKey);
            }
        }
예제 #2
0
 /// <summary>
 /// 解码获得用户信息
 /// </summary>
 /// <param name="encryptedData"></param>
 /// <param name="sessionKey"></param>
 /// <returns></returns>
 public static UserInfo DecryptUserInfo(IEncryptedDataInfo encryptedData, string sessionKey)
 {
     return(encryptedData.DecryptWxMsg <UserInfo>(sessionKey));
 }
예제 #3
0
 /// <summary>
 /// 解码获得手机号
 /// </summary>
 /// <param name="encryptedData"></param>
 /// <param name="sessionKey"></param>
 /// <returns></returns>
 public static PhoneInfo DecryptPhone(IEncryptedDataInfo encryptedData, string sessionKey)
 {
     return(encryptedData.DecryptWxMsg <PhoneInfo>(sessionKey));
 }