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); }
public static void tesdDecFile() { string filePath = @"D:\ProjectDemo\SM2Crypto\tmp\MA05M6KK9201810311620120201.enc"; FileStream fs = new FileStream(filePath, FileMode.Open); byte[] data = new byte[fs.Length]; fs.Seek(0, SeekOrigin.Begin); fs.Read(data, 0, (int)fs.Length); fs.Close(); byte[] prik = Encoding.ASCII.GetBytes(PRV_KEY); //var data = Hex.Decode(Encoding.ASCII.GetBytes(encStr)); var decodedData = SM2Utils.Decrypt(Hex.Decode(prik), data); string zipFilePath = @"D:\ProjectDemo\SM2Crypto\test\MA05M6KK9201810311620120201.zip"; FileStream zfs = new FileStream(zipFilePath, FileMode.Create); zfs.Write(decodedData, 0, decodedData.Length); zfs.Close(); }
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(); }