C#使用Bouncy Castle验证标志数据

我正在尝试使用Bouncy Castle签名并验证C#中的原始数据。我经历了我在google上发现的几乎所有内容,但似乎并不能解决我的问题。请看看我的方法。

public class Crypto2
{
    private static RsaKeyParameters MakeKey(String modulusHexString,String exponentHexString,bool isPrivateKey)
    {
        var modulus = new Org.BouncyCastle.Math.BigInteger(modulusHexString);
        var exponent = new Org.BouncyCastle.Math.BigInteger(exponentHexString);

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

    public static 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,bytes.Length);
        byte[] signature = sig.GenerateSignature();

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

        return signedString;
    }

    public static 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,msgBytes.Length);
        return signer.VerifySignature(expectedSig);
    }

    protected void Page_Load(object sender,EventArgs e)
    {
        string data = "Hello World";

        string privateKeyPath = system.web.HttpContext.Current.Server.MapPath("~/App_Data/e-MOne-i.key");
        string publicKeyPath = system.web.HttpContext.Current.Server.MapPath("~/App_Data/m1pay-fpx.cer");

        var key = readPrivateKey(privateKeyPath);

        var publicKey = ReadCertificate(publicKeyPath);

        var SignedData = Crypto2.Sign(data,((RsaKeyParameters)key.Private).modulus.ToString(),((RsaKeyParameters)key.Private).Exponent.ToString());

        bool result = Crypto2.Verify(data,SignedData,((RsaKeyParameters)publicKey.GetPublicKey()).modulus.ToString(),((RsaKeyParameters)publicKey.GetPublicKey()).Exponent.ToString());
    }

    static AsymmetricCipherKeyPair readPrivateKey(string privateKeyFileName)
    {
        AsymmetricCipherKeyPair keyPair;

        using (var reader = File.OpenText(privateKeyFileName))
            keyPair = (AsymmetricCipherKeyPair)new PemReader(reader).ReadObject();

        return keyPair;
    }

    static X509Certificate ReadCertificate(string filename)
    {
        X509CertificateParser certParser = new X509CertificateParser();

        Stream stream = new FileStream(filename,FileMode.Open);
        X509Certificate cert = certParser.ReadCertificate(stream);
        stream.Close();

        return cert;
    }
}
kjhsdgfhkjshfuwerytu 回答:C#使用Bouncy Castle验证标志数据

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3168117.html

大家都在问