当前位置: 首页 > news >正文

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

相关文章:

  • SA3D:基于 NeRF 的三维场景分割方法
  • k8s+wordpress+zabbix+elastic+filebeat+kibana服务搭建以及测试
  • APK漏洞扫描工具
  • 高可用篇_A Docker容器化技术_V 基于DockerCompose实现容器编排
  • MyBatis拦截器四种类型和自定义拦截器的使用流程
  • 黑豹程序员-java按限定范围产生多个随机数
  • BUUCTF-----[SWPU2019]Web1
  • C# 协程的使用
  • DVWA靶场-暴力破解
  • 【Java EE】线程安全的集合类
  • STM32点亮LED灯与蜂鸣器发声
  • .net6Api后台+uniapp导出Excel
  • @Autowired 与@Resource的区别
  • <机器学习初识>——《机器学习》
  • 第十三届蓝桥杯嵌入式省赛程序设计详细题解
  • 《Java编程思想》读书笔记-对象导论
  • 10个最佳ES6特性 ES7与ES8的特性
  • 5、React组件事件详解
  • Android框架之Volley
  • EOS是什么
  • IDEA 插件开发入门教程
  • IP路由与转发
  • Java教程_软件开发基础
  • tab.js分享及浏览器兼容性问题汇总
  • V4L2视频输入框架概述
  • Vue源码解析(二)Vue的双向绑定讲解及实现
  • 编写符合Python风格的对象
  • 精益 React 学习指南 (Lean React)- 1.5 React 与 DOM
  • 浏览器缓存机制分析
  • 前端技术周刊 2019-01-14:客户端存储
  • 王永庆:技术创新改变教育未来
  • 再次简单明了总结flex布局,一看就懂...
  • 智能合约开发环境搭建及Hello World合约
  • RDS-Mysql 物理备份恢复到本地数据库上
  • 如何用纯 CSS 创作一个货车 loader
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (2021|NIPS,扩散,无条件分数估计,条件分数估计)无分类器引导扩散
  • (C#)获取字符编码的类
  • (Java)【深基9.例1】选举学生会
  • (poj1.3.2)1791(构造法模拟)
  • (第27天)Oracle 数据泵转换分区表
  • (二)斐波那契Fabonacci函数
  • (黑马出品_高级篇_01)SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
  • (论文阅读26/100)Weakly-supervised learning with convolutional neural networks
  • (一)认识微服务
  • (转)人的集合论——移山之道
  • (转载)Linux 多线程条件变量同步
  • ***汇编语言 实验16 编写包含多个功能子程序的中断例程
  • ./mysql.server: 没有那个文件或目录_Linux下安装MySQL出现“ls: /var/lib/mysql/*.pid: 没有那个文件或目录”...
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET构架之我见
  • .NET连接MongoDB数据库实例教程
  • .NET中使用Redis (二)
  • /bin/bash^M: bad interpreter: No such file ordirectory