国密加密算法-SM2、SM3、SM4

国密加密

加密类型加密算法应用范围对应国际加密算法说明
非对称加密SM2身份认证、数字签名、密码交换、256位椭圆曲线RSA、RSA4096需要生成秘钥对(公钥和私钥
对称加密算法 SM1 128位数字加密、算法不公开、仅以IP核的形式存在于芯片中。智能IC卡、智能密码钥匙、加密卡、加密机 DES、3DES、AES(128)、AES192、AES256 AES是取代DES的算法
SM4 128位数据加密、相当于AES(128)
完整性运算 SM3 256位数据摘要计算,相当于SHA256 SHA1、SHA-256、SHA-384、SHA-512
  • 引用 KYSharp.SM

  • 示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KYSharp.SM;

namespace ConsoleApp6
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("************* SM2 *************");
            //公钥
            string publicKey = "";
            //私钥
            string privateKey = "";
            //生成公钥和私钥
            SM2Utils.GenerateKeyPair(out publicKey, out privateKey);

            Console.WriteLine("加密明文:" + "000000");
            Console.WriteLine("publicKey:"+publicKey);
            //开始加密
            string cliperText = SM2Utils.Encrypt(publicKey, "000000");
            Console.WriteLine("密文:" + cliperText);
            Console.WriteLine("privateKey:" + privateKey);
            //解密
            string plainText = SM2Utils.Decrypt(privateKey, cliperText);
            Console.WriteLine("明文:" + plainText);
            Console.WriteLine();
            Console.WriteLine("**************  SM3 ***************");
            SM3JM();
            Console.WriteLine("************ SM4 ***************");
            SM4JM();
            Console.ReadKey();

        }
        private static void SM3JM()
        {
            SM3 bo = new SM3();
            string s = "asdfasde";
            Console.WriteLine("明文:"+s);
            string str = bo.Encrypt(s);
            Console.WriteLine("密文:" + str);
            Console.WriteLine("长度:" + str.Length);
            Console.WriteLine("开始解密");
            string str1 = bo.Encrypt(s);
            Console.WriteLine(str1);
        }

        private static void SM4JM()
        {
     
            string plainText = "erefei中国人民god";
            Console.WriteLine("明文:"+plainText);
            SM4Utils sm4 = new SM4Utils();
            sm4.secretKey = "JeF8U9wHFOMfs2Y8";
            sm4.hexString = false;

            Console.WriteLine("ECB模式");
            string cliperText = sm4.Encrypt_ECB(plainText);
            Console.WriteLine("密文:"+cliperText);
            Console.WriteLine();

            string ks=sm4.Decrypt_ECB(cliperText);
            Console.WriteLine("解密:"+ks);
            Console.WriteLine();

            Console.WriteLine("CBC模式");
            string s = "UISwD9fW6cFh9SNS";
            sm4.iv = s;
            cliperText = sm4.Encrypt_CBC(plainText);
            Console.WriteLine("密文:"+cliperText);
            Console.WriteLine();
            string mw = sm4.Decrypt_CBC(cliperText);
            Console.WriteLine("解密:"+mw);
            Console.WriteLine();

        }

    }
}

posted @ 2023-07-17 17:48  丹心石  阅读(4466)  评论(0编辑  收藏  举报