Пример #1
0
        public String Decrypt_CBC(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));
        }
Пример #2
0
        public byte[] decryptECB(String key, Boolean isPadding, byte[] expectedBytes)
        {
            SM4Context ctx = new SM4Context();

            ctx.isPadding = isPadding;
            ctx.mode      = SM4.SM4_DECRYPT;
            secretKey     = key;
            byte[] keyBytes;
            if (hexString)
            {
                keyBytes = Hex.Decode(secretKey);
            }
            else
            {
                keyBytes = Encoding.Default.GetBytes(secretKey);
            }

            SM4 sm4 = new SM4();

            sm4.sm4_setkey_dec(ctx, keyBytes);
            byte[] decrypted = sm4.sm4_crypt_ecb(ctx, expectedBytes);
            return(decrypted);
        }