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

相关文章

Oracle登录时忘记用户名或密码该如何解决

《Oracle登录时忘记用户名或密码该如何解决》:本文主要介绍如何在Oracle12c中忘记用户名和密码时找回或重置用户账户信息,文中通过代码介绍的非常详细,对同样遇到这个问题的同学具有一定的参... 目录一、忘记账户:二、忘记密码:三、详细情况情况 1:1.1. 登录到数据库1.2. 查看当前用户信息1.

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)

《SpringBoot使用Jasypt对YML文件配置内容加密的方法(数据库密码加密)》本文介绍了如何在SpringBoot项目中使用Jasypt对application.yml文件中的敏感信息(如数... 目录SpringBoot使用Jasypt对YML文件配置内容进行加密(例:数据库密码加密)前言一、J

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

MySQL9.0默认路径安装下重置root密码

《MySQL9.0默认路径安装下重置root密码》本文主要介绍了MySQL9.0默认路径安装下重置root密码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录问题描述环境描述解决方法正常模式下修改密码报错原因问题描述mysqlChina编程采用默认安装路径,

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

MySQL修改密码的四种实现方式

《MySQL修改密码的四种实现方式》文章主要介绍了如何使用命令行工具修改MySQL密码,包括使用`setpassword`命令和`mysqladmin`命令,此外,还详细描述了忘记密码时的处理方法,包... 目录mysql修改密码四种方式一、set password命令二、使用mysqladmin三、修改u

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

C#从XmlDocument提取完整字符串的方法

《C#从XmlDocument提取完整字符串的方法》文章介绍了两种生成格式化XML字符串的方法,方法一使用`XmlDocument`的`OuterXml`属性,但输出的XML字符串不带格式,可读性差,... 方法1:通过XMLDocument的OuterXml属性,见XmlDocument类该方法获得的xm