1

我在尝试使用指定的私钥解密 RSA 加密的字节数组时遇到了这个问题:基本上,在调用 .Decrypt() 方法之后,我得到的只是一个空字节数组(字节 [0])。

这是代码:

static RSACryptoServiceProvider eng;
    static RSACryptoServiceProvider gne;

    static RSA()
    {
        eng = new RSACryptoServiceProvider();
        gne = new RSACryptoServiceProvider();
        eng.FromXmlString(xmlKKEY);
        gne.FromXmlString(xmlPKEY);
        eng.ImportParameters(eng.ExportParameters(true));
    }

    public static byte[] Encrypt(byte[] data)
    {
        return gne.Encrypt(data, false);
    }

    public static byte[] Decrypt(byte[] data)
    {
        byte[] d = eng.Decrypt(data, false);
        return d;
    }

由于请求,我已经包含了密钥 - 当时我使用 pepta.net 生成了它们:

    private readonly static string KKEY = @"
-----BEGIN RSA PRIVATE KEY-----
MIIBOQIBAAJBALrz8nKq+vpVBnZTNTM/jaRDRTdKPdS6c59+Fb4cEHF4Tj959EWH
dxpP0neCzXYeygJoVMjGshsOo59YoQXcGeUCAwEAAQJAMTauiAkCcIuZW+SjiI4e
VzNuMRPWGv3KGh4y0wzIN1YkvQvRgi9vTfY5AqGK1SRtlwcLuXJz2MyYUpcK5yJX
MQIhAOisz2NriiIHzBoklOVYKcx4fy8OdSUa70dFHWOs+jITAiEAzbHCoFBFXiIL
AZ2AUJojuNAtmktpCU1HK+42xA0+wycCIDmpAagAsXWnewQLhr6jL5CVV+dgbNVq
LFv8G8Fkz4fzAiBu8XXcKe+AOjRdhqnyz3B8+ivekNXyYnAEzdNUIyC6+QIgK4Rd
Y+ShRJK2nXZ5JUbz+FfwpzYYBVEaGg8dKK7nRcw=
-----END RSA PRIVATE KEY-----";

        private static readonly string xmlKKEY = "<RSAKeyValue><Modulus>uvPycqr6+lUGdlM1Mz+NpENFN0o91Lpzn34VvhwQcXhOP3n0RYd3Gk/Sd4LNdh7KAmhUyMayGw6jn1ihBdwZ5Q==</Modulus><Exponent>AQAB</Exponent><P>6KzPY2uKIgfMGiSU5VgpzHh/Lw51JRrvR0UdY6z6MhM=</P><Q>zbHCoFBFXiILAZ2AUJojuNAtmktpCU1HK+42xA0+wyc=</Q><DP>OakBqACxdad7BAuGvqMvkJVX52Bs1WosW/wbwWTPh/M=</DP><DQ>bvF13CnvgDo0XYap8s9wfPor3pDV8mJwBM3TVCMguvk=</DQ><InverseQ>K4RdY+ShRJK2nXZ5JUbz+FfwpzYYBVEaGg8dKK7nRcw=</InverseQ><D>MTauiAkCcIuZW+SjiI4eVzNuMRPWGv3KGh4y0wzIN1YkvQvRgi9vTfY5AqGK1SRtlwcLuXJz2MyYUpcK5yJXMQ==</D></RSAKeyValue>";

        private readonly static string PKEY = @"
-----BEGIN PUBLIC KEY-----
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALrz8nKq+vpVBnZTNTM/jaRDRTdKPdS6
c59+Fb4cEHF4Tj959EWHdxpP0neCzXYeygJoVMjGshsOo59YoQXcGeUCAwEAAQ==
-----END PUBLIC KEY-----";

        private static readonly string xmlPKEY = "<RSAKeyValue><Modulus>uvPycqr6+lUGdlM1Mz+NpENFN0o91Lpzn34VvhwQcXhOP3n0RYd3Gk/Sd4LNdh7KAmhUyMayGw6jn1ihBdwZ5Q==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";

提前致谢。

4

0 回答 0