public String Encrypt_CBC(String plainText) { SM4_Context ctx = new SM4_Context(); ctx.isPadding = true; ctx.mode = SM4.SM4_ENCRYPT; byte[] keyBytes; byte[] ivBytes; if (hexString) { keyBytes = Hex.Decode(secretKey); ivBytes = Hex.Decode(iv); } else { keyBytes = Encoding.ASCII.GetBytes(secretKey); ivBytes = Encoding.ASCII.GetBytes(iv); } SM4 sm4 = new SM4(); sm4.sm4_setkey_enc(ctx, keyBytes); byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Encoding.ASCII.GetBytes(plainText)); String cipherText = Encoding.ASCII.GetString(Hex.Encode(encrypted)); return(cipherText); }
public byte[] Encrypt_ECB(byte[] plainBytes, byte[] keyBytes) { SM4_Context ctx = new SM4_Context(); ctx.isPadding = false; ctx.mode = SM4.SM4_ENCRYPT; SM4 sm4 = new SM4(); sm4.sm4_setkey_enc(ctx, keyBytes); byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainBytes); return(encrypted); //return Hex.Encode(encrypted); }