简体   繁体   English

带有SHA256的RSACryptoServiceProvider.SignHash返回128个字节而不是32个字节?

[英]RSACryptoServiceProvider.SignHash with SHA256 returns 128 bytes instead of 32?

I'm new to cryptography, so perhaps I'm confused on what should happen here. 我是密码技术的新手,所以也许我对这里应该发生的事情感到困惑。 I start with a 32 byte message digest and attempt to sign it using my private key. 我从一个32字节的消息摘要开始,然后尝试使用我的私钥对其进行签名。 The output of RSA.SignHash is a 128 bytes. RSA.SignHash的输出为128个字节。 I am expecting 32. 我希望32。

    static private byte[] RSAHashAndSignData(byte[] data, RSAParameters privateKey)
    {
        byte[] signedHash;
        //Create a new instance of RSACryptoServiceProvider.
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
        {
            //Import the public key 
            RSA.ImportParameters(privateKey);

            //Encrypt the passed byte array
            var sha = SHA256Managed.Create();
            var digest = sha.ComputeHash(data);
            signedHash = RSA.SignHash(digest, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
        }

        return signedHash;
    }

I am expecting 32. 我希望32。

That is an incorrect assumption. 那是一个错误的假设。 Signing does not produce a size equal to the size of the input. 签名产生的大小不等于输入的大小。

Using PKCS#1 padding, using a 1024-bit RSA key will produce as 128-byte signature. 使用PKCS#1填充,使用1024位RSA密钥将产生128字节的签名。 The size of a signature is related to the size of the modulus of the signing key. 签名的大小与签名密钥的模数的大小有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM