public 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.Default.GetBytes(secretKey); } SM4 sm4 = new SM4(); sm4.sm4_setkey_enc(ctx, keyBytes); byte[] encrypted = sm4.sm4_crypt_ecb(ctx, Encoding.Default.GetBytes(plainText)); String cipherText = Encoding.Default.GetString(Hex.Encode(encrypted)); return(cipherText); }
public 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.Default.GetBytes(secretKey); } SM4 sm4 = new SM4(); sm4.sm4_setkey_dec(ctx, keyBytes); byte[] decrypted = sm4.sm4_crypt_ecb(ctx, Hex.Decode(cipherText)); return(Encoding.Default.GetString(decrypted)); }