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

简介: 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

相关文章
|
6天前
|
自然语言处理 C# Windows
C#开源免费的Windows右键菜单管理工具
C#开源免费的Windows右键菜单管理工具
|
6天前
|
Linux C# 开发工具
C#开源的一款友好的.NET SDK管理器
C#开源的一款友好的.NET SDK管理器
|
6天前
|
IDE C# 开发工具
一个开源轻量级的C#代码格式化工具(支持VS和VS Code)
一个开源轻量级的C#代码格式化工具(支持VS和VS Code)
|
6天前
|
数据采集 前端开发 数据挖掘
Fizzler库+C#:从微博抓取热点的最简单方法
本文介绍如何使用Fizzler库和C#构建微博热点信息爬虫。通过Fizzler的CSS选择器定位关键信息,提取热点标题和排名,实现微博内容的智能挖掘。示例代码展示单线程和多线程采集方法,并涉及代理IP使用。
Fizzler库+C#:从微博抓取热点的最简单方法
|
6天前
|
C# Windows
C#开源的两款功能强大的录屏神器
C#开源的两款功能强大的录屏神器
|
6天前
|
数据采集 存储 C#
抓取Instagram数据:Fizzler库带您进入C#程序的世界
在当今数字化的世界中,数据是无价之宝。社交媒体平台如Instagram成为了用户分享照片、视频和故事的热门场所。作为开发人员,我们可以利用爬虫技术来抓取这些平台上的数据,进行分析、挖掘和应用。本文将介绍如何使用C#编写一个简单的Instagram爬虫程序,使用Fizzler库来解析HTML页面,同时利用代理IP技术提高采集效率。
抓取Instagram数据:Fizzler库带您进入C#程序的世界
|
6天前
|
Web App开发 API C#
PuppeteerSharp库在C#中的应用案例
PuppeteerSharp库在C#中的应用案例
|
6天前
|
API C# C++
微软官方发布的C#开源、免费、实用的Windows工具箱
微软官方发布的C#开源、免费、实用的Windows工具箱
|
6天前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
|
6天前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。