Exemple #1
0
        /// <summary>
        /// 解密ECB模式
        /// </summary>
        /// <param name="secretKey">密钥</param>
        /// <param name="hexString">明文是否是十六进制</param>
        /// <param name="cipherText">密文</param>
        /// <returns>返回明文</returns>
        public String Decrypt_ECB(String secretKey, bool hexString, String cipherText)
        {
            Sm4Context ctx = new Sm4Context();

            ctx.isPadding = true;
            ctx.mode      = Sm4.SM4_DECRYPT;

            byte[] keyBytes;
            if (hexString)
            {
                keyBytes = Hex.Decode(secretKey);
            }
            else
            {
                keyBytes = Encoding.UTF8.GetBytes(secretKey);
            }

            Sm4 sm4 = new Sm4();

            sm4.sm4_setkey_dec(ctx, keyBytes);
            byte[] decrypted = sm4.sm4_crypt_ecb(ctx, Hex.Decode(cipherText));
            return(Encoding.UTF8.GetString(decrypted));
        }
Exemple #2
0
        /// <summary>
        /// 解密CBC模式
        /// </summary>
        /// <param name="secretKey">密钥</param>
        /// <param name="hexString">明文是否是十六进制</param>
        /// <param name="iv"></param>
        /// <param name="cipherText">密文</param>
        /// <returns>返回明文</returns>
        public String Decrypt_CBC(String secretKey, bool hexString, string iv, String cipherText)
        {
            Sm4Context ctx = new Sm4Context();

            ctx.isPadding = true;
            ctx.mode      = Sm4.SM4_DECRYPT;
            byte[] keyBytes;
            byte[] ivBytes;
            if (hexString)
            {
                keyBytes = Hex.Decode(secretKey);
                ivBytes  = Hex.Decode(iv);
            }
            else
            {
                keyBytes = Encoding.Default.GetBytes(secretKey);
                ivBytes  = Encoding.Default.GetBytes(iv);
            }
            Sm4 sm4 = new Sm4();

            sm4.sm4_setkey_dec(ctx, keyBytes);
            byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Hex.Decode(cipherText));
            return(Encoding.Default.GetString(decrypted));
        }