.net用BouncyCastle进行签名&加解密



http://www.bouncycastle.org/csharp/index.html

下载bin的zip文件。


工程添加引用“BouncyCastle.Crypto.dll”



代码添加引用:

using Org.BouncyCastle.Pkcs;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;



签名&读证书序列号:

            FileStream fileStream = null;
            try
            {
                fileStream = new FileStream(certPath, FileMode.Open);
                Pkcs12Store store = new Pkcs12Store(fileStream, certPwd.ToCharArray());

                string pName = null;
                foreach (string n in store.Aliases)
                {
                    if (store.IsKeyEntry(n))
                    {
                        pName = n;
                        //break;
                    }
                }

                Cert signCert = new Cert();
                AsymmetricKeyParameter key = store.GetKey(pName).Key;
                X509CertificateEntry[] chain = store.GetCertificateChain(pName);
                X509Certificate cert = chain[0].Certificate;
                string certId = cert.SerialNumber.ToString(); //证书序列号

                ISigner normalSig = SignerUtilities.GetSigner("SHA1WithRSA");
                normalSig.Init(true, key);
                normalSig.BlockUpdate(data, 0, data.Length);
                byte[] normalResult = normalSig.GenerateSignature(); //签名结果
                
            
            }
            finally
            {
                if (fileStream != null)
                    fileStream.Close();
            }


验签&读证书序列号:

                FileStream fileStream = null;
                try
                {
                    fileStream = new FileStream(file.DirectoryName + "\\" + file.Name, FileMode.Open);
                    X509Certificate certificate = new X509CertificateParser().ReadCertificate(fileStream);
                    string certId = certificate.SerialNumber.ToString(); //证书序列号
                    AsymmetricKeyParameter key = certificate.GetPublicKey();

                    ISigner verifier = SignerUtilities.GetSigner("SHA1WithRSA");
                    verifier.Init(false, key);
                    verifier.BlockUpdate(srcByte, 0, srcByte.Length);
                    return verifier.VerifySignature(signature); //验签结果

                }
                finally
                {
                    if(fileStream != null)
                        fileStream.Close();
                }



加密:

                FileStream fileStream = null;
                try
                {
                    fileStream = new FileStream(file.DirectoryName + "\\" + file.Name, FileMode.Open);
                    X509Certificate certificate = new X509CertificateParser().ReadCertificate(fileStream);
                    string certId = certificate.SerialNumber.ToString(); //证书序列号
                    AsymmetricKeyParameter key = certificate.GetPublicKey();

                    IBufferedCipher c = CipherUtilities.GetCipher("RSA/NONE/PKCS1Padding");
                    c.Init(true, new ParametersWithRandom(key, new SecureRandom()));
                    return c.DoFinal(dataToBeEnc); //加密结果

                }
                finally
                {
                    if(fileStream != null)
                        fileStream.Close();
                }




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
BouncyCastle 是一个流行的开源加密库,提供了多种加密算法和安全协议的实现。.NET Core 也可以使用 BouncyCastle 库来实现加密和解密的功能。 在 .NET Core 中使用 BouncyCastle 库,需要先安装 BouncyCastle NuGet 包。可以通过 Visual Studio 中的 NuGet 包管理器搜索并安装,也可以通过命令行使用 dotnet 命令进行安装: ``` dotnet add package BouncyCastle ``` 安装完成后,就可以在代码中使用 BouncyCastle 库提供的加密和解密方法了。以下是一个使用 BouncyCastle进行 AES 加密和解密的示例代码: ```csharp using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.Engines; using Org.BouncyCastle.Crypto.Modes; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; public class AesCipher { private readonly byte[] _key; private readonly byte[] _iv; public AesCipher(byte[] key, byte[] iv) { _key = key; _iv = iv; } public byte[] Encrypt(byte[] data) { IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7Padding"); cipher.Init(true, new ParametersWithIV(new KeyParameter(_key), _iv)); return cipher.DoFinal(data); } public byte[] Decrypt(byte[] data) { IBufferedCipher cipher = CipherUtilities.GetCipher("AES/CBC/PKCS7Padding"); cipher.Init(false, new ParametersWithIV(new KeyParameter(_key), _iv)); return cipher.DoFinal(data); } } ``` 在上面的示例代码中,我们使用BouncyCastle 库提供的 `IBufferedCipher` 接口来进行加密和解密。`CipherUtilities.GetCipher` 方法可以根据指定的算法名称获取对应的 `IBufferedCipher` 实例。在初始化 `IBufferedCipher` 实例时,我们需要传入加密/解密的模式、密钥和初始化向量等参数。在加密/解密时,我们调用 `DoFinal` 方法来执行加密/解密操作,并返回加密/解密后的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值