C#版开源免费的Bouncy Castle密码库

2024-03-13 14:36

本文主要是介绍C#版开源免费的Bouncy Castle密码库,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

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

项目介绍

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

Bouncy Castle密码学库介绍

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

项目源代码

图片

图片

创建控制台应用

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

图片

图片

安装BouncyCastle包

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

图片

BouncyCastle使用示例

    internal class Program{static void Main(string[] args){#region AES加密解密示例string aesPlaintext = "Hello, 追逐时光者!!!";byte[] aesKey = new byte[16];byte[] aesIV = new byte[16];byte[] aesCiphertext = EncryptAES(aesPlaintext, aesKey, aesIV);string decryptedAesPlaintext = DecryptAES(aesCiphertext, aesKey, aesIV);Console.WriteLine("AES plaintext: " + aesPlaintext);Console.WriteLine("AES ciphertext: " + Convert.ToBase64String(aesCiphertext));Console.WriteLine("Decrypted AES plaintext: " + decryptedAesPlaintext);#endregion#region DES 加密解密示例string desPlaintext = "Hello, DES!";byte[] desKey = new byte[8];byte[] desIV = new byte[8];byte[] desCiphertext = EncryptDES(desPlaintext, desKey, desIV);string decryptedDesPlaintext = DecryptDES(desCiphertext, desKey, desIV);Console.WriteLine("DES plaintext: " + desPlaintext);Console.WriteLine("DES ciphertext: " + Convert.ToBase64String(desCiphertext));Console.WriteLine("Decrypted DES plaintext: " + decryptedDesPlaintext);#endregion#region RC4 加密解密示例string rc4Plaintext = "Hello, RC4!";byte[] rc4Key = new byte[16];byte[] rc4Ciphertext = EncryptRC4(rc4Plaintext, rc4Key);string decryptedRc4Plaintext = DecryptRC4(rc4Ciphertext, rc4Key);Console.WriteLine("RC4 plaintext: " + rc4Plaintext);Console.WriteLine("RC4 ciphertext: " + Convert.ToBase64String(rc4Ciphertext));Console.WriteLine("Decrypted RC4 plaintext: " + decryptedRc4Plaintext);#endregion#region 哈希算法示例// MD5 示例string md5Plaintext = "Hello, MD5!";string md5Hash = CalculateMD5Hash(md5Plaintext);Console.WriteLine("MD5 hash of 'Hello, MD5!': " + md5Hash);// SHA1 示例string sha1Plaintext = "Hello, SHA1!";string sha1Hash = CalculateSHA1Hash(sha1Plaintext);Console.WriteLine("SHA1 hash of 'Hello, SHA1!': " + sha1Hash);// SHA256 示例string sha256Plaintext = "Hello, SHA256!";string sha256Hash = CalculateSHA256Hash(sha256Plaintext);Console.WriteLine("SHA256 hash of 'Hello, SHA256!': " + sha256Hash);#endregion}#region AES加密解密示例/// <summary>/// AES 加密方法/// </summary>/// <param name="plaintext">plaintext</param>/// <param name="key">key</param>/// <param name="iv">iv</param>/// <returns></returns>public static byte[] EncryptAES(string plaintext, byte[] key, byte[] iv){IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/PKCS7Padding");cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));return cipher.DoFinal(System.Text.Encoding.UTF8.GetBytes(plaintext));}/// <summary>/// AES 解密方法/// </summary>/// <param name="ciphertext">ciphertext</param>/// <param name="key">key</param>/// <param name="iv">iv</param>/// <returns></returns>public static string DecryptAES(byte[] ciphertext, byte[] key, byte[] iv){IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/PKCS7Padding");cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES", key), iv));byte[] plaintext = cipher.DoFinal(ciphertext);return System.Text.Encoding.UTF8.GetString(plaintext);}#endregion#region DES 加密解密示例/// <summary>/// DES 加密方法/// </summary>/// <param name="plaintext">plaintext</param>/// <param name="key">key</param>/// <param name="iv">iv</param>/// <returns></returns>public static byte[] EncryptDES(string plaintext, byte[] key, byte[] iv){IBufferedCipher cipher = CipherUtilities.GetCipher("DES/CBC/PKCS7Padding");cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("DES", key), iv));return cipher.DoFinal(System.Text.Encoding.UTF8.GetBytes(plaintext));}/// <summary>/// DES 解密方法/// </summary>/// <param name="ciphertext">ciphertext</param>/// <param name="key">key</param>/// <param name="iv">iv</param>/// <returns></returns>public static string DecryptDES(byte[] ciphertext, byte[] key, byte[] iv){IBufferedCipher cipher = CipherUtilities.GetCipher("DES/CBC/PKCS7Padding");cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("DES", key), iv));byte[] plaintext = cipher.DoFinal(ciphertext);return System.Text.Encoding.UTF8.GetString(plaintext);}#endregion#region RC4 加密解密示例/// <summary>/// RC4 加密方法/// </summary>/// <param name="plaintext">plaintext</param>/// <param name="key">key</param>/// <returns></returns>public static byte[] EncryptRC4(string plaintext, byte[] key){IStreamCipher cipher = new RC4Engine();cipher.Init(true, new KeyParameter(key));byte[] data = System.Text.Encoding.UTF8.GetBytes(plaintext);byte[] ciphertext = new byte[data.Length];cipher.ProcessBytes(data, 0, data.Length, ciphertext, 0);return ciphertext;}/// <summary>/// RC4 解密方法/// </summary>/// <param name="ciphertext">ciphertext</param>/// <param name="key">key</param>/// <returns></returns>public static string DecryptRC4(byte[] ciphertext, byte[] key){IStreamCipher cipher = new RC4Engine();cipher.Init(false, new KeyParameter(key));byte[] plaintext = new byte[ciphertext.Length];cipher.ProcessBytes(ciphertext, 0, ciphertext.Length, plaintext, 0);return System.Text.Encoding.UTF8.GetString(plaintext);}#endregion#region 哈希算法示例/// <summary>/// 计算 MD5 哈希/// </summary>/// <param name="input">input</param>/// <returns></returns>public static string CalculateMD5Hash(string input){IDigest digest = new MD5Digest();byte[] hash = new byte[digest.GetDigestSize()];byte[] data = System.Text.Encoding.UTF8.GetBytes(input);digest.BlockUpdate(data, 0, data.Length);digest.DoFinal(hash, 0);return Convert.ToBase64String(hash);}/// <summary>/// 计算 SHA1 哈希/// </summary>/// <param name="input">input</param>/// <returns></returns>public static string CalculateSHA1Hash(string input){IDigest digest = new Sha1Digest();byte[] hash = new byte[digest.GetDigestSize()];byte[] data = System.Text.Encoding.UTF8.GetBytes(input);digest.BlockUpdate(data, 0, data.Length);digest.DoFinal(hash, 0);return Convert.ToBase64String(hash);}/// <summary>/// 计算 SHA256 哈希/// </summary>/// <param name="input">input</param>/// <returns></returns>public static string CalculateSHA256Hash(string input){IDigest digest = new Sha256Digest();byte[] hash = new byte[digest.GetDigestSize()];byte[] data = System.Text.Encoding.UTF8.GetBytes(input);digest.BlockUpdate(data, 0, data.Length);digest.DoFinal(hash, 0);return Convert.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

这篇关于C#版开源免费的Bouncy Castle密码库的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/805189

相关文章

C#基础之委托详解(Delegate)

《C#基础之委托详解(Delegate)》:本文主要介绍C#基础之委托(Delegate),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 委托定义2. 委托实例化3. 多播委托(Multicast Delegates)4. 委托的用途事件处理回调函数LINQ

在C#中调用Python代码的两种实现方式

《在C#中调用Python代码的两种实现方式》:本文主要介绍在C#中调用Python代码的两种实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#调用python代码的方式1. 使用 Python.NET2. 使用外部进程调用 Python 脚本总结C#调

C#中的 StreamReader/StreamWriter 使用示例详解

《C#中的StreamReader/StreamWriter使用示例详解》在C#开发中,StreamReader和StreamWriter是处理文本文件的核心类,属于System.IO命名空间,本... 目录前言一、什么是 StreamReader 和 StreamWriter?1. 定义2. 特点3. 用

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

如何使用C#串口通讯实现数据的发送和接收

《如何使用C#串口通讯实现数据的发送和接收》本文详细介绍了如何使用C#实现基于串口通讯的数据发送和接收,通过SerialPort类,我们可以轻松实现串口通讯,并结合事件机制实现数据的传递和处理,感兴趣... 目录1. 概述2. 关键技术点2.1 SerialPort类2.2 异步接收数据2.3 数据解析2.

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

电脑报错cxcore100.dll丢失怎么办? 多种免费修复缺失的cxcore100.dll文件的技巧

《电脑报错cxcore100.dll丢失怎么办?多种免费修复缺失的cxcore100.dll文件的技巧》你是否也遇到过“由于找不到cxcore100.dll,无法继续执行代码,重新安装程序可能会解... 当电脑报错“cxcore100.dll未找到”时,这通常意味着系统无法找到或加载这编程个必要的动态链接库

C# 委托中 Invoke/BeginInvoke/EndInvoke和DynamicInvoke 方法的区别和联系

《C#委托中Invoke/BeginInvoke/EndInvoke和DynamicInvoke方法的区别和联系》在C#中,委托(Delegate)提供了多种调用方式,包括Invoke、Begi... 目录前言一、 Invoke方法1. 定义2. 特点3. 示例代码二、 BeginInvoke 和 EndI

无需邀请码!Manus复刻开源版OpenManus下载安装与体验

《无需邀请码!Manus复刻开源版OpenManus下载安装与体验》Manus的完美复刻开源版OpenManus安装与体验,无需邀请码,手把手教你如何在本地安装与配置Manus的开源版OpenManu... Manus是什么?Manus 是 Monica 团队推出的全球首款通用型 AI Agent。Man

C#中的 Dictionary常用操作

《C#中的Dictionary常用操作》C#中的DictionaryTKey,TValue是用于存储键值对集合的泛型类,允许通过键快速检索值,并且具有唯一键、动态大小和无序集合的特性,常用操作包括添... 目录基本概念Dictionary的基本结构Dictionary的主要特性Dictionary的常用操作