Exemple #1
0
        /// <summary>
        /// SM4 ECB模式加密,由EncodeBySM4方法调用
        /// </summary>
        /// <param name="plainText"></param>
        /// <returns></returns>
        private String Encrypt_ECB(String plainText)
        {
            SM4_Context ctx = new SM4_Context();

            ctx.isPadding = true;
            ctx.mode      = SM4.SM4_ENCRYPT;
            byte[] keyBytes;
            if (HexString)
            {
                keyBytes = Hex.Decode(SecretKey);
            }
            else
            {
                keyBytes = Encoding.UTF8.GetBytes(SecretKey);
            }
            SM4 sm4 = new SM4();

            sm4.sm4_setkey_enc(ctx, keyBytes);
            byte[] encrypted = sm4.sm4_crypt_ecb(ctx, Encoding.UTF8.GetBytes(plainText));

            return(Convert.ToBase64String(encrypted));
        }
Exemple #2
0
        /// <summary>
        /// SM4 ECB模式解密 由DecodeBySM4调用
        /// </summary>
        /// <param name="cipherText"></param>
        /// <returns></returns>
        private String Decrypt_ECB(String cipherText)
        {
            SM4_Context ctx = new SM4_Context();

            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, Convert.FromBase64String(cipherText));
            return(Encoding.UTF8.GetString(decrypted));
        }