public static void TestSm2Enc() { string testStr = "hello world"; Console.WriteLine("原始数据 : " + testStr); byte[] sourceData = Encoding.ASCII.GetBytes(testStr); byte[] pubKey = HexStringToByteArray(PubKey); string encStr = SM2Utils.Encrypt(pubKey, sourceData); Console.WriteLine("加密后数据 : " + encStr); byte[] prik = HexStringToByteArray(PriKey); var data = Hex.Decode(Encoding.ASCII.GetBytes(encStr)); var decodedData = SM2Utils.Decrypt(prik, data); var decodedStr = Encoding.ASCII.GetString(decodedData); Console.WriteLine("解密后数据 : " + decodedStr); }
private void BtnSm2Test(object sender, RoutedEventArgs e) { //公钥 string publickey = ""; //私钥 string privatekey = ""; //生成公钥和私钥 SM2Utils.GenerateKeyPair(out publickey, out privatekey); System.Console.Out.WriteLine("加密明文: " + "000000"); System.Console.Out.WriteLine("publickey:" + publickey); //开始加密 string cipherText = SM2Utils.Encrypt(publickey, "000000"); System.Console.Out.WriteLine("密文: " + cipherText); System.Console.Out.WriteLine("privatekey:" + privatekey); //解密 string plainText = SM2Utils.Decrypt(privatekey, cipherText); System.Console.Out.WriteLine("明文: " + plainText); Console.ReadLine(); }