示例#1
0
        public void IntegrationTest()
        {
            var data = Guid.NewGuid().ToString();

            {
                var a1 = RSAHelper.LoadX509FromPublicCertificate(PublicKeyCer)
                         .GetRSAPublicKey().EncryptBigData(data, RSAEncryptionPadding.OaepSHA256);
                var a2 = RSAHelper.FromPem(PublicKeyPem)
                         .EncryptBigData(data, RSAEncryptionPadding.OaepSHA256);
                var a3 = RSAHelper.FromPem(PublicKeyPem)
                         .Encrypt(data, RSAEncryptionPadding.Pkcs1);
                var a4 = RSAHelper.FromPem(PublicKeyPem)
                         .EncryptBigDataWithSplit(data, RSAEncryptionPadding.Pkcs1);
                var d1 = RSAHelper.FromPem(PrivateKeyPkcs8)
                         .DecryptBigData(a1, RSAEncryptionPadding.OaepSHA256);
                var d28 = RSAHelper.FromPem(PrivateKeyPkcs8)
                          .DecryptBigData(a2, RSAEncryptionPadding.OaepSHA256);
                var d21 = RSAHelper.FromPem(PrivateKeyPkcs1)
                          .DecryptBigData(a1, RSAEncryptionPadding.OaepSHA256);
                var d3 = RSAHelper.FromPem(PrivateKeyPkcs1)
                         .Decrypt(a3, RSAEncryptionPadding.Pkcs1);
                var d4 = RSAHelper.FromPem(PrivateKeyPkcs1)
                         .DecryptBigDataWithSplit(a4, RSAEncryptionPadding.Pkcs1);

                d1.ShouldBe(d28);
                d1.ShouldBe(d21);
                d1.ShouldBe(d3);

                var signature = RSAHelper.FromPem(PrivateKeyPkcs8)
                                .SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pss);

                var signature1 = RSAHelper.FromPem(PrivateKeyPkcs1)
                                 .SignData(data, HashAlgorithmName.SHA256, RSASignaturePadding.Pss);

                RSAHelper.LoadX509FromPublicCertificate(PublicKeyCer)
                .GetRSAPublicKey().VerifySignData(data, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pss)
                .ShouldBe(true);

                RSAHelper.FromPem(PublicKeyPem)
                .VerifySignData(data, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pss)
                .ShouldBe(true);

                RSAHelper.LoadX509FromPublicCertificate(PublicKeyCer)
                .GetRSAPublicKey().VerifySignData(data, signature1, HashAlgorithmName.SHA256, RSASignaturePadding.Pss)
                .ShouldBe(true);

                RSAHelper.FromPem(PublicKeyPem)
                .VerifySignData(data, signature1, HashAlgorithmName.SHA256, RSASignaturePadding.Pss)
                .ShouldBe(true);
            }

            {
                using var rsa = RSAHelper.FromPem(PublicKeyPem);
                Should.Throw <Exception>(() => rsa.ToPem(true, true));
            }

            {
                // xml 私钥导出测试
                using var rsa = RSAHelper.FromPem(PrivateKeyPkcs8);
                var encrypted = rsa.EncryptBigData(data, RSAEncryptionPadding.OaepSHA256);
                var xml       = rsa.ToXml(true);
                _testOutputHelper.WriteLine("###############");
                _testOutputHelper.WriteLine(xml);
                using var rsa1 = RSAHelper.FromXml(xml);
                rsa1.DecryptBigData(encrypted, RSAEncryptionPadding.OaepSHA256).ShouldBe(data);
            }

            {
                // xml 公钥导出测试
                using var rsa = RSAHelper.FromPem(PrivateKeyPkcs8);
                var xml = rsa.ToXml(false);
                _testOutputHelper.WriteLine("###############");
                _testOutputHelper.WriteLine(xml);
                using var rsa1 = RSAHelper.FromXml(xml);
                var encrypted = rsa1.EncryptBigData(data, RSAEncryptionPadding.OaepSHA256);
                rsa.DecryptBigData(encrypted, RSAEncryptionPadding.OaepSHA256).ShouldBe(data);
            }

            {
                // pem pkcs1 导出导入测试
                using var rsa = RSAHelper.FromPem(PrivateKeyPkcs8);
                var encrypted = rsa.EncryptBigData(data, RSAEncryptionPadding.OaepSHA256);
                var pem       = rsa.ToPem(true, false);
                _testOutputHelper.WriteLine("###############");
                _testOutputHelper.WriteLine(pem);
                using var rsa1 = RSAHelper.FromPem(pem);
                rsa1.DecryptBigData(encrypted, RSAEncryptionPadding.OaepSHA256).ShouldBe(data);
            }

            {
                // pem pkcs1 导出导入测试
                using var rsa = RSAHelper.FromPem(PrivateKeyPkcs8);
                var pem = rsa.ToPem(false, false);
                _testOutputHelper.WriteLine("###############");
                _testOutputHelper.WriteLine(pem);
                using var rsa1 = RSAHelper.FromPem(pem);
                var encrypted = rsa1.EncryptBigData(data, RSAEncryptionPadding.OaepSHA256);
                rsa.DecryptBigData(encrypted, RSAEncryptionPadding.OaepSHA256).ShouldBe(data);
            }

            {
                // pem pkcs8 导出导入测试
                using var rsa = RSAHelper.FromPem(PrivateKeyPkcs8);
                var encrypted = rsa.EncryptBigData(data, RSAEncryptionPadding.OaepSHA256);
                var pem       = rsa.ToPem(true, true);
                _testOutputHelper.WriteLine("###############");
                _testOutputHelper.WriteLine(pem);
                using var rsa1 = RSAHelper.FromPem(pem);
                rsa1.DecryptBigData(encrypted, RSAEncryptionPadding.OaepSHA256).ShouldBe(data);
            }

            {
                // pem pkcs8 导出导入测试
                using var rsa = RSAHelper.FromPem(PrivateKeyPkcs8);
                var pem = rsa.ToPem(false, true);
                _testOutputHelper.WriteLine("###############");
                _testOutputHelper.WriteLine(pem);
                using var rsa1 = RSAHelper.FromPem(pem);
                var encrypted = rsa1.EncryptBigData(data, RSAEncryptionPadding.OaepSHA256);
                rsa.DecryptBigData(encrypted, RSAEncryptionPadding.OaepSHA256).ShouldBe(data);
            }
        }