【问题标题】:C# Sign Data with RSA using BouncyCastleC# 使用 BouncyCastle 使用 RSA 对数据进行签名
【发布时间】:2012-02-08 11:20:09
【问题描述】:

有谁知道如何使用充气城堡在 c# 中对数据进行签名的简单教程或示例代码。在 Java 中有大量的教程和示例。我在 c# 中找不到单个示例。有人知道怎么做吗?

【问题讨论】:

  • 您有什么理由要为此使用 bouncycastle 吗? .NET 有自己的 RSA 加密类。
  • 我想这是一个公平的问题。我过去在 Java 中使用过 BouncyCastle,并且知道它与我需要与之通信的后端的兼容性没有问题。 .Net 版本也可能如此,但它似乎不太灵活,并且在填充等方面的选择也更少。
  • 这是有道理的——尤其是如果您担心互操作性:)
  • @blowdart 内置的 RSA 加密不支持加载私钥,它强制捆绑在证书中,这不适合许多用途。

标签: c# bouncycastle


【解决方案1】:

好的,我找不到任何有关如何执行此操作的文档。但我最终弄清楚了。 我将完整的代码粘贴在这里,希望它可以帮助将来的人。

这个类将为提供的字符串计算一个带有 sha1 哈希的 RSA 签名并验证它。

using System;
using System.IO;
using System.Text;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.OpenSsl;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Utilities.Encoders;

namespace API.Crypto
{
    public class RsaSha1Signing
    {
        private RsaKeyParameters MakeKey(String modulusHexString, String exponentHexString, bool isPrivateKey)
        {
            var modulus = new Org.BouncyCastle.Math.BigInteger(modulusHexString, 16);
            var exponent = new Org.BouncyCastle.Math.BigInteger(exponentHexString, 16);

            return new RsaKeyParameters(isPrivateKey, modulus, exponent);
        }

        public String Sign(String data, String privateModulusHexString, String privateExponentHexString)
        {
            /* Make the key */
            RsaKeyParameters key = MakeKey(privateModulusHexString, privateExponentHexString, true);

            /* Init alg */
            ISigner sig = SignerUtilities.GetSigner("SHA1withRSA");

            /* Populate key */
            sig.Init(true, key);

            /* Get the bytes to be signed from the string */
            var bytes = Encoding.UTF8.GetBytes(data);

            /* Calc the signature */
            sig.BlockUpdate(bytes, 0, bytes.Length);
            byte[] signature = sig.GenerateSignature();

            /* Base 64 encode the sig so its 8-bit clean */
            var signedString = Convert.ToBase64String(signature);

            return signedString;
        }

        public bool Verify(String data, String expectedSignature, String publicModulusHexString, String publicExponentHexString)
        {
            /* Make the key */
            RsaKeyParameters key = MakeKey(publicModulusHexString, publicExponentHexString, false);

            /* Init alg */
            ISigner signer = SignerUtilities.GetSigner("SHA1withRSA");

            /* Populate key */
            signer.Init(false, key);

            /* Get the signature into bytes */
            var expectedSig = Convert.FromBase64String(expectedSignature);

            /* Get the bytes to be signed from the string */
            var msgBytes = Encoding.UTF8.GetBytes(data);

            /* Calculate the signature and see if it matches */
            signer.BlockUpdate(msgBytes, 0, msgBytes.Length);
            return signer.VerifySignature(expectedSig);
        }
    }
}

【讨论】:

  • 好吧,终于。有人发了明确的指示。非常感谢!
  • 谁能告诉我们什么是 privateModulusHexString 和 privateExponentHexString。我不喜欢充气城堡。
  • @Mallikarjun 这些是您用来签署和验证数据的密钥的一部分。您需要已经创建了一个密钥才能使用此代码。因此,例如,您的 privateModulusHexString 看起来像这样“14A1345B425C65457D856E3453F”...
  • @w.donahue 嗨,你能不能给我更多关于 privateModulusHexString 和 privateExponentHexString 的信息,我需要它,因为曲线 secp256r1 算法是 ECDSA。如果是,请告诉我从哪里获得 privateExponentHexString 和 privateExponentHexString 密钥是根据这些指令创建的 stackoverflow.com/questions/19466907/… 谢谢!
【解决方案2】:

查看 Bouncy Castle 网站。有源代码和示例的存档。 http://www.bouncycastle.org/csharp/download/bccrypto-net-1.7-src-ext.zip

例如,有很多 NUnit 测试。 以下是使用 RSA 算法加密数据字节数组的方法代码作为示例,但在 Bouncy Castle 源代码和测试中您可以找到更多示例。

    public static byte[] Encrypt(byte[] data, AsymmetricKeyParameter key)
    {
        RsaEngine e = new RsaEngine();
        e.Init(true, key);
        int blockSize = e.GetInputBlockSize();
        List<byte> output = new List<byte>();

        for (int chunkPosition = 0; chunkPosition < data.Length; chunkPosition += blockSize)
        {
            int chunkSize = Math.Min(blockSize, data.Length - (chunkPosition * blockSize));
            output.AddRange(e.ProcessBlock(data, chunkPosition, chunkSize));
        }
        return output.ToArray();
    }

【讨论】:

  • 它只是跟随我 - 链接以获取示例 - 不起作用吗?
猜你喜欢
  • 2014-01-13
  • 1970-01-01
  • 2021-05-14
  • 2017-12-05
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
  • 2018-07-21
相关资源
最近更新 更多