最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中免費(fèi)密碼庫(kù)BouncyCastle的使用詳解

 更新時(shí)間:2024年03月13日 11:37:21   作者:追逐時(shí)光者  
這篇文章主要來和大家分享一個(gè)C#版開源、免費(fèi)的Bouncy?Castle密碼庫(kù):BouncyCastle,文中介紹了BouncyCastle的具體使用,需要的可以參考下

前言

今天給大家分享一款C#版開源、免費(fèi)的Bouncy Castle密碼庫(kù):BouncyCastle。

項(xiàng)目介紹

BouncyCastle是一款C#版開源、免費(fèi)的Bouncy Castle密碼庫(kù),開發(fā)人員可以通過該項(xiàng)目在他們的 C# 應(yīng)用程序中使用 Bouncy Castle 提供的各種密碼學(xué)功能,從而加強(qiáng)數(shù)據(jù)的安全性和保護(hù)隱私信息。

Bouncy Castle介紹

Bouncy Castle是一個(gè)流行的密碼學(xué)庫(kù),提供了廣泛的密碼算法和協(xié)議的實(shí)現(xiàn)(包括對(duì)稱加密、非對(duì)稱加密、哈希函數(shù)、數(shù)字簽名等)。它由澳大利亞注冊(cè)的慈善組織“Bouncy Castle軍團(tuán)”開發(fā),旨在提供可靠而安全的加密解決方案。

項(xiàng)目源代碼

創(chuàng)建控制臺(tái)應(yīng)用

創(chuàng)建一個(gè)名為:BouncyCastleExercise的控制臺(tái)。

安裝BouncyCastle包

搜索名為:BouncyCastle.Cryptography包安裝:

BouncyCastle使用示例

  internal class Program
    {
        static void Main(string[] args)
        {
            #region AES加密解密示例

            string aesPlaintext = "Hello, 追逐時(shí)光者?。?!";
            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>
        /// 計(jì)算 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>
        /// 計(jì)算 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>
        /// 計(jì)算 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

    }

輸出結(jié)果:

項(xiàng)目源碼地址

https://github.com/bcgit/bc-csharp

以上就是C#中免費(fèi)密碼庫(kù)BouncyCastle的使用詳解的詳細(xì)內(nèi)容,更多關(guān)于C# BouncyCastle的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C#+MO實(shí)現(xiàn)一個(gè)道路編輯軟件(剛開始)

    C#+MO實(shí)現(xiàn)一個(gè)道路編輯軟件(剛開始)

    C#+MO實(shí)現(xiàn)一個(gè)道路編輯軟件(剛開始)...
    2007-04-04
  • C#中datatable去重的方法

    C#中datatable去重的方法

    這篇文章主要介紹了C#中datatable去重的方法,通過兩種不同的方法對(duì)比分析了datatable去重的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • 詳解C#之委托

    詳解C#之委托

    這篇文章主要介紹了C#委托的含義以及用法,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí)
    2020-06-06
  • C# JsonHelper 操作輔助類,拿來直接用

    C# JsonHelper 操作輔助類,拿來直接用

    本文總結(jié)了一些常用的JSON操作輔助類,包括轉(zhuǎn)換、判斷、Ajax異步等操作,希望能幫到大家。
    2016-05-05
  • C#實(shí)現(xiàn)批量重命名文件的項(xiàng)目實(shí)踐

    C#實(shí)現(xiàn)批量重命名文件的項(xiàng)目實(shí)踐

    文件重命名是一個(gè)在IT行業(yè)中常見的任務(wù),它涉及到對(duì)大量文件進(jìn)行系統(tǒng)性的命名更改,以便于更有效的文件管理和數(shù)據(jù)檢索,下面就來詳細(xì)的介紹一下C#實(shí)現(xiàn)批量重命名文件,感興趣的可以了解一下
    2026-05-05
  • 最新評(píng)論

    和政县| 南丹县| 方正县| 宁德市| 鹤庆县| 库尔勒市| 安化县| 纳雍县| 宽甸| 多伦县| 凤台县| 抚松县| 金乡县| 虞城县| 苏尼特左旗| 宜黄县| 高唐县| 文山县| 冷水江市| 思南县| 湖北省| 北流市| 麻城市| 诏安县| 鄂托克前旗| 商都县| 天水市| 稷山县| 凤山市| 郸城县| 当阳市| 泊头市| 广汉市| 庄河市| 长白| 丹棱县| 元阳县| 阜阳市| 昆明市| 定襄县| 兴安县|