Exemplo n.º 1
0
        /// <summary>
        /// 报文解密
        /// 使用场景:1.掌上生活请求商户
        ///           2.掌上生活响应商户
        /// </summary>
        /// <param name="decryptBody">加密后内容(需要解密的字符串)</param>
        /// <param name="xmlPrivateKey">解密使用的Key,为商户RSA私钥</param>
        /// <returns>明文</returns>
        public static string Decrypt(string decryptBody, string xmlPrivateKey)
        {
            string[] data = decryptBody.Split('|');
            if (2 != data.Length)
            {
                throw new ArgumentException("加密报文格式错误!");
            }
            byte[] aesKey = RsaUtils.Decrypt(data[0], xmlPrivateKey);

            //需要先base64Encode
            return(AesUtils.Decrypt(data[1], Convert.ToBase64String(aesKey)));
        }
Exemplo n.º 2
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);
        }