Exemple #1
0
        /// <summary>
        /// 文加密
        /// 使用场景:1.商户请求掌上生活
        ///           2.商户响应掌上生活
        /// </summary>
        /// <param name="encryptBody">需要加密的字符串</param>
        /// <param name="xmlPublicKey">加密使用的Key,为掌上生活RSA公钥</param>
        /// <returns>密文</returns>
        public static string Encrypt(string encryptBody, string xmlPublicKey)
        {
            if (string.IsNullOrEmpty(encryptBody))
            {
                throw new ArgumentException("报文不能为空!");
            }

            if (string.IsNullOrEmpty(xmlPublicKey))
            {
                throw new ArgumentException("公钥不能为空!");
            }

            string aesKey           = AesUtils.GenAesKey();
            string aesEncryptedBody = AesUtils.Encrypt(encryptBody, aesKey);
            // TODO 需要先base64Decode

            string encryptedAesKey = RsaUtils.Encrypt(Convert.FromBase64String(aesKey), xmlPublicKey);

            return(encryptedAesKey + "|" + aesEncryptedBody);
        }
Exemple #2
0
 /// <summary>
 /// 验签
 /// </summary>
 /// <param name="verifyBody">待验签的数据</param>
 /// <param name="sign">签名</param>
 /// <param name="xmlPublicKey">验签所使用的Key,为掌上生活公钥</param>
 /// <param name="signAlgorithm">签名算法(SHA1WithRSA 或 SHA256WithRSA)</param>
 /// <returns>true为验签成功,false为验签失败</returns>
 public static bool Verify(string verifyBody, string sign, string xmlPublicKey, string signAlgorithm)
 {
     if (string.IsNullOrEmpty(verifyBody))
     {
         throw new ArgumentException("验签数据不能为空!");
     }
     if (string.IsNullOrEmpty(sign))
     {
         throw new ArgumentException("签名不能为空!");
     }
     if (string.IsNullOrEmpty(xmlPublicKey))
     {
         throw new ArgumentException("公钥不能为空!");
     }
     if (string.IsNullOrEmpty(signAlgorithm))
     {
         throw new ArgumentException("验签算法不能为空!");
     }
     return(RsaUtils.Verify(verifyBody, sign, xmlPublicKey, signAlgorithm));
 }