使用.net自带的RSA,需要引用System.Security.Cryptography
测试环境.net4.6
static void Main(string[] args) { var RSA = new RSACryptoServiceProvider(); var publickKey = RSA.ToXmlString(false); var privateKey = RSA.ToXmlString(true); var publicKeyRSA = new RSACryptoServiceProvider(); publicKeyRSA.FromXmlString(publickKey); var privateKeyRSA = new RSACryptoServiceProvider(); privateKeyRSA.FromXmlString(privateKey); var encryptBytes = publicKeyRSA.Encrypt(Encoding.Default.GetBytes("hellow world"), false); string tmp = ""; for (int i = 0; i < encryptBytes.Length; i++) { tmp += encryptBytes[i] + ","; } tmp = tmp.TrimEnd(','); Console.WriteLine(tmp); var sign = privateKeyRSA.SignData(encryptBytes, "SHA1"); if (publicKeyRSA.VerifyData(encryptBytes, "SHA1", sign)) { var decryptBytes = privateKeyRSA.Decrypt(encryptBytes, false); var str = Encoding.Default.GetString(decryptBytes); Console.WriteLine(str); } else { Console.WriteLine("Sign Error!"); } Console.Read(); }
运行打印:
104,205,171,68,41,206,37,127,112,195,150,236,162,29,144,253,78,197,173,65,75,240 ,223,35,186,212,218,50,24,60,144,133,174,97,74,90,61,97,97,54,214,231,30,191,33, 18,102,103,230,79,231,100,171,129,0,165,33,147,22,69,83,67,105,212,92,118,112,16 5,200,11,91,241,56,161,218,11,62,179,83,167,141,27,2,188,169,102,222,133,37,191, 41,48,246,90,238,164,105,54,238,126,6,140,74,197,208,164,197,209,98,68,243,160,1 65,66,64,223,234,175,160,5,24,198,169,206,110,100,235,6 hellow world