.net中加解密用BouncyCastle就够了,支持常用的各种加密解密算法

这篇具有很好参考价值的文章主要介绍了.net中加解密用BouncyCastle就够了,支持常用的各种加密解密算法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

BouncyCastle 是一个流行的 Java 加解密库,也支持在 .NET 平台上使用。下面是 BouncyCastle 在 .NET 下使用的一些常见功能,包括 AES、RSA、MD5、SHA1、DES、SHA256、SHA384、SHA512 等。

在开始之前,请确保你已经将 BouncyCastle 的 NuGet 包安装到你的项目中。你可以通过 NuGet 包管理器控制台或 Visual Studio 中的 NuGet 包管理器进行安装。

Install-Package BouncyCastle

接下来,我将演示如何使用 BouncyCastle 实现一些常见的加解密操作。

1. AES 加解密

using System;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Modes;
using Org.BouncyCastle.Crypto.Parameters;

public class AesExample
{
    public static byte[] Encrypt(string plaintext, byte[] key, byte[] iv)
    {
        CipherEngine engine = new CipherEngine();
        CipherParameters keyParam = new KeyParameter(key);
        ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv);

        engine.Init(true, keyParamWithIV);

        byte[] input = Encoding.UTF8.GetBytes(plaintext);
        byte[] output = new byte[engine.GetOutputSize(input.Length)];

        int len = engine.ProcessBytes(input, 0, input.Length, output, 0);
        engine.DoFinal(output, len);

        return output;
    }

    public static string Decrypt(byte[] ciphertext, byte[] key, byte[] iv)
    {
        CipherEngine engine = new CipherEngine();
        CipherParameters keyParam = new KeyParameter(key);
        ParametersWithIV keyParamWithIV = new ParametersWithIV(keyParam, iv);

        engine.Init(false, keyParamWithIV);

        byte[] output = new byte[engine.GetOutputSize(ciphertext.Length)];

        int len = engine.ProcessBytes(ciphertext, 0, ciphertext.Length, output, 0);
        engine.DoFinal(output, len);

        return Encoding.UTF8.GetString(output);
    }
}

// 示例用法
byte[] aesKey = new byte[16]; // AES 128-bit key
byte[] aesIV = new byte[16];  // AES 128-bit IV
string plaintext = "Hello, BouncyCastle!";

byte[] ciphertext = AesExample.Encrypt(plaintext, aesKey, aesIV);
string decryptedText = AesExample.Decrypt(ciphertext, aesKey, aesIV);

Console.WriteLine($"Plaintext: {plaintext}");
Console.WriteLine($"Ciphertext: {Convert.ToBase64String(ciphertext)}");
Console.WriteLine($"Decrypted Text: {decryptedText}");

2. RSA 加解密

using System;
using System.Text;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;

public class RsaExample
{
    public static byte[] Encrypt(string plaintext, AsymmetricKeyParameter publicKey)
    {
        CipherEngine engine = new CipherEngine();
        engine.Init(true, publicKey);

        byte[] input = Encoding.UTF8.GetBytes(plaintext);
        byte[] output = engine.ProcessBytes(input, 0, input.Length);

        return output;
    }

    public static string Decrypt(byte[] ciphertext, AsymmetricKeyParameter privateKey)
    {
        CipherEngine engine = new CipherEngine();
        engine.Init(false, privateKey);

        byte[] output = engine.ProcessBytes(ciphertext, 0, ciphertext.Length);

        return Encoding.UTF8.GetString(output);
    }
}

// 示例用法
RsaKeyPairGenerator rsaKeyPairGen = GeneratorUtilities.GetKeyPairGenerator("RSA");
rsaKeyPairGen.Init(new KeyGenerationParameters(new SecureRandom(), 2048)); // 2048-bit key size
AsymmetricCipherKeyPair keyPair = rsaKeyPairGen.GenerateKeyPair();

AsymmetricKeyParameter publicKey = keyPair.Public;
AsymmetricKeyParameter privateKey = keyPair.Private;

