Ejemplo n.º 1
0
        public String DecryptCBC4JS(String cipherText)
        {
            var ctx = new SM4ContextJS();

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

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

            var sm4 = new SM4CryptoServiceProviderJS();

            sm4.sm4_setkey_dec(ctx, keyBytes);
            byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Hex.Decode(cipherText));
            return(Encoding.UTF8.GetString(decrypted));
        }
Ejemplo n.º 2
0
        public String EncryptCBC4JS(String plainText)
        {
            var ctx = new SM4ContextJS();

            ctx.isPadding = true;
            ctx.mode      = SM4CryptoServiceProviderJS.SM4_ENCRYPT;

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

            SM4CryptoServiceProviderJS sm4 = new SM4CryptoServiceProviderJS();

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

            String cipherText = Encoding.UTF8.GetString(Hex.Encode(encrypted));

            return(cipherText);
        }