new_10009_YanCheng_Metrology/Assets/Scripts/SecretUtils/Sm4Base.cs

82 lines
2.3 KiB
C#

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
{
/// <summary>
/// 生成SM4 byte Key
/// </summary>
/// <returns></returns>
public static byte[] GenerateKey()
{
byte[] sm4key= SM4Util.GenerateKey(SM4Util.SM4_KEY_128);
return sm4key;
}
/// <summary>
/// 生成SM4 string Key
/// </summary>
/// <returns></returns>
public static string GenerateKeyString()
{
byte[] sm4key = SM4Util.GenerateKey(SM4Util.SM4_KEY_128);
return Hex.ToHexString(sm4key, 0, sm4key.Length);
}
/// <summary>
/// CBC模式加密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static byte[] EncryptCBC(byte[] data,string key)
{
byte[] cipher = SM4Util.EncryptCBC(data, Hex.Decode(key), Hex.Decode(key));
return cipher;
}
/// <summary>
/// CBC模式解密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static byte[] DecryptCBC(byte[] data,string key)
{
byte[] plain = SM4Util.DecryptCBC(data, Hex.Decode(key), Hex.Decode(key));
return plain;
}
/// <summary>
/// ECB模式加密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static byte[] EncryptECB(byte[] data, byte[] key)
{
byte[] cipher = SM4Util.EncryptECB(data, key);
return cipher;
}
/// <summary>
/// ECB模式解密
/// </summary>
/// <param name="data"></param>
/// <param name="key"></param>
/// <returns></returns>
public static byte[] DecryptECB(byte[] data, byte[] key)
{
byte[] plain = SM4Util.DecryptECB(data, key);
return plain;
}
}
}