当前位置:   article > 正文

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

bouncy castle c#

前言

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

项目介绍

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

Bouncy Castle密码学库介绍

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

项目源代码

图片

图片

创建控制台应用

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

图片

图片

安装BouncyCastle包

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

图片

BouncyCastle使用示例

  1.     internal class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             #region AES加密解密示例
  6.             string aesPlaintext = "Hello, 追逐时光者!!!";
  7.             byte[] aesKey = new byte[16];
  8.             byte[] aesIV = new byte[16];
  9.             byte[] aesCiphertext = EncryptAES(aesPlaintext, aesKey, aesIV);
  10.             string decryptedAesPlaintext = DecryptAES(aesCiphertext, aesKey, aesIV);
  11.             Console.WriteLine("AES plaintext: " + aesPlaintext);
  12.             Console.WriteLine("AES ciphertext: " + Convert.ToBase64String(aesCiphertext));
  13.             Console.WriteLine("Decrypted AES plaintext: " + decryptedAesPlaintext);
  14.             #endregion
  15.             #region DES 加密解密示例
  16.             string desPlaintext = "Hello, DES!";
  17.             byte[] desKey = new byte[8];
  18.             byte[] desIV = new byte[8];
  19.             byte[] desCiphertext = EncryptDES(desPlaintext, desKey, desIV);
  20.             string decryptedDesPlaintext = DecryptDES(desCiphertext, desKey, desIV);
  21.             Console.WriteLine("DES plaintext: " + desPlaintext);
  22.             Console.WriteLine("DES ciphertext: " + Convert.ToBase64String(desCiphertext));
  23.             Console.WriteLine("Decrypted DES plaintext: " + decryptedDesPlaintext);
  24.             #endregion
  25.             #region RC4 加密解密示例
  26.             string rc4Plaintext = "Hello, RC4!";
  27.             byte[] rc4Key = new byte[16];
  28.             byte[] rc4Ciphertext = EncryptRC4(rc4Plaintext, rc4Key);
  29.             string decryptedRc4Plaintext = DecryptRC4(rc4Ciphertext, rc4Key);
  30.             Console.WriteLine("RC4 plaintext: " + rc4Plaintext);
  31.             Console.WriteLine("RC4 ciphertext: " + Convert.ToBase64String(rc4Ciphertext));
  32.             Console.WriteLine("Decrypted RC4 plaintext: " + decryptedRc4Plaintext);
  33.             #endregion
  34.             #region 哈希算法示例
  35.             // MD5 示例
  36.             string md5Plaintext = "Hello, MD5!";
  37.             string md5Hash = CalculateMD5Hash(md5Plaintext);
  38.             Console.WriteLine("MD5 hash of 'Hello, MD5!': " + md5Hash);
  39.             // SHA1 示例
  40.             string sha1Plaintext = "Hello, SHA1!";
  41.             string sha1Hash = CalculateSHA1Hash(sha1Plaintext);
  42.             Console.WriteLine("SHA1 hash of 'Hello, SHA1!': " + sha1Hash);
  43.             // SHA256 示例
  44.             string sha256Plaintext = "Hello, SHA256!";
  45.             string sha256Hash = CalculateSHA256Hash(sha256Plaintext);
  46.             Console.WriteLine("SHA256 hash of 'Hello, SHA256!': " + sha256Hash);
  47.             #endregion
  48.         }
  49.         #region AES加密解密示例
  50.         /// <summary>
  51.         /// AES 加密方法
  52.         /// </summary>
  53.         /// <param name="plaintext">plaintext</param>
  54.         /// <param name="key">key</param>
  55.         /// <param name="iv">iv</param>
  56.         /// <returns></returns>
  57.         public static byte[] EncryptAES(string plaintext, byte[] key, byte[] iv)
  58.         {
  59.             IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/PKCS7Padding");
  60.             cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES"key), iv));
  61.             return cipher.DoFinal(System.Text.Encoding.UTF8.GetBytes(plaintext));
  62.         }
  63.         /// <summary>
  64.         /// AES 解密方法
  65.         /// </summary>
  66.         /// <param name="ciphertext">ciphertext</param>
  67.         /// <param name="key">key</param>
  68.         /// <param name="iv">iv</param>
  69.         /// <returns></returns>
  70.         public static string DecryptAES(byte[] ciphertext, byte[] key, byte[] iv)
  71.         {
  72.             IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CTR/PKCS7Padding");
  73.             cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("AES"key), iv));
  74.             byte[] plaintext = cipher.DoFinal(ciphertext);
  75.             return System.Text.Encoding.UTF8.GetString(plaintext);
  76.         }
  77.         #endregion
  78.         #region DES 加密解密示例
  79.         /// <summary>
  80.         /// DES 加密方法
  81.         /// </summary>
  82.         /// <param name="plaintext">plaintext</param>
  83.         /// <param name="key">key</param>
  84.         /// <param name="iv">iv</param>
  85.         /// <returns></returns>
  86.         public static byte[] EncryptDES(string plaintext, byte[] key, byte[] iv)
  87.         {
  88.             IBufferedCipher cipher = CipherUtilities.GetCipher("DES/CBC/PKCS7Padding");
  89.             cipher.Init(true, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("DES"key), iv));
  90.             return cipher.DoFinal(System.Text.Encoding.UTF8.GetBytes(plaintext));
  91.         }
  92.         /// <summary>
  93.         /// DES 解密方法
  94.         /// </summary>
  95.         /// <param name="ciphertext">ciphertext</param>
  96.         /// <param name="key">key</param>
  97.         /// <param name="iv">iv</param>
  98.         /// <returns></returns>
  99.         public static string DecryptDES(byte[] ciphertext, byte[] key, byte[] iv)
  100.         {
  101.             IBufferedCipher cipher = CipherUtilities.GetCipher("DES/CBC/PKCS7Padding");
  102.             cipher.Init(false, new ParametersWithIV(ParameterUtilities.CreateKeyParameter("DES"key), iv));
  103.             byte[] plaintext = cipher.DoFinal(ciphertext);
  104.             return System.Text.Encoding.UTF8.GetString(plaintext);
  105.         }
  106.         #endregion
  107.         #region RC4 加密解密示例
  108.         /// <summary>
  109.         /// RC4 加密方法
  110.         /// </summary>
  111.         /// <param name="plaintext">plaintext</param>
  112.         /// <param name="key">key</param>
  113.         /// <returns></returns>
  114.         public static byte[] EncryptRC4(string plaintext, byte[] key)
  115.         {
  116.             IStreamCipher cipher = new RC4Engine();
  117.             cipher.Init(true, new KeyParameter(key));
  118.             byte[] data = System.Text.Encoding.UTF8.GetBytes(plaintext);
  119.             byte[] ciphertext = new byte[data.Length];
  120.             cipher.ProcessBytes(data0data.Length, ciphertext, 0);
  121.             return ciphertext;
  122.         }
  123.         /// <summary>
  124.         /// RC4 解密方法
  125.         /// </summary>
  126.         /// <param name="ciphertext">ciphertext</param>
  127.         /// <param name="key">key</param>
  128.         /// <returns></returns>
  129.         public static string DecryptRC4(byte[] ciphertext, byte[] key)
  130.         {
  131.             IStreamCipher cipher = new RC4Engine();
  132.             cipher.Init(false, new KeyParameter(key));
  133.             byte[] plaintext = new byte[ciphertext.Length];
  134.             cipher.ProcessBytes(ciphertext, 0, ciphertext.Length, plaintext, 0);
  135.             return System.Text.Encoding.UTF8.GetString(plaintext);
  136.         }
  137.         #endregion
  138.         #region 哈希算法示例
  139.         /// <summary>
  140.         /// 计算 MD5 哈希
  141.         /// </summary>
  142.         /// <param name="input">input</param>
  143.         /// <returns></returns>
  144.         public static string CalculateMD5Hash(string input)
  145.         {
  146.             IDigest digest = new MD5Digest();
  147.             byte[] hash = new byte[digest.GetDigestSize()];
  148.             byte[] data = System.Text.Encoding.UTF8.GetBytes(input);
  149.             digest.BlockUpdate(data0data.Length);
  150.             digest.DoFinal(hash, 0);
  151.             return Convert.ToBase64String(hash);
  152.         }
  153.         /// <summary>
  154.         /// 计算 SHA1 哈希
  155.         /// </summary>
  156.         /// <param name="input">input</param>
  157.         /// <returns></returns>
  158.         public static string CalculateSHA1Hash(string input)
  159.         {
  160.             IDigest digest = new Sha1Digest();
  161.             byte[] hash = new byte[digest.GetDigestSize()];
  162.             byte[] data = System.Text.Encoding.UTF8.GetBytes(input);
  163.             digest.BlockUpdate(data0data.Length);
  164.             digest.DoFinal(hash, 0);
  165.             return Convert.ToBase64String(hash);
  166.         }
  167.         /// <summary>
  168.         /// 计算 SHA256 哈希
  169.         /// </summary>
  170.         /// <param name="input">input</param>
  171.         /// <returns></returns>
  172.         public static string CalculateSHA256Hash(string input)
  173.         {
  174.             IDigest digest = new Sha256Digest();
  175.             byte[] hash = new byte[digest.GetDigestSize()];
  176.             byte[] data = System.Text.Encoding.UTF8.GetBytes(input);
  177.             digest.BlockUpdate(data0data.Length);
  178.             digest.DoFinal(hash, 0);
  179.             return Convert.ToBase64String(hash);
  180.         }
  181.         #endregion
  182.     }

输出结果:

图片

项目源码地址

更多项目实用功能和特性欢迎前往项目开源地址查看

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/269259
推荐阅读
相关标签