SM2&SM3&SM4 C#实现

文章在整理过程中,着急需要源码的评论区留邮箱,看到后发送。。。
public static String SM4Encrypt(String secretKey, String msg)
        {
            byte[] mSecretKey = hexToByte(secretKey);
            List<byte> Answer = new List<byte>();
            using (SM4 sm4 = new SM4(CipherDirection.Encryption, CipherMode.ECB, PaddingMode.PKCS7))
            {
                sm4.Initialize(mSecretKey);

                byte[] Cipher = Encoding.Default.GetBytes(msg);
                if (sm4.TransformFinalBlock(Cipher, 0, Cipher.Length, Answer))
                {
                    //System.Console.WriteLine(Utils.ToString(Answer.ToArray()));
                    return Utils.ToString(Answer.ToArray());
                }
                else
                {
                    return "";
                }
            }

        }

        public static String SM4Decrypt(String secretKey, String hexMsg)
        {
            byte[] mSecretKey = hexToByte(secretKey);
            List<byte> Answer = new List<byte>();
            using (SM4 sm4 = new SM4(CipherDirection.Decryption, CipherMode.ECB, PaddingMode.PKCS7))
            {
                sm4.Initialize(mSecretKey);

                byte[] Cipher = hexToByte(hexMsg);
                if (sm4.TransformFinalBlock(Cipher, 0, Cipher.Length, Answer))
                {
                    //System.Console.WriteLine(Encoding.Default.GetString(Answer.ToArray()));
                    return Encoding.Default.GetString(Answer.ToArray());
                }
                else
                {
                    return "";
                }
            }
        }

你可能感兴趣的:(C#,SM,sm3,sm4,sm2)