带导入密钥的RSA C#

时间:2012-08-14 11:52:06

标签: c# key rsa

我正在尝试导入RSAParameters,使用我的私钥和开放密钥来签名哈希,但我总是会收到错误。

使用方法CryptographicException

rsa.ImportParameters

我的代码:

static void Main(string[] args)
        {
            byte[] signedBytes;
            byte[] sha1 = new byte[] { 0x3D, 0x7E, 0x0C, 0x3B, 0x2B, 0x3F, 0xB4, 0x76, 0x99, 0x5E, 
                                       0x7B, 0x96, 0xA8, 0x95, 0xF9, 0x58, 0xB5, 0xC2, 0x91, 0xFA };

            using (var rsa = new RSACryptoServiceProvider(1024, new CspParameters(1)))
            {
                RSAParameters private_key = new RSAParameters();
                private_key.D = System.Text.Encoding.ASCII.GetBytes(D); // all data is ASCII string(not hex)
                private_key.DP = System.Text.Encoding.ASCII.GetBytes(DP);
                private_key.DQ = System.Text.Encoding.ASCII.GetBytes(DQ);
                private_key.P = System.Text.Encoding.ASCII.GetBytes(P);
                private_key.Q = System.Text.Encoding.ASCII.GetBytes(Q);
                private_key.Exponent = System.Text.Encoding.ASCII.GetBytes(E);
                private_key.InverseQ = System.Text.Encoding.ASCII.GetBytes(E);
                private_key.Modulus = System.Text.Encoding.ASCII.GetBytes(N);
                rsa.ImportParameters(private_key); // CryptographicException always!

                signedBytes = rsa.SignHash(sha1, CryptoConfig.MapNameToOID("SHA1"));
                Console.WriteLine(BitConverter.ToString(signedBytes, 0).Replace("-", ""));
            }
        }

你能说出为什么会这样吗?我在我的C ++应用程序中使用此键D,DP,E,N,一切正常!

0 个答案:

没有答案