用的组件NETCore.Encrypt
static void Main(string[] args) { string RsaPublicKey = @"-----BEGIN PUBLIC KEY----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDYHSAbIAbjctQt8EpNiNzO52QN P9uIAlNqry8aHPkrzyxDzEakkJ9d3Yd+ejdxxzKzTc93pdj2KpdVqADiqnnm9K1q K3oTs3uX7aXzTo0Jc/lqb1qOtl7RLlo3ScrqAU+yeFvvlPg+wqI+tyDeqD+tT974 3XURVD5+FiVleD5ooQIDAQAB -----END PUBLIC KEY-----"; string RsaPrivateKey = @"-----BEGIN RSA PRIVATE KEY----- MIICXAIBAAKBgQDYHSAbIAbjctQt8EpNiNzO52QNP9uIAlNqry8aHPkrzyxDzEak kJ9d3Yd+ejdxxzKzTc93pdj2KpdVqADiqnnm9K1qK3oTs3uX7aXzTo0Jc/lqb1qO tl7RLlo3ScrqAU+yeFvvlPg+wqI+tyDeqD+tT9743XURVD5+FiVleD5ooQIDAQAB AoGAZgb6DVRFbEPd4AI9UfV3KUcceRTYs5FFiGxENHPRPtmD8E4UW8TbOBlJynAw lMqSKQxnEfl+QFlDMPEr2dbZzUy4WXd/4x0pliLwHeq3W5dARTAqRU3RNuZ4CH6f 1GiOrWeJQygZUejeQxmQQOW42cuHMAy6pV+pn5o+tyozCTECQQD8d9SURr0Z6g2w VLRRQjkPiSOu2D4gBmfvey+l789Ly9e4BjPmLo1oXYLcbi4/0nVd0Hz+961KejJl 54fvFZ5rAkEA2yMZN0a2CnzC5WUYxdhqAPw8r+Yv9B6V/pLquZjz6r3SsMpokRA8 zWi+LCdEXX2J/PhhB1dpcmR851xmz81AIwJAYxPHqfTkyyGuA5AKP4VkjTM9fKVC JCE/cpz+QvmMeWtimcZdYazNM7LW4xieL8+w3YMvOoQKTpsL4i/tiacLRwJBAM3o zA/prLJsCwJcHYwCT5D/ctLh29tqRg0A5fhhpYjEt04R03HDrS5iJpf8ibvDc761 cQ7h7x5jGA6Z/61xMrsCQD3m/iCG0JDchFMeDpMNK7DtscOdtBYM9iY17XrYubdJ J/W64+fAq3abjIZC4+BRf8bARacSYUTLL2a3VZHJr40= -----END RSA PRIVATE KEY-----"; //PKCS1 var pkcs1KeyTuple = EncryptProvider.RSAToPem(false); var publicPem = pkcs1KeyTuple.publicPem; var privatePem = pkcs1KeyTuple.privatePem; var rawStr = "xxx"; var enctypedStr = EncryptProvider.RSAEncryptWithPem(RsaPublicKey, rawStr); var decryptedStr = EncryptProvider.RSADecryptWithPem(RsaPrivateKey, enctypedStr); Console.WriteLine("Hex:" + decryptedStr); //在网站http://tool.chacuo.net/cryptrsapubkey里用公钥进行加密后得到str var str = "yP7IZgwCo4R++c+ouj6jbMbuxK5cnO6t9Jlu7ZvVNRzMNm/6qw5CuPEQw5dHn7lUZFZ2BHZttBSj6Wwr/cvXUH08vdPSXAhNJP/rQmg6aEwM2HcyQSJ/mpajRfj2KPBQwFuJuz1xU2PFZ2YQhaHLSJaUCL/cV9k68gEUCEdx0xk="; var bytes = Convert.FromBase64String(str); str = string.Join("", bytes.Select(x => x.ToString("X2")).ToArray()); var decryptedStr1 = EncryptProvider.RSADecryptWithPem(RsaPrivateKey, str); Console.WriteLine("Base64:" + decryptedStr1); Console.ReadKey(); }
输出