前言

今天大姚给大家分享一款C#版开源、免费的Bouncy Castle密码库:BouncyCastle。

项目介绍

BouncyCastle是一款C#版开源、免费的Bouncy Castle密码库,开发人员可以通过该项目在他们的 C# 应用程序中使用 Bouncy Castle 提供的各种密码学功能,从而加强数据的安全性和保护隐私信息。

Bouncy Castle密码学库介绍

Bouncy Castle是一个流行的密码学库,提供了广泛的密码算法和协议的实现。它由澳大利亚注册的慈善组织“Bouncy Castle军团”开发,旨在提供可靠而安全的加密解决方案。

项目源代码

创建控制台应用

创建一个名为:BouncyCastleExercise的控制台。

安装BouncyCastle包

搜索名为:BouncyCastle.Cryptography包安装:

BouncyCastle使用示例

internalclassProgram{staticvoidMain(string[]args){#regionAES加密解密示例stringaesPlaintext="Hello, 追逐时光者!!!";byte[]aesKey=newbyte[16];byte[]aesIV=newbyte[16];byte[]aesCiphertext=EncryptAES(aesPlaintext,aesKey,aesIV);stringdecryptedAesPlaintext=DecryptAES(aesCiphertext,aesKey,aesIV);Console.WriteLine("AESplaintext:"+aesPlaintext);Console.WriteLine("AESciphertext:"+Convert.ToBase64String(aesCiphertext));Console.WriteLine("DecryptedAESplaintext:"+decryptedAesPlaintext);#endregion#regionDES加密解密示例stringdesPlaintext="Hello,DES!";byte[]desKey=newbyte[8];byte[]desIV=newbyte[8];byte[]desCiphertext=EncryptDES(desPlaintext,desKey,desIV);stringdecryptedDesPlaintext=DecryptDES(desCiphertext,desKey,desIV);Console.WriteLine("DESplaintext:"+desPlaintext);Console.WriteLine("DESciphertext:"+Convert.ToBase64String(desCiphertext));Console.WriteLine("DecryptedDESplaintext:"+decryptedDesPlaintext);#endregion#regionRC4加密解密示例stringrc4Plaintext="Hello,RC4!";byte[]rc4Key=newbyte[16];byte[]rc4Ciphertext=EncryptRC4(rc4Plaintext,rc4Key);stringdecryptedRc4Plaintext=DecryptRC4(rc4Ciphertext,rc4Key);Console.WriteLine("RC4plaintext:"+rc4Plaintext);Console.WriteLine("RC4ciphertext:"+Convert.ToBase64String(rc4Ciphertext));Console.WriteLine("DecryptedRC4plaintext:"+decryptedRc4Plaintext);#endregion#region哈希算法示例//MD5示例stringmd5Plaintext="Hello,MD5!";stringmd5Hash=CalculateMD5Hash(md5Plaintext);Console.WriteLine("MD5hashof'Hello,MD5!':"+md5Hash);//SHA1示例stringsha1Plaintext="Hello,SHA1!";stringsha1Hash=CalculateSHA1Hash(sha1Plaintext);Console.WriteLine("SHA1hashof'Hello,SHA1!':"+sha1Hash);//SHA256示例stringsha256Plaintext="Hello,SHA256!";stringsha256Hash=CalculateSHA256Hash(sha256Plaintext);Console.WriteLine("SHA256hashof'Hello,SHA256!':"+sha256Hash);#endregion}#regionAES加密解密示例//////AES加密方法//////plaintext///key///iv///publicstaticbyte[]EncryptAES(stringplaintext,byte[]key,byte[]iv){IBufferedCiphercipher=CipherUtilities.GetCipher("AES/CTR/PKCS7Padding");cipher.Init(true,newParametersWithIV(ParameterUtilities.CreateKeyParameter("AES",key),iv));returncipher.DoFinal(System.Text.Encoding.UTF8.GetBytes(plaintext));}//////AES解密方法//////ciphertext///key///iv///publicstaticstringDecryptAES(byte[]ciphertext,byte[]key,byte[]iv){IBufferedCiphercipher=CipherUtilities.GetCipher("AES/CTR/PKCS7Padding");cipher.Init(false,newParametersWithIV(ParameterUtilities.CreateKeyParameter("AES",key),iv));byte[]plaintext=cipher.DoFinal(ciphertext);returnSystem.Text.Encoding.UTF8.GetString(plaintext);}#endregion#regionDES加密解密示例//////DES加密方法//////plaintext///key///iv///publicstaticbyte[]EncryptDES(stringplaintext,byte[]key,byte[]iv){IBufferedCiphercipher=CipherUtilities.GetCipher("DES/CBC/PKCS7Padding");cipher.Init(true,newParametersWithIV(ParameterUtilities.CreateKeyParameter("DES",key),iv));returncipher.DoFinal(System.Text.Encoding.UTF8.GetBytes(plaintext));}//////DES解密方法//////ciphertext///key///iv///publicstaticstringDecryptDES(byte[]ciphertext,byte[]key,byte[]iv){IBufferedCiphercipher=CipherUtilities.GetCipher("DES/CBC/PKCS7Padding");cipher.Init(false,newParametersWithIV(ParameterUtilities.CreateKeyParameter("DES",key),iv));byte[]plaintext=cipher.DoFinal(ciphertext);returnSystem.Text.Encoding.UTF8.GetString(plaintext);}#endregion#regionRC4加密解密示例//////RC4加密方法//////plaintext///key///publicstaticbyte[]EncryptRC4(stringplaintext,byte[]key){IStreamCiphercipher=newRC4Engine();cipher.Init(true,newKeyParameter(key));byte[]data=System.Text.Encoding.UTF8.GetBytes(plaintext);byte[]ciphertext=newbyte[data.Length];cipher.ProcessBytes(data,0,data.Length,ciphertext,0);returnciphertext;}//////RC4解密方法//////ciphertext///key///publicstaticstringDecryptRC4(byte[]ciphertext,byte[]key){IStreamCiphercipher=newRC4Engine();cipher.Init(false,newKeyParameter(key));byte[]plaintext=newbyte[ciphertext.Length];cipher.ProcessBytes(ciphertext,0,ciphertext.Length,plaintext,0);returnSystem.Text.Encoding.UTF8.GetString(plaintext);}#endregion#region哈希算法示例//////计算MD5哈希//////input///publicstaticstringCalculateMD5Hash(stringinput){IDigestdigest=newMD5Digest();byte[]hash=newbyte[digest.GetDigestSize()];byte[]data=System.Text.Encoding.UTF8.GetBytes(input);digest.BlockUpdate(data,0,data.Length);digest.DoFinal(hash,0);returnConvert.ToBase64String(hash);}//////计算SHA1哈希//////input///publicstaticstringCalculateSHA1Hash(stringinput){IDigestdigest=newSha1Digest();byte[]hash=newbyte[digest.GetDigestSize()];byte[]data=System.Text.Encoding.UTF8.GetBytes(input);digest.BlockUpdate(data,0,data.Length);digest.DoFinal(hash,0);returnConvert.ToBase64String(hash);}//////计算SHA256哈希//////input///publicstaticstringCalculateSHA256Hash(stringinput){IDigestdigest=newSha256Digest();byte[]hash=newbyte[digest.GetDigestSize()];byte[]data=System.Text.Encoding.UTF8.GetBytes(input);digest.BlockUpdate(data,0,data.Length);digest.DoFinal(hash,0);returnConvert.ToBase64String(hash);}#endregion}

输出结果:

项目源码地址

更多项目实用功能和特性欢迎前往项目开源地址查看,别忘了给项目一个Star支持。

https://github.com/bcgit/bc-csharp

优秀项目和框架精选

该项目已收录到C#/.NET/.NET Core优秀项目和框架精选中,关注优秀项目和框架精选能让你及时了解C#、.NET和.NET Core领域的最新动态和最佳实践,提高开发工作效率和质量。坑已挖,欢迎大家踊跃提交PR推荐或自荐(让优秀的项目和框架不被埋没)。

https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.md