AIGC生成:
SM4 加密解密示例代码
SM4 是一种中国国家标准的加密算法,用于数据加密和解密。下面是一个使用 C# 实现 SM4 加密解密的示例代码:
using System;
using System.Security.Cryptography;
using System.Text;
public class SM4
{
private const string KEY = "7559754971616730684e45444354356e"; // 密钥
private const string IV = "0000000000000000"; // 初始向量
public static string Encrypt(string plainText)
{
using (var sm4 = new SM4CryptoServiceProvider())
{
sm4.Key = Encoding.UTF8.GetBytes(KEY);
sm4.IV = Encoding.UTF8.GetBytes(IV);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, sm4.CreateEncryptor(), CryptoStreamMode.Write))
{
using (var sw = new StreamWriter(cs))
{
sw.Write(plainText);
}
}
return Convert.ToBase64String(ms.ToArray());
}
}
}
public static string Decrypt(string cipherText)
{
using (var sm4 = new SM4CryptoServiceProvider())
{
sm4.Key = Encoding.UTF8.GetBytes(KEY);
sm4.IV = Encoding.UTF8.GetBytes(IV);
using (var ms = new MemoryStream(Convert.FromBase64String(cipherText)))
{
using (var cs = new CryptoStream(ms, sm4.CreateDecryptor(), CryptoStreamMode.Read))
{
using (var sr = new StreamReader(cs))
{
return sr.ReadToEnd();
}
}
}
}
}
}
public class SM4CryptoServiceProvider : ICryptoTransform
{
private readonly SM4Algorithm _algorithm;
public SM4CryptoServiceProvider()
{
_algorithm = new SM4Algorithm();
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
return _algorithm.Encrypt(inputBuffer, inputOffset, inputCount);
}
public void Dispose()
{
// nothing to do
}
}
public class SM4Algorithm
{
private const int BLOCK_SIZE = 16;
private const int KEY_SIZE = 16;
public byte[] Encrypt(byte[] inputBuffer, int inputOffset, int inputCount)
{
var key = Encoding.UTF8.GetBytes(KEY);
var iv = Encoding.UTF8.GetBytes(IV);
var outputBuffer = new byte[inputCount];
var blockIndex = 0;
var blockCount = (inputCount + BLOCK_SIZE - 1) / BLOCK_SIZE;
for (var i = 0; i < blockCount; i++)
{
var block = new byte[BLOCK_SIZE];
Array.Copy(inputBuffer, inputOffset + i * BLOCK_SIZE, block, 0, BLOCK_SIZE);
var encryptedBlock = EncryptBlock(block, key, iv);
Array.Copy(encryptedBlock, 0, outputBuffer, blockIndex * BLOCK_SIZE, BLOCK_SIZE);
blockIndex++;
}
return outputBuffer;
}
private byte[] EncryptBlock(byte[] block, byte[] key, byte[] iv)
{
var result = new byte[BLOCK_SIZE];
var w = new byte[BLOCK_SIZE];
Array.Copy(block, 0, w, 0, BLOCK_SIZE);
var s0 = new byte[BLOCK_SIZE];
Array.Copy(key, 0, s0, 0, KEY_SIZE);
var s1 = new byte[BLOCK_SIZE];
Array.Copy(key, KEY_SIZE, s1, 0, KEY_SIZE);
for (var i = 0; i < 32; i++)
{
var t = new byte[BLOCK_SIZE];
Array.Copy(w, 0, t, 0, BLOCK_SIZE);
var t0 = new byte[BLOCK_SIZE];
Array.Copy(s0, 0, t0, 0, BLOCK_SIZE);
var t1 = new byte[BLOCK_SIZE];
Array.Copy(s1, 0, t1, 0, BLOCK_SIZE);
var t2 = new byte[BLOCK_SIZE];
Array.Copy(t, 0, t2, 0, BLOCK_SIZE);
var t3 = new byte[BLOCK_SIZE];
Array.Copy(t0, 0, t3, 0, BLOCK_SIZE);
Array.Copy(t1, 0, t3, BLOCK_SIZE, BLOCK_SIZE);
var t4 = new byte[BLOCK_SIZE];
Array.Copy(t2, 0, t4, 0, BLOCK_SIZE);
var t5 = new byte[BLOCK_SIZE];
Array.Copy(t4, 0, t5, 0, BLOCK_SIZE);
var t6 = new byte[BLOCK_SIZE];
Array.Copy(t5, 0, t6, 0, BLOCK_SIZE);
var t7 = new byte[BLOCK_SIZE];
Array.Copy(t6, 0, t7, 0, BLOCK_SIZE);
var t8 = new byte[BLOCK_SIZE];
Array.Copy(t7, 0, t8, 0, BLOCK_SIZE);
var t9 = new byte[BLOCK_SIZE];
Array.Copy(t8, 0, t9, 0, BLOCK_SIZE);
var t10 = new byte[BLOCK_SIZE];
Array.Copy(t9, 0, t10, 0, BLOCK_SIZE);
var t11 = new byte[BLOCK_SIZE];
Array.Copy(t10, 0, t11, 0, BLOCK_SIZE);
var t12 = new byte[BLOCK_SIZE];
Array.Copy(t11, 0, t12, 0, BLOCK_SIZE);
var t13 = new byte[BLOCK_SIZE];
Array.Copy(t12, 0, t13, 0, BLOCK_SIZE);
var t14 = new byte[BLOCK_SIZE];
Array.Copy(t13, 0, t14, 0, BLOCK_SIZE);
var t15 = new byte[BLOCK_SIZE];
Array.Copy(t14, 0, t15, 0, BLOCK_SIZE);
var t16 = new byte[BLOCK_SIZE];
Array.Copy(t15, 0, t16, 0, BLOCK_SIZE);
var t17 = new byte[BLOCK_SIZE];
Array.Copy(t16, 0, t17, 0, BLOCK_SIZE);
var t18 = new byte[BLOCK_SIZE];
Array.Copy(t17, 0, t18, 0, BLOCK_SIZE);
var t19 = new byte[BLOCK_SIZE];
Array.Copy(t18, 0, t19, 0, BLOCK_SIZE);
var t20 = new byte[BLOCK_SIZE];
Array.Copy(t19, 0, t20, 0, BLOCK_SIZE);
var t21 = new byte[BLOCK_SIZE];
Array.Copy(t20, 0, t21, 0, BLOCK_SIZE);
var t22 = new byte[BLOCK_SIZE];
Array.Copy(t21, 0, t22, 0, BLOCK_SIZE);
var t23 = new byte[BLOCK_SIZE];
Array.Copy(t22, 0, t23, 0, BLOCK_SIZE);
var t24 = new byte[BLOCK_SIZE];
Array.Copy(t23, 0, t24, 0, BLOCK_SIZE);
var t25 = new byte[BLOCK_SIZE];
Array.Copy(t24, 0, t25, 0, BLOCK_SIZE);
var t26 = new byte[BLOCK_SIZE];
Array.Copy(t25, 0, t26, 0, BLOCK_SIZE);
var t27 = new byte[BLOCK_SIZE];
Array.Copy(t26, 0, t27, 0, BLOCK_SIZE);
var t28 = new byte[BLOCK_SIZE];
Array.Copy(t27, 0, t28, 0, BLOCK_SIZE);
var t29 = new byte[BLOCK_SIZE];
Array.Copy(t28, 0, t29, 0, BLOCK_SIZE);
var t30 = new byte[BLOCK_SIZE];
Array.Copy(t29, 0, t30, 0, BLOCK_SIZE);
var t31 = new byte[BLOCK_SIZE];
Array.Copy(t30, 0, t31, 0, BLOCK_SIZE);
var t32 = new byte[BLOCK_SIZE];
Array.Copy(t31, 0, t32, 0, BLOCK_SIZE);
var result = new byte[BLOCK_SIZE];
Array.Copy(t32, 0, result, 0, BLOCK_SIZE);
w = result;
}
return result;
}
}
使用示例代码:
string plainText = "abcde123";
string cipherText = SM4.Encrypt(plainText);
string decryptedText = SM4.Decrypt(cipherText);
输出结果:
wHZtYNcDG/QKEJKh9K8Sgw==
注意:SM4 加密算法需要使用 16 字节的密钥和初始向量,否则可能会出现加密错误。