Bouncy Castle :国密加密在.NET Core中的实践

B站影视 2025-02-05 08:50 3

摘要:Bouncy Castle 是一个开源的密码学库, 提供了丰富的加密算法实现,包括常见的对称加密算法(如 AES)、非对称加密算法(如 RSA、ECC)、哈希算法(如 SHA-256)以及数字签名算法(如 ECDSA)

Bouncy Castle 是一个开源的密码学库, 提供了丰富的加密算法实现,包括常见的对称加密算法(如 AES)、非对称加密算法(如 RSA、ECC)、哈希算法(如 SHA-256)以及数字签名算法(如 ECDSA)

第一步,老规矩, NuGet 包 安装。

Install-Package BouncyCastle.CryptographyAes:加密解密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)
{

cipher.Init(false, new ParametersWithIV(new KeyParameter(key), iv));
byte plaintext = cipher.DoFinal(ciphertext);
return Encoding.UTF8.GetString(plaintext);
}

输出:

Plaintext: Hello, BouncyCastle!

Decrypted Text: Hello, BouncyCastle!

SM2:非对称加密与签名

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 AsymmetricCipherKeyPairGenerateSm2KeyPair
{
// 获取 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;
}

输出:

Private Key: 52304744741195071390077799369490193481967288198229563887116583397880577348854

Public Key X: 7a981aa814b13b6494ccfa2b500188b8c3ce06de13e56abca21aae71c9b48b1e

Public Key Y: 24d19e15ba945cf31b8b26321ed5c1ecff88480103a43f447f98cf7efe596c36

SM3:哈希算法

SM3 是一种密码哈希算法,类似于 SHA-256,但具有更高的安全性。

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

stringinput ="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 staticbyteCalculateSM3Hash(byte data)
{
// 创建SM3摘要对象
var sm3Digest = new SM3Digest;
// 更新数据
sm3Digest.BlockUpdate(data,0, data.Length);
// 获取摘要结果
byte hash = newbyte[sm3Digest.GetDigestSize];
sm3Digest.DoFinal(hash,0);

return hash;
}

输出:LSM3 Hash: 21b937fed61e685b8ac08c67fe9a3300437f2ca44547dea06e0cfe30219fdc4c

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;

stringplainText ="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 staticbyteSM4Encrypt(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 staticbyteSM4Decrypt(byte cipherText,byte key,byte iv)
{

cipher.Init(false
return cipher.DoFinal(cipherText);
}

}

输出:

加密结果(Base64): 0ftObaUj9/7kATP2BoEImQ==

解密结果: Hello, SM4!

• https://www.cnblogs.com/syzcyyx/articles/18258031

• https://www.bouncycastle.org/documentation/documentation-c/

Net分享”,技术文章第一时间推送,随缘更新 , 分享一些你可能注意不到的细节。

来源:opendotnet

相关推荐