C#实现SM4国密算法,可调用可运行,有示例
此项目基础需要第三方Nuget包:Portable.BouncyCastle;
添加方法:右击项目名称,选择“管理Nuget程序包”,在搜索栏搜索Portable.BouncyCastle,选择安装。
此项目有四个类,Sm4Crypto、SM4、SM4Context、Form1。其中Form1是调用加密算法及输入原文的界面类。
需要引入的命名空间:
using Org.BouncyCastle.Utilities.Encoders;
1.Sm4Crypto
public class Sm4Crypto
{
public string secretKey = “98145489617106616498”;
public string iv = “0000000000000000”;
public bool hexString = false;
#region ECB模式加密
/// <summary>
/// ECB模式加密
/// </summary>
/// <param name="plainText"></param>
/// <returns></returns>
public byte[] Encrypt_ECB(string plainText)
{
SM4Context ctx = new SM4Context();
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));
return Hex.Encode(encrypted);
}
/// <summary>
/// ECB模式加密
/// </summary>
/// <param name="plainBytes"></param>
/// <returns></returns>
public byte[] Encrypt_ECB(byte[] plainBytes)
{
SM4Context ctx = new SM4Context();
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, plainBytes);
return Hex.Encode(encrypted);
}
/// <summary>
/// ECB模式加密
/// </summary>
/// <param name="plainText"></param>
/// <returns></returns>
public string Encrypt_ECB_Str(string plainText)
{
SM4Context ctx = new SM4Context();
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));
return Encoding.Default.GetString(Hex.Encode(encrypted));
}
/// <summary>
/// ECB模式加密
/// </summary>
/// <param name="plainBytes"></param>
/// <returns></returns>
public string Encrypt_ECB_Str(byte[] plainBytes)
{
SM4Context ctx = new SM4Context();
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, plainBytes);
return Encoding.Default.GetString(Hex.Encode(encrypted));
}
#endregion
#region ECB模式解密
/// <summary>
/// ECB模式解密
/// </summary>
/// <param name="cipherText"></param>
/// <returns></returns>
public byte[] Decrypt_ECB(string cipherText)
{
SM4Context ctx = new SM4Context();
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 decrypted;
}
/// <summary>
/// ECB模式解密
/// </summary>
/// <param name="plainBytes"></param>
/// <returns></returns>
public byte[] Decrypt_ECB(byte[] cipherBytes)
{
SM4Context ctx = new SM4Context();
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(cipherBytes));
return decrypted;
}
/// <summary>
/// ECB模式解密
/// </summary>
/// <param name="cipherText"&