string plaintext = "Hello, BouncyCastle!";

byte[] ciphertext = RsaExample.Encrypt(plaintext, publicKey);
string decryptedText = RsaExample.Decrypt(ciphertext, privateKey);

Console.WriteLine($"Plaintext: {plaintext}");
Console.WriteLine($"Ciphertext: {Convert.ToBase64String(ciphertext)}");
Console.WriteLine($"Decrypted Text: {decryptedText}");

3. MD5、SHA1、SHA256、SHA384、SHA512

using System;
using System.Security.Cryptography;
using System.Text;
using Org.BouncyCastle.Crypto.Digests;

public class HashExample
{
    public static string ComputeMD5(string input)
    {
        MD5 md5 = MD5.Create();
        byte[] hashBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
        return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
    }

    public static string ComputeSHA1(string input)
    {
        SHA1 sha1 = SHA1.Create();
        byte[] hashBytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(input));
        return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
    }

    public static string ComputeSHA256(string input)
    {
        Sha256Digest sha256 = new Sha256Digest();
        byte[] inputBytes = Encoding.UTF8.GetBytes(input);
        sha256.BlockUpdate(inputBytes, 0, inputBytes.Length);

        byte[] hashBytes = new byte[sha256.GetDigestSize()];
        sha256.DoFinal(hashBytes, 0);

        return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
    }

    public static string ComputeSHA384(string input)
    {
        Sha384Digest sha384 = new Sha384Digest();
        byte[] inputBytes = Encoding.UTF8.GetBytes(input);
        sha384.BlockUpdate(inputBytes, 0, inputBytes.Length);

        byte[] hashBytes = new byte[sha384.GetDigestSize()];
        sha384.DoFinal(hashBytes, 0);

        return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
    }

    public static string ComputeSHA512(string input)
    {
        Sha512Digest sha512 = new Sha512Digest();
        byte[] inputBytes = Encoding.UTF8.GetBytes(input);
        sha512.BlockUpdate(inputBytes, 0, inputBytes.Length);

        byte[] hashBytes = new byte[sha512.GetDigestSize()];
        sha512.DoFinal(hashBytes, 0);

        return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
    }
}

// 示例用法
string input = "Hello, BouncyCastle!";
Console.WriteLine($"MD5: {HashExample.ComputeMD5(input)}");
Console.WriteLine($"SHA1: {HashExample.ComputeSHA1(input)}");
Console.WriteLine($"SHA256: {HashExample.ComputeSHA256(input)}");
Console.WriteLine($"SHA384: {HashExample.ComputeSHA384(input)}");
Console.WriteLine($"SHA512: {HashExample.ComputeSHA512(input)}");

这些示例展示了在 .NET 下使用 BouncyCastle 实现 AES、RSA、MD5、SHA1、SHA256、SHA384、SHA512 加解密的基本操作。

具体的实现细节可能根据 BouncyCastle 版本略有变化,建议查阅 BouncyCastle 的官方文档以获取最新信息。文章来源地址https://www.toymoban.com/news/detail-750155.html

