using System; using System.Collections.Generic; using System.Text; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Paddings; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Utilities.Encoders; using SecretUtils.Crypto; namespace SecretUtils { public class Sm4Base { /// /// 生成SM4 byte Key /// /// public static byte[] GenerateKey() { byte[] sm4key= SM4Util.GenerateKey(SM4Util.SM4_KEY_128); return sm4key; } /// /// 生成SM4 string Key /// /// public static string GenerateKeyString() { byte[] sm4key = SM4Util.GenerateKey(SM4Util.SM4_KEY_128); return Hex.ToHexString(sm4key, 0, sm4key.Length); } /// /// CBC模式加密 /// /// /// /// public static byte[] EncryptCBC(byte[] data,string key) { byte[] cipher = SM4Util.EncryptCBC(data, Hex.Decode(key), Hex.Decode(key)); return cipher; } /// /// CBC模式解密 /// /// /// /// public static byte[] DecryptCBC(byte[] data,string key) { byte[] plain = SM4Util.DecryptCBC(data, Hex.Decode(key), Hex.Decode(key)); return plain; } /// /// ECB模式加密 /// /// /// /// public static byte[] EncryptECB(byte[] data, byte[] key) { byte[] cipher = SM4Util.EncryptECB(data, key); return cipher; } /// /// ECB模式解密 /// /// /// /// public static byte[] DecryptECB(byte[] data, byte[] key) { byte[] plain = SM4Util.DecryptECB(data, key); return plain; } } }