myloveCc / NETCore.Encrypt

NETCore encrypt and decrypt tool,Include aes,des,rsa,md5,sha1,sha256,sha384,sha512
MIT License
614 stars 155 forks source link

rsa 异常 #35

Closed leyou240 closed 4 years ago

leyou240 commented 4 years ago

请问我的使用方式有问题吗?异常如下 Internal.Cryptography.CryptoThrowHelper+WindowsCryptographicException : 该项不存在。 at System.Security.Cryptography.CngKeyLite.ExportKeyBlob(SafeNCryptKeyHandle keyHandle, String blobType) at System.Security.Cryptography.RSAImplementation.RSACng.ExportKeyBlob(Boolean includePrivateParameters) 代码如下

[Fact(DisplayName = "Rsa from pem test")]
        public void Rsa_From_Pem_Test()
        {
            //Act
            var pemPublicKey = @"
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCxnBvS8cdsnAev2sRDRYWxznm1
QxZzaypfNXLvK7CDGk8TR7K+Pzsa+tpJfoyN/Z4B6xdlpsERo2Cu6AzolvrDLx5w
ZoI0kgdfaBMbUkdOB1m97zFYjKWoPeTskFzWZ3GHcQ3EXT0NJXXFXAskY45vEpbc
5qFgEhcPy3BMqHRibwIDAQAB
-----END PUBLIC KEY-----";

            var rsaFromPublickPem = EncryptProvider.RSAFromPem(pemPublicKey);

            var pemPrivateKey = @"
-----BEGIN RSA PRIVATE KEY-----
MIICWwIBAAKBgQCxnBvS8cdsnAev2sRDRYWxznm1QxZzaypfNXLvK7CDGk8TR7K+
Pzsa+tpJfoyN/Z4B6xdlpsERo2Cu6AzolvrDLx5wZoI0kgdfaBMbUkdOB1m97zFY
jKWoPeTskFzWZ3GHcQ3EXT0NJXXFXAskY45vEpbc5qFgEhcPy3BMqHRibwIDAQAB
AoGAAdwpqm7fxh0S3jOYpJULeQ45gL11dGX7Pp4CWHYzq1vQ14SDtFxYfnLWwGLz
499zvSoSHP1pvjPgz6lxy9Rw8dUxCgvh8VQydMQzaug2XD1tkmtcSWInwFKBAfQ7
rceleyD0aK8JHJiuzM1p+yIJ/ImGK0Zk2U/svqrdJrNR4EkCQQDo3d5iWcjd3OLD
38k1GALEuN17KNpJqLvJcIEJl0pcHtOiNnyy2MR/XUghDpuxwhrhudB/TvX4tuI0
MUeVo5fjAkEAw0D6m9jkwE5uuEYN/l/84rbQ79p2I7r5Sk6zbMyBOvgl6CDlJyxY
434DDm6XW7c55ALrnlratEW5HPiPxuHZBQJANnE4vtGy7nvn4Fd/mRQmAYwe695f
On1iefP9lxpx3huu6uvGN6IKPqS2alQZ/nMdCc0Be+IgC6fmNsGWtNtsdQJAJvB4
ikgxJqD9t8ZQ2CAwgM5Q0OTSlsGdIdKcOeB3DVmbxbV5vdw8RfJFjcVEbkgWRYDH
mKcp4rXc+wgfNFyqOQJATZ1I5ER8AZAn5JMMH9zK+6oFvhLUgKyWO18W+dbcFrBd
AzlTB+HHYEIyTmaDtXWAwgBvJNIHk4BbM1meCH4QnA==
-----END RSA PRIVATE KEY-----";

            var rsaFromPrivatePem = EncryptProvider.RSAFromPem(pemPrivateKey);

            //Assert
            Assert.NotNull(rsaFromPublickPem);
            Assert.NotNull(rsaFromPublickPem);
            Assert.Equal(rsaFromPublickPem.KeySize, rsaFromPrivatePem.KeySize);
            var privateKey= EncryptProvider.CreateRsaKey(rsaFromPrivatePem);
            var publicKey = EncryptProvider.CreateRsaKey(rsaFromPublickPem);
            var raw = "123123124";
            var signStr = EncryptProvider.RSASign(raw, privateKey.PrivateKey);
            var result = EncryptProvider.RSAVerify(signStr, signStr, publicKey.PublicKey);
            Assert.True(result);
leyou240 commented 4 years ago

修改此方法运行成功!我提个pr

/// <summary>
        /// Create an RSA key 
        /// </summary>
        /// <param name="rsa">rsa</param>
        /// <param name="includePrivate"></param>
        /// <returns></returns>
        public static RSAKey CreateRsaKey(RSA rsa, bool includePrivate = true)
        {
            Check.Argument.IsNotNull(rsa, nameof(rsa));

            string publicKey = rsa.ToJsonString(false);

            var rsaKey = new RSAKey()
            {
                PublicKey = publicKey,

                Exponent = rsa.ExportParameters(false).Exponent.ToHexString(),
                Modulus = rsa.ExportParameters(false).Modulus.ToHexString()
            };

            if (includePrivate)
            {
                string privateKey = rsa.ToJsonString(true);
                rsaKey.PrivateKey = privateKey;
            }
            return rsaKey;
        }
myloveCc commented 4 years ago

非常感谢,已经合并了。最近忙于其他项目,这边来看的少了,抱歉抱歉。