c# Bouncy Castle 密码库

Aes:加密解密

using BouncyCastleDemo;

string plaintext = "Hello, BouncyCastle!";

byte[] ciphertext = Util.EncryptAES(plaintext, aesKey, aesIV);
string decryptedText = Util.DecryptAES(ciphertext, aesKey, aesIV);

Console.WriteLine($"Plaintext: {plaintext}");
Console.WriteLine($"Ciphertext: {Convert.ToBase64String(ciphertext)}");
Console.WriteLine($"Decrypted Text: {decryptedText}");

using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using System.Text;

public static byte[] EncryptAES(string plaintext, byte[] key, byte[] iv)
{

    IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7Padding");
    cipher.Init(true, new ParametersWithIV(new KeyParameter(key), iv));
    return cipher.DoFinal(Encoding.UTF8.GetBytes(plaintext));
}

public static string DecryptAES(byte[] ciphertext, byte[] key, byte[] iv)
{
    IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7Padding");
    cipher.Init(false, new ParametersWithIV(new KeyParameter(key), iv));
    byte[] plaintext = cipher.DoFinal(ciphertext);
    return Encoding.UTF8.GetString(plaintext);
}

SM2:非对称加密与签名

using BouncyCastleDemo;
using Org.BouncyCastle.Asn1.GM;
using Org.BouncyCastle.Crypto.Generators;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;

var keyPair = Util.GenerateSm2KeyPair();
var privateKey = (ECPrivateKeyParameters)keyPair.Private;
var publicKey = (ECPublicKeyParameters)keyPair.Public;

Console.WriteLine($"Private Key: {privateKey.D}");
Console.WriteLine($"Public Key X: {publicKey.Q.XCoord}");
Console.WriteLine($"Public Key Y: {publicKey.Q.YCoord}");

public static AsymmetricCipherKeyPair GenerateSm2KeyPair()
{
    // 获取 SM2 椭圆曲线参数
    var ecParams = GMNamedCurves.GetByName("sm2p256v1");
    var domainParams = new ECDomainParameters(ecParams.Curve, ecParams.G, ecParams.N, ecParams.H);

    // 初始化密钥生成器
    var keyGen = new ECKeyPairGenerator("EC"); // 使用 "EC" 作为算法名称
    var keyGenParams = new ECKeyGenerationParameters(domainParams, new SecureRandom());
    keyGen.Init(keyGenParams);

    // 生成密钥对
    return keyGen.GenerateKeyPair();
}

SM3:哈希算法

using BouncyCastleDemo;
using Org.BouncyCastle.Crypto.Digests;

string input = "Hello, SM3!";
byte[] data = System.Text.Encoding.UTF8.GetBytes(input);
// 计算SM3哈希
byte[] hash = Util.CalculateSM3Hash(data);
Console.WriteLine("SM3 Hash: " + BitConverter.ToString(hash).Replace("-", "").ToLower());

public static byte[] CalculateSM3Hash(byte[] data)
{
    // 创建SM3摘要对象
    var sm3Digest = new SM3Digest();
    // 更新数据
    sm3Digest.BlockUpdate(data, 0, data.Length);
    // 获取摘要结果
    byte[] hash = newbyte[sm3Digest.GetDigestSize()];
    sm3Digest.DoFinal(hash, 0);

    return hash;
}

SM4:对称加密

SM4 是中国国家密码管理局发布的对称分组加密算法,也称为 SMS4。它是一种分组密码算法,采用 128 位的分组长度和 128 位的密钥长度,通过 32 轮非线性迭代实现加密和解密。

SM4 工作模式 SM4 支持多种工作模式,适用于不同的应用场景: ECB(电子密码本模式): 独立加密每个数据块,相同的明文块产生相同的密文块。 适用于加密大量重复数据块,但安全性较低。 CBC(密码块链接模式): 使用前一个块的密文与当前块的明文进行 XOR 操作后再加密。 适用于需要较高安全性的场合,如文件加密和网络通信。 CFB(密码反馈模式): 将加密算法当作流密码使用,适用于加密字节流或实时数据传输。 OFB(输出反馈模式): 生成密钥流与明文进行 XOR 操作,适用于加密大量数据。 CTR(计数器模式): 使用递增的计数器与密钥一起加密固定值,然后与明文进行 XOR 操作。 适用于大数据量的加密,具有高安全性和高效率。

using BouncyCastleDemo;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Paddings;
using Org.BouncyCastle.Crypto.Parameters;
using System.Text;

string plainText = "Hello, SM4!";
byte[] key = Encoding.UTF8.GetBytes("0123456789abcdef"); // 16 字节密钥
byte[] iv = Encoding.UTF8.GetBytes("0123456789abcdef"); // 16 字节 IV
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);

// 加密
byte[] encrypted = SM4Example.SM4Encrypt(plainBytes, key, iv);
Console.WriteLine("加密结果(Base64): " + Convert.ToBase64String(encrypted));

// 解密
byte[] decrypted = SM4Example.SM4Decrypt(encrypted, key, iv);
Console.WriteLine("解密结果: " + Encoding.UTF8.GetString(decrypted));

publicclassSM4Example
{
public static byte[] SM4Encrypt(byte[] plainText, byte[] key, byte[] iv)
  {
      var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new SM4Engine()), new Pkcs7Padding());
      cipher.Init(true, new ParametersWithIV(new KeyParameter(key), iv));
      return cipher.DoFinal(plainText);
  }

public static byte[] SM4Decrypt(byte[] cipherText, byte[] key, byte[] iv)
  {
      var cipher = new PaddedBufferedBlockCipher(new CbcBlockCipher(new SM4Engine()), new Pkcs7Padding());
      cipher.Init(false, new ParametersWithIV(new KeyParameter(key), iv));
      return cipher.DoFinal(cipherText);
  }

}

作者:spike

分类: Net

创作时间:2025-04-05

更新时间:2025-04-07

联系方式放在中括号之中例如[[email protected]],回复评论在开头加上标号例如:#1