Exemple #1
0
        /// <summary>
        /// RSA私玥解密
        /// </summary>
        /// <param name="privateKey"></param>
        /// <param name="cipherText"></param>
        /// <returns></returns>

        public static string RsaDecrypt(string privateKey, string cipherText)
        {
            byte[] dataBytes = Convert.FromBase64String(cipherText); //对加密方法返回的byte[],用Convert.ToBase64String,普通的文字并不是 base 64 编码的,不能使用 FromBase64String 转换成 byte[]
            using (RSA privateKeyRsaProvider = RsaProvider.CreateRsaProviderFromPrivateKey(privateKey))
            {
                return(Encoding.UTF8.GetString(privateKeyRsaProvider.Decrypt(dataBytes, RSAEncryptionPadding.Pkcs1)));
            }
        }
Exemple #2
0
        /// <summary>
        /// 使用私钥签名
        /// </summary>
        /// <param name="data">原始数据</param>
        /// <returns></returns>
        public static string RsaSign(string privateKey, string data)
        {
            byte[] dataBytes = Encoding.UTF8.GetBytes(data);

            using (RSA privateKeyRsaProvider = RsaProvider.CreateRsaProviderFromPrivateKey(privateKey))
            {
                var signatureBytes = privateKeyRsaProvider.SignData(dataBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);

                return(Convert.ToBase64String(signatureBytes));
            }
        }