/// <summary> /// 解密 /// </summary> /// <param name="resData">byte[]类型的加密字符串</param> /// <param name="privateKey">xml格式的私钥</param> /// <param name="isOaep">是否使用oaep填充方式和,ture使用oaep,false使用Pkcs1方式</param> /// <returns>明文</returns> public static byte[] Decrypt(byte[] resData, string xmlPrivateKey, bool isOaep) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); RSACryptoExtensions.FromXmlString(rsa, xmlPrivateKey); //rsa.FromXmlString(xmlPrivateKey); return(rsa.Decrypt(resData, isOaep)); }
/// <summary> /// 加密 /// </summary> /// <param name="resData">byte[]类型的需要加密的字符串</param> /// <param name="publicKey">xml格式的公钥</param> /// <param name="isOaep">是否使用oaep填充方式和,ture使用oaep,false使用Pkcs1方式</param> /// <returns>加密后的数据</returns> public static string Encrypt(byte[] resData, string xmlPublicKey, bool isOaep) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); RSACryptoExtensions.FromXmlString(rsa, xmlPublicKey); //rsa.FromXmlString(xmlPublicKey); return(Convert.ToBase64String(rsa.Encrypt(resData, isOaep))); }
/// <summary> /// 签名 /// </summary> /// <param name="content">byte[]类型的待签名字符串</param> /// <param name="xml格式的privateKey">xml格式的私钥</param> /// <param name="signAlgorithm">签名算法,SHA256/SHA1</param> /// <returns>签名后字符串</returns> public static byte[] Sign(byte[] content, string xmlPrivateKey, string signAlgorithm) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); RSACryptoExtensions.FromXmlString(rsa, xmlPrivateKey); //rsa.FromXmlString(xmlPrivateKey); if (Constants.CMBLIFE_SIGN_ALGORITHM_SHA256.Equals(signAlgorithm)) { return(rsa.SignData(content, new SHA256CryptoServiceProvider())); } else if (Constants.CMBLIFE_SIGN_ALGORITHM_SHA1.Equals(signAlgorithm)) { return(rsa.SignData(content, new SHA1CryptoServiceProvider())); } else { throw new ArgumentException("签名算法不合法!"); } }