VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • C#教程之C# SM加密(2)



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Org.BouncyCastle.Utilities.Encoders;
 
namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("***************SM2*************************");
 
            SM2();
 
            Console.WriteLine("***************SM4*************************");
 
            SM4();
            Console.ReadLine();
        }
 
 
        public static void SM2()
        {
            string plainText = "ababcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcc";
 
            //随机生成私公钥
            //SM2Utils.GenerateKeyPair();
 
            byte[] sourceData = Encoding.Default.GetBytes(plainText);
 
            //下面的秘钥可以使用generateKeyPair()生成的秘钥内容  
            // 国密规范正式私钥  
            String prik = "3690655E33D5EA3D9A4AE1A1ADD766FDEA045CDEAA43A9206FB8C430CEFE0D94";
            // 国密规范正式公钥  
            String pubk = "04F6E0C3345AE42B51E06BF50B98834988D54EBC7460FE135A48171BC0629EAE205EEDE253A530608178A98F1E19BB737302813BA39ED3FA3C51639D7A20C7391A";
 
            Console.WriteLine("加密: ");
            String cipherText = SM2Utils.Encrypt(Hex.Decode(pubk), sourceData);
            Console.WriteLine(cipherText);
            Console.WriteLine("解密: ");
            plainText = Encoding.Default.GetString(SM2Utils.Decrypt(Hex.Decode(prik), Hex.Decode(cipherText)));
            Console.WriteLine(plainText);
 
        }
 
        public static void SM4()
        {
            string plainText = "ababcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcc";
            Console.WriteLine("ECB模式");
            String cipherText = SM4Utils.Encrypt_ECB(plainText);
            Console.WriteLine("密文: " + cipherText);
            Console.WriteLine("");
 
            plainText = SM4Utils.Decrypt_ECB(cipherText);
            Console.WriteLine("明文: " + plainText);
            Console.WriteLine("");
 
            Console.WriteLine("CBC模式");
            cipherText = SM4Utils.Encrypt_CBC(plainText);
            Console.WriteLine("密文: " + cipherText);
            Console.WriteLine("");
 
            plainText = SM4Utils.Decrypt_CBC(cipherText);
            Console.WriteLine("明文: " + plainText);
 
        }
    }
}


其中最新版dll有改动之处
引用:https://blog.csdn.net/ererfei/article/details/50999820


相关教程