用Portable.BouncyCastle来进行加解密的代码demo

这篇博客介绍了如何在C#中使用Portable.BouncyCastle库进行Des、MD5、SHA-1、SHA-256加密以及SignEnvelop签名操作。文中详细展示了加密和解密的代码示例,同时也提到了RSA加密的分段处理。该库提供了与Java类似的功能,方便在不同场景下进行加解密和签名。
摘要由CSDN通过智能技术生成

前言

这里对之前对接的公司中的代码demo做一个总结,原本为清一色的java,哈哈。这里都转成C#。用到的库是Portable.BouncyCastle官网。之前也是准备用.net core 内置的类,方法,但实际在用的时候比如因为desKey并不是特定长度的,导致抛了一些异常,于是就改用了这个库。

这个库中有如下这么一段的介绍:

The lightweight API works with everything from the J2ME to the JDK 1.7 and there is also an API in C# providing equivalent functionality for most of the above.

实际用下来也确实如此,方法基本上是“相同”名字的。

正文

Des

这是一种对称密钥加密算法,也就是说加密解密的秘钥是一样的。

  1. 加密
        public static string DesEncrypt(string dataXml, string desKey)
        {
            var keyParam = ParameterUtilities.CreateKeyParameter("DES", Convert.FromBase64String(desKey));
            var cipher = (BufferedBlockCipher) CipherUtilities.GetCipher("DES/NONE/PKCS5Padding");

            cipher.Init(true, keyParam);
            var bs = Encoding.UTF8.GetBytes(dataXml);
            var rst = cipher.DoFinal(bs);
            // var asciiBs = Encoding.ASCII.GetBytes(Encoding.UTF8.GetString(rst));
            return Convert.ToBase64String(rst);
        }

看方法签名,需要两个参数,1.要加密的内容,2.加密的秘钥。先用desKey作为参数创建一个keyParam。然后用DES/NONE/PKCS5Padding作为参数获取一个cipher,这个参数用来详细描述des加密时候的相关细节,具体是根据对方给的代码里面来的。然后对这个cipher做一个初始化,第一个参数true表示这个cipher是用来加密的,并且传入之前的keyParam。然后获取加密内容的字节数组,编码是utf-8,一般都是这个编码。然后调用cipher的DoFinal方法就能获取加密之后的内容了。最后一行转成了一个base64字符串。

通过这个方法就能对数据进行des加密了。中间也涉及了字符编码格式,base64转换的内容,这些是根据给的demo写的。

  1. 解密
        public static string DesDecrypt(string text, string desKey)
        {
            var keyParam = ParameterUtilities.CreateKeyParameter("DES", Convert.FromBase64String(desKey));
            var cipher = (BufferedBlockCipher) CipherUtilities.GetCipher("DES/NONE/PKCS5Padding");

            cipher.Init(false, keyParam);
            var bs = Convert.FromBase64String(text);
            var rst = cipher.DoFinal(bs);
            return Encoding.UTF8.GetString(rst);
        }

下面是解密方法。两个参数,1.需要解密的内容。 2.解密的key。这个key是和加密的时候一样的。首先也是通过desKey获取一个keyParam,然后用DE

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
■Generation and parsing of PKCS#12 files. ■X.509: Generators and parsers for V1 and V3 certificates, V2 CRLs and attribute certificates. ■PBE algorithms supported by PBEUtil: PBEwithMD2andDES-CBC, PBEwithMD2andRC2-CBC, PBEwithMD5andDES-CBC, PBEwithMD5andRC2-CBC, PBEwithSHA1andDES-CBC, PBEwithSHA1andRC2-CBC, PBEwithSHA-1and128bitRC4, PBEwithSHA-1and40bitRC4, PBEwithSHA-1and3-keyDESEDE-CBC, PBEwithSHA-1and2-keyDESEDE-CBC, PBEwithSHA-1and128bitRC2-CBC, PBEwithSHA-1and40bitRC2-CBC, PBEwithHmacSHA-1, PBEwithHmacSHA-224, PBEwithHmacSHA-256, PBEwithHmacRIPEMD128, PBEwithHmacRIPEMD160, and PBEwithHmacRIPEMD256. ■Signature algorithms supported by SignerUtilities: MD2withRSA, MD4withRSA, MD5withRSA, RIPEMD128withRSA, RIPEMD160withRSA, RIPEMD256withRSA, SHA-1withRSA, SHA-224withRSA, SHA-256withRSAandMGF1, SHA-384withRSAandMGF1, SHA-512withRSAandMGF1, SHA-1withDSA, and SHA-1withECDSA. ■Symmetric key algorithms: AES, Blowfish, Camellia, CAST5, CAST6, DESede, DES, GOST28147, HC-128, HC-256, IDEA, NaccacheStern, RC2, RC4, RC5-32, RC5-64, RC6, Rijndael, Serpent, Skipjack, TEA/XTEA, Twofish, and VMPC. ■Symmetric key modes: CBC, CFB, CTS, GOFB, OFB, OpenPGPCFB, and SIC (or CTR). ■Symmetric key paddings: ISO10126d2, ISO7816d4, PKCS#5/7, TBC, X.923, and Zero Byte. ■Asymmetric key algorithms: RSA (with blinding), ElGamal, DSA, ECDSA. ■Asymmetric key paddings/encodings: ISO9796d1, OAEP, and PKCS#1. ■Digests: GOST3411, MD2, MD4, MD5, RIPEMD128, RIPEMD160, RIPEMD256, RIPEMD320, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, Tiger, and Whirlpool. ■Signer mechanisms: DSA, ECDSA, ECGOST3410, GOST3410, ISO9796d2, PSS, RSA. ■Key Agreement: Diffie-Hellman and EC-DH. ■Macs: CBCBlockCipher, CFBBlockCipher, GOST28147, HMac, and ISO9797 Alg. 3. ■PBE generators: PKCS#12, and PKCS#5 - schemes 1 and 2. ■OpenPGP (RFC 4880) ■Cryptographic Message Syntax (CMS, RFC 3852), including streaming API. ■Online Certificate Status Protocol (OCSP, RFC 2560). ■Time Stamp Protocol (TSP, RFC 3161). ■TLS/SSL Client with support for client side authentication.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值