/// <summary> /// RSA字符串加密 /// </summary> /// <param name="source">源字符串 明文</param> /// <param name="key">密匙</param> /// <returns>加密遇到错误将会返回原字符串</returns> public static string RsaEncrypt(string source, string keyFilePath) { byte[] en = Encoding.UTF8.GetBytes(source); RSAParameters param = PemConverter.GetPemPublicKey(keyFilePath); RSACryptoServiceProvider rsasp = new RSACryptoServiceProvider(); rsasp.ImportParameters(param); byte[] encryptedData = rsasp.Encrypt(en, false); return(Convert.ToBase64String(encryptedData)); }
/// <summary> /// RSA字符串解密 /// </summary> /// <param name="encryptString">密文</param> /// <param name="key">密钥</param> /// <returns>遇到解密失败将会返回原字符串</returns> public static string RsaDecrypt(string encryptString, string keyFilePath) { byte[] de = Convert.FromBase64String(encryptString); RSAParameters param = PemConverter.GetPemPrivateKey(keyFilePath); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); rsa.ImportParameters(param); byte[] decryptedData = rsa.Decrypt(de, false); return(Encoding.UTF8.GetString(decryptedData)); }