到了这里,关于.net中加解密用BouncyCastle就够了,支持常用的各种加密解密算法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【重要】Poe订阅问题看这一篇就够了!Poe免费版和订阅版区别?解决Poe各种问题!最方便使用各类AI!

    Q:Poe是什么?Poe是什么玩意? A:Poe(Platform for Open Exploration)是一款由Quora于2022年12月推出的 AI聊天工具集成平台 ,汇集了众多AI聊天机器人,如G|P|T-3.5、G|P|T-4和Claude系列等。 Q:Poe有什么用?Poe能做些什么?Poe有哪些优势?Poe能干什么?Poe安卓手机上可以用吗?PoeIOS手机可

    2024年04月26日
    浏览(34)
  • 【Golang入门教程】Goland常用快捷键,看这一篇就够了

    强烈推荐 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站: 人工智能 前言 在进行Go语言开发时,熟练使用快捷键是提高效率、加快编码速度的关键。 Goland作为一款强大的集成开发环境(IDE),提供了丰富的快捷键

    2024年02月20日
    浏览(40)
  • 二维码生成解析用ZXing.NET就够了,不要再引一堆生成和解析库了

    ZXing.NET 是一个开源的、功能强大的二维码处理库,它能够对二维码进行解码(读取信息)和编码(生成二维码)。ZXing 是 \\\"Zebra Crossing\\\" 的缩写,是一个跨平台的、用于解码和生成条形码和二维码的库。以下是一些 ZXing.Net 的主要功能通过实例讲解。 这些示例演示了 ZXing.Net 的

    2024年02月04日
    浏览(27)
  • Python常用基础语法知识点大全合集,看完这一篇文章就够了

    Python 是一门独特的语言,快速浏览一下他的要点: 面向对象:每一个变量都是一个类,有其自己的属性(attribute)与方法(method)。 语法块:用缩进(四个空格)而不是分号、花括号等符号来标记。因此,行首的空格不能随意书写。 注释:行内用“#”号,行间注释写在两

    2023年04月22日
    浏览(40)
  • 解决国密SM2加解密部署到weblogic后bouncyCastle bcprov-jdk15on的包冲突

    1.报错内容 tried to access method org.bouncycastle.math.ec.ECPoint$Fp.init(Lorg/bouncycastle/math/ec/ECCurve;Lorg/bouncycastle/math/ec/ECFieldElement; Lorg/bouncycastle/math/ec/ECFieldElement;)V from class SM2Utils.SM2 at...... 2.分析          我的SpringBoot项目中,依赖bcprov-jdk15on-1.59版本jar包,本地编译运行都是没问题的

    2024年01月19日
    浏览(27)
  • 计组一篇就够了

    1.2.1计算机工作过程 1.2.4计算机性能指标 机器字长、指令字长、存储字长的区别和联系是什么? 机器字长:计算机能直接处理的二进制数据的位数,机器字长一般等于内部寄存器的大小, 它决定了计算机的运算精度 。 指令字长:一个指令字中包含的二进制代码的位数。 存

    2024年02月01日
    浏览(27)
  • 分库分表用这个就够了

    2018年写过一篇分库分表的文章《SpringBoot使用sharding-jdbc分库分表》,但是存在很多不完美的地方比如: sharding-jdbc的版本(1.4.2)过低,现在github上的最新版本都是5.3.2了,很多用法和API都过时了。 分库分表配置采用Java硬编码的方式不够灵活 持久层使用的是spring-boot-starter-d

    2024年02月08日
    浏览(27)
  • python入门,一篇就够了

    函数必须写注释:文档注释格式 \\\'\\\'\\\'注释内容\\\'\\\'\\\' 参数中的等号两边不要用空格 相邻函数用两个空行隔开 小写 + 下划线 函数名 模块名 实例名 驼峰法 类名 结构化类型,有一系列的属性和类型 标量类型,此对象无可访问的内部对象 python 中,整型相除默认是浮点型 建议:使用

    2024年02月15日
    浏览(25)
  • Spark入门(一篇就够了)

    声明 : 本文为大数据肌肉猿公众号的《5W字总结Spark》的学习笔记,如有侵权请联系本人删除! Spark 知识图谱如下: Spark 是当今大数据领域最活跃、最热门、最高效的大数据通用计算平台之一 。 Hadoop 之父 Doug Cutting 指出:Use of MapReduce engine for Big Data projects will decline, repla

    2024年02月03日
    浏览(26)
  • 搞懂flyaway一篇就够了

    Flyway是一个用于数据库迁移的开源工具,它可以帮助开发人员轻松地管理数据库架构的变化。Flyway通过迁移来更新数据库,迁移可以使用特定于数据库的SQL语法或者用于高级数据库转换的Java编写。Flyway支持两种类型的迁移:有版本的迁移和可重复的迁移。有版本的迁移具有唯

    2024年02月03日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包