首页 > 编程语言 >C#版开源免费的Bouncy Castle密码库

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

时间:2024-03-13 10:58:16浏览次数:14  
标签:string C# plaintext Bouncy cipher key new byte 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

    }

输出结果:

项目源码地址

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

标签:string,C#,plaintext,Bouncy,cipher,key,new,byte,Castle
From: https://www.cnblogs.com/mq0036/p/18070141

相关文章

  • 41_Docker网络
    Docker网络常用命令dockerhelpnetworkconnectConnectacontainertoanetworkcreateCreateanetworkdisconnectDisconnectacontainerfromanetworkinspectDisplaydetailedinformationononeormorenetworksls......
  • 40_DockerFile简介
    Dockerfile简介常用保留字FROM基础镜像,当前新镜像是基于哪个镜像的,指定一个已经存在的镜像作为模板,第一条必须是FROMMAINTAINER镜像维护者的姓名和邮箱地址RUN容器构建时需要运行的命令,有两种格式RUNyum-yinstallvimRUN[".......
  • docker-compose 部署gitlab
    主机ip:192.168.1.139[root@localhostgitlab_docker]#catdocker-compose.ymlversion:'3.1'services:gitlab:image:'gitlab/gitlab-ce:latest'container_name:gitlab#随着docker重启自动启动restart:alwaysenvironment:......
  • 43_Docker可视化工具
    portainer安装dockerrun-d-p8000:8000-p9000:9000\--nameportainer\--restart=always\-v/var/run/docker.sock:/var/run/docker.sock\-vportainer_data:/data\portainer/portainer#IP:9000进入容器监控三剑客docker-comp......
  • 42_Docker容器编排
    下载安装docker-composecurl-L"https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-$(uname-s)-$(uname-m)"-o/usr/local/bin/docker-compose#curl-L"https://get.daocloud.io/docker/compose/releases/download/v2.17.2/d......
  • linux Shell 命令行-07-func 函数
    拓展阅读linuxShell命令行-00-intro入门介绍linuxShell命令行-02-var变量linuxShell命令行-03-array数组linuxShell命令行-04-operator操作符linuxShell命令行-05-test验证是否符合条件linuxShell命令行-06-flowcontrol流程控制linuxShell命令行-07-f......
  • 数据收发卡设计方案:428-基于XC7Z100+ADRV9009的双收双发无线电射频板卡 5G小基站 无线
    基于XC7Z100+ADRV9009的双收双发无线电射频板卡一、板卡概述    基于XC7Z100+ADRV9009的双收双发无线电射频板卡是基于Xilinx ZYNQ FPGA和ADI的无线收发芯片ADRV9009开发的专用功能板卡,用于5G小基站,无线图传,数据收发等领域。 二、板卡原理及功能   板卡使用XC......
  • KeyError: 'Cache only has 0 layers, attempted to access layer with index 0'
    Traceback(mostrecentcalllast):File"/disk2/xiaoming/Github/ChatHxk/chinese_alpaca_2_7b_16k_hf/hxk_demo.py",line345,ingentaskret=self.mfunc(callback=_callback,**self.kwargs)File"/disk2/xiaoming/Github/ChatHxk/chinese_alpa......
  • MBR4060DC-ASEMI光伏专用二极管MBR4060DC
    编辑:llMBR4060DC-ASEMI光伏专用二极管MBR4060DC型号:MBR4060DC品牌:ASEMI封装:TO-263最大平均正向电流(IF):40A最大循环峰值反向电压(VRRM):60V最大正向电压(VF):V工作温度:-65°C~150°C反向恢复时间:ns重量:1.38克芯片个数:4芯片尺寸:130mil正向浪涌电流(IFMS):300AMBR4060DC特性:耐压......
  • Assetbundle.Unload(true)卸载资源时没调用ScriptableObject的OnDisable
    1)Assetbundle.Unload(true)卸载资源时没调用ScriptableObject的OnDisable2)UnityVolumeManager中ReplaceData如何优化3)关于使用Addressable的资源放在远程服务器后的下载问题4)Prefab对DLL中脚本的引用丢失这是第377篇UWA技术知识分享的推送,精选了UWA社区的热门话题,涵盖了UWA问答......