Exemple #1
0
 /// <summary>
 /// RSA公玥加密
 /// </summary>
 /// <param name="publicKey"></param>
 /// <param name="text"></param>
 /// <returns></returns>
 public static string RsaEncrypt(string publicKey, string text)
 {
     byte[] dataBytes = Encoding.UTF8.GetBytes(text); //对普通的文字操作,用Encoding.UTF8.GetBytes()
     using (RSA publicKeyRsaProvider = RsaProvider.CreateRsaProviderFromPublicKey(publicKey))
     {
         return(Convert.ToBase64String(publicKeyRsaProvider.Encrypt(dataBytes, RSAEncryptionPadding.Pkcs1)));
     }
 }
Exemple #2
0
        /// <summary>
        /// 使用公钥验证签名
        /// </summary>
        /// <param name="data">原始数据</param>
        /// <param name="sign">签名</param>
        /// <returns></returns>
        public static bool RsaVerify(string publicKey, string data, string sign)
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(data);
            byte[] signBytes = Convert.FromBase64String(sign);

            using (RSA publicKeyRsaProvider = RsaProvider.CreateRsaProviderFromPublicKey(publicKey))
            {
                var verify = publicKeyRsaProvider.VerifyData(dataBytes, signBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

                return(verify);
            }
        }