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

相关文章

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

使用C#删除Excel表格中的重复行数据的代码详解

《使用C#删除Excel表格中的重复行数据的代码详解》重复行是指在Excel表格中完全相同的多行数据,删除这些重复行至关重要,因为它们不仅会干扰数据分析,还可能导致错误的决策和结论,所以本文给大家介绍... 目录简介使用工具C# 删除Excel工作表中的重复行语法工作原理实现代码C# 删除指定Excel单元

SpringBoot如何对密码等敏感信息进行脱敏处理

《SpringBoot如何对密码等敏感信息进行脱敏处理》这篇文章主要为大家详细介绍了SpringBoot对密码等敏感信息进行脱敏处理的几个常用方法,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录​1. 配置文件敏感信息脱敏​​2. 日志脱敏​​3. API响应脱敏​​4. 其他注意事项​​总结

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

C#使用MQTTnet实现服务端与客户端的通讯的示例

《C#使用MQTTnet实现服务端与客户端的通讯的示例》本文主要介绍了C#使用MQTTnet实现服务端与客户端的通讯的示例,包括协议特性、连接管理、QoS机制和安全策略,具有一定的参考价值,感兴趣的可... 目录一、MQTT 协议简介二、MQTT 协议核心特性三、MQTTNET 库的核心功能四、服务端(BR

C#继承之里氏替换原则分析

《C#继承之里氏替换原则分析》:本文主要介绍C#继承之里氏替换原则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#里氏替换原则一.概念二.语法表现三.类型检查与转换总结C#里氏替换原则一.概念里氏替换原则是面向对象设计的基本原则之一:核心思想:所有引py

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

C#实现访问远程硬盘的图文教程

《C#实现访问远程硬盘的图文教程》在现实场景中,我们经常用到远程桌面功能,而在某些场景下,我们需要使用类似的远程硬盘功能,这样能非常方便地操作对方电脑磁盘的目录、以及传送文件,这次我们将给出一个完整的... 目录引言一. 远程硬盘功能展示二. 远程硬盘代码实现1. 底层业务通信实现2. UI 实现三. De

C#通过进程调用外部应用的实现示例

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下... 目录窗口程序类进程信息类 系统设置类 以WINFORM应用程序为例,在C#应用程序中调用python程序

基于C#实现MQTT通信实战

《基于C#实现MQTT通信实战》MQTT消息队列遥测传输,在物联网领域应用的很广泛,它是基于Publish/Subscribe模式,具有简单易用,支持QoS,传输效率高的特点,下面我们就来看看C#实现... 目录1、连接主机2、订阅消息3、发布消息MQTT(Message Queueing Telemetr