public static void RSAParametersToBlob_PublicOnly() { byte[] blob; using (var rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(TestData.RSA1024Params); blob = rsa.ExportCspBlob(false); } RSAParameters exported; using (var rsa = new RSACryptoServiceProvider()) { rsa.ImportCspBlob(blob); Assert.True(rsa.PublicOnly); exported = rsa.ExportParameters(false); } RSAParameters expected = new RSAParameters { Modulus = TestData.RSA1024Params.Modulus, Exponent = TestData.RSA1024Params.Exponent, }; ImportExport.AssertKeyEquals(expected, exported); }
public static void ImportFromPem_Pkcs8UnEncrypted_UnrelatedAlgorithmIsIgnored() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN EC PRIVATE KEY----- MHcCAQEEIHChLC2xaEXtVv9oz8IaRys/BNfWhRv2NJ8tfVs0UrOKoAoGCCqGSM49 AwEHoUQDQgAEgQHs5HRkpurXDPaabivT2IaRoyYtIsuk92Ner/JmgKjYoSumHVmS NfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ== -----END EC PRIVATE KEY----- -----BEGIN PRIVATE KEY----- MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAtz9Z9e6L1V4kt/8C mtFqhUPJbSU+VDGbk1MsQcPBR3uJ2y0vM9e5qHRYSOBqjmg7UERRHhvKNiUn4Xz0 KzgGFQIDAQABAkEAr+byNi+cr17FpJH4MCEiPXaKnmkH4c4U52EJtL9yg2gijBrp Ykat3c2nWb0EGGi5aWgXxQHoi7z97/ACD4X3KQIhAPNyex6GdiBVlNPHOgInTU8a mARKKVHIXM0SxvxXrRl7AiEAwLI66OpSqftDTv1KUfNe6+hyoh23ggzUSYiWuVT0 Ya8CHwiO/cUU9RIt8A2B84gf2ZfuV2nPMaSuZpTPFC/K5UsCIQCsJMzx1JuilQAN acPiMCuFTnRSFYAhozpmsqoLyTREqwIhAMLJlZTGjEB2N+sEazH5ToEczQzKqp7t 9juGNbOPhoEL -----END PRIVATE KEY-----"; rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(true); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters, rsaParameters); } }
public static void RSAParametersToBlob_PublicPrivate() { byte[] blob; using (var rsa = new RSACryptoServiceProvider()) { rsa.ImportParameters(TestData.RSA1024Params); blob = rsa.ExportCspBlob(true); } RSAParameters exported; using (var rsa = new RSACryptoServiceProvider()) { rsa.ImportCspBlob(blob); Assert.False(rsa.PublicOnly); exported = rsa.ExportParameters(true); } RSAParameters expected = TestData.RSA1024Params; ImportExport.AssertKeyEquals(expected, exported); }
public static void ImportFromPem_RSAPrivateKey_IgnoresOtherAlgorithms() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN EC PRIVATE KEY----- MHcCAQEEIHChLC2xaEXtVv9oz8IaRys/BNfWhRv2NJ8tfVs0UrOKoAoGCCqGSM49 AwEHoUQDQgAEgQHs5HRkpurXDPaabivT2IaRoyYtIsuk92Ner/JmgKjYoSumHVmS NfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ== -----END EC PRIVATE KEY----- -----BEGIN RSA PRIVATE KEY----- MIIBOwIBAAJBALc/WfXui9VeJLf/AprRaoVDyW0lPlQxm5NTLEHDwUd7idstLzPX uah0WEjgao5oO1BEUR4byjYlJ+F89Cs4BhUCAwEAAQJBAK/m8jYvnK9exaSR+DAh Ij12ip5pB+HOFOdhCbS/coNoIowa6WJGrd3Np1m9BBhouWloF8UB6Iu8/e/wAg+F 9ykCIQDzcnsehnYgVZTTxzoCJ01PGpgESilRyFzNEsb8V60ZewIhAMCyOujqUqn7 Q079SlHzXuvocqIdt4IM1EmIlrlU9GGvAh8Ijv3FFPUSLfANgfOIH9mX7ldpzzGk rmaUzxQvyuVLAiEArCTM8dSbopUADWnD4jArhU50UhWAIaM6ZrKqC8k0RKsCIQDC yZWUxoxAdjfrBGsx+U6BHM0Myqqe7fY7hjWzj4aBCw== -----END RSA PRIVATE KEY-----"; rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(true); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters, rsaParameters); } }
public static void ImportFromPem_RSAPrivateKey_PrecedingUnrelatedPem() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN CERTIFICATE----- MIICTzCCAgmgAwIBAgIJAMQtYhFJ0+5jMA0GCSqGSIb3DQEBBQUAMIGSMQswCQYD VQQGEwJVUzETMBEGA1UECAwKV2FzaGluZ3RvbjEQMA4GA1UEBwwHUmVkbW9uZDEY MBYGA1UECgwPTWljcm9zb2Z0IENvcnAuMSAwHgYDVQQLDBcuTkVUIEZyYW1ld29y ayAoQ29yZUZ4KTEgMB4GA1UEAwwXUlNBIDM4NC1iaXQgQ2VydGlmaWNhdGUwHhcN MTYwMzAyMTY1OTA0WhcNMTYwNDAxMTY1OTA0WjCBkjELMAkGA1UEBhMCVVMxEzAR BgNVBAgMCldhc2hpbmd0b24xEDAOBgNVBAcMB1JlZG1vbmQxGDAWBgNVBAoMD01p Y3Jvc29mdCBDb3JwLjEgMB4GA1UECwwXLk5FVCBGcmFtZXdvcmsgKENvcmVGeCkx IDAeBgNVBAMMF1JTQSAzODQtYml0IENlcnRpZmljYXRlMEwwDQYJKoZIhvcNAQEB BQADOwAwOAIxANrMIthuZxV1Ay4x8gbc/BksZeLVEInlES0JbyiCr9tbeM22Vy/S 9h2zkEciMuPZ9QIDAQABo1AwTjAdBgNVHQ4EFgQU5FG2Fmi86hJOCf4KnjaxOGWV dRUwHwYDVR0jBBgwFoAU5FG2Fmi86hJOCf4KnjaxOGWVdRUwDAYDVR0TBAUwAwEB /zANBgkqhkiG9w0BAQUFAAMxAEzDg/u8TlApCnE8qxhcbTXk2MbX+2n5PCn+MVrW wggvPj3b2WMXsVWiPr4S1Y/nBA== -----END CERTIFICATE----- -----BEGIN RSA PRIVATE KEY----- MIIBOwIBAAJBALc/WfXui9VeJLf/AprRaoVDyW0lPlQxm5NTLEHDwUd7idstLzPX uah0WEjgao5oO1BEUR4byjYlJ+F89Cs4BhUCAwEAAQJBAK/m8jYvnK9exaSR+DAh Ij12ip5pB+HOFOdhCbS/coNoIowa6WJGrd3Np1m9BBhouWloF8UB6Iu8/e/wAg+F 9ykCIQDzcnsehnYgVZTTxzoCJ01PGpgESilRyFzNEsb8V60ZewIhAMCyOujqUqn7 Q079SlHzXuvocqIdt4IM1EmIlrlU9GGvAh8Ijv3FFPUSLfANgfOIH9mX7ldpzzGk rmaUzxQvyuVLAiEArCTM8dSbopUADWnD4jArhU50UhWAIaM6ZrKqC8k0RKsCIQDC yZWUxoxAdjfrBGsx+U6BHM0Myqqe7fY7hjWzj4aBCw== -----END RSA PRIVATE KEY-----"; rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(true); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters, rsaParameters); } }
public static void ImportFromPem_SubjectPublicKeyInfo_Simple() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN PUBLIC KEY----- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALc/WfXui9VeJLf/AprRaoVDyW0lPlQx m5NTLEHDwUd7idstLzPXuah0WEjgao5oO1BEUR4byjYlJ+F89Cs4BhUCAwEAAQ== -----END PUBLIC KEY-----"; rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(false); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters.ToPublic(), rsaParameters); } }
public static void ImportFromPem_RSAPublicKey_Simple() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN RSA PUBLIC KEY----- MEgCQQC3P1n17ovVXiS3/wKa0WqFQ8ltJT5UMZuTUyxBw8FHe4nbLS8z17modFhI 4GqOaDtQRFEeG8o2JSfhfPQrOAYVAgMBAAE= -----END RSA PUBLIC KEY-----"; rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(false); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters.ToPublic(), rsaParameters); } }
public static void ImportFromPem_SubjectPublicKeyInfo_IgnoresUnrelatedAlgorithm() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN EC PRIVATE KEY----- MHcCAQEEIHChLC2xaEXtVv9oz8IaRys/BNfWhRv2NJ8tfVs0UrOKoAoGCCqGSM49 AwEHoUQDQgAEgQHs5HRkpurXDPaabivT2IaRoyYtIsuk92Ner/JmgKjYoSumHVmS NfZ9nLTVjxeD08pD548KWrqmJAeZNsDDqQ== -----END EC PRIVATE KEY----- -----BEGIN PUBLIC KEY----- MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALc/WfXui9VeJLf/AprRaoVDyW0lPlQx m5NTLEHDwUd7idstLzPXuah0WEjgao5oO1BEUR4byjYlJ+F89Cs4BhUCAwEAAQ== -----END PUBLIC KEY-----"; rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(false); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters.ToPublic(), rsaParameters); } }
public static void ImportFromPem_RSAPrivateKey_Simple() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN RSA PRIVATE KEY----- MIIBOwIBAAJBALc/WfXui9VeJLf/AprRaoVDyW0lPlQxm5NTLEHDwUd7idstLzPX uah0WEjgao5oO1BEUR4byjYlJ+F89Cs4BhUCAwEAAQJBAK/m8jYvnK9exaSR+DAh Ij12ip5pB+HOFOdhCbS/coNoIowa6WJGrd3Np1m9BBhouWloF8UB6Iu8/e/wAg+F 9ykCIQDzcnsehnYgVZTTxzoCJ01PGpgESilRyFzNEsb8V60ZewIhAMCyOujqUqn7 Q079SlHzXuvocqIdt4IM1EmIlrlU9GGvAh8Ijv3FFPUSLfANgfOIH9mX7ldpzzGk rmaUzxQvyuVLAiEArCTM8dSbopUADWnD4jArhU50UhWAIaM6ZrKqC8k0RKsCIQDC yZWUxoxAdjfrBGsx+U6BHM0Myqqe7fY7hjWzj4aBCw== -----END RSA PRIVATE KEY-----"; rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(true); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters, rsaParameters); } }
public static void ImportFromPem_Pkcs8UnEncrypted_Simple() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN PRIVATE KEY----- MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAtz9Z9e6L1V4kt/8C mtFqhUPJbSU+VDGbk1MsQcPBR3uJ2y0vM9e5qHRYSOBqjmg7UERRHhvKNiUn4Xz0 KzgGFQIDAQABAkEAr+byNi+cr17FpJH4MCEiPXaKnmkH4c4U52EJtL9yg2gijBrp Ykat3c2nWb0EGGi5aWgXxQHoi7z97/ACD4X3KQIhAPNyex6GdiBVlNPHOgInTU8a mARKKVHIXM0SxvxXrRl7AiEAwLI66OpSqftDTv1KUfNe6+hyoh23ggzUSYiWuVT0 Ya8CHwiO/cUU9RIt8A2B84gf2ZfuV2nPMaSuZpTPFC/K5UsCIQCsJMzx1JuilQAN acPiMCuFTnRSFYAhozpmsqoLyTREqwIhAMLJlZTGjEB2N+sEazH5ToEczQzKqp7t 9juGNbOPhoEL -----END PRIVATE KEY-----"; rsa.ImportFromPem(pem); RSAParameters rsaParameters = rsa.ExportParameters(true); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters, rsaParameters); } }
public static void ImportFromEncryptedPem_Pkcs8Encrypted_Byte_Simple() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN ENCRYPTED PRIVATE KEY----- MIIBvTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQIciLWmWb33X0CAggA MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAECBBBVEmHhJdbi+HKzPttNjXm4BIIB YFejknurbot2VDXwc671A0mfA0cw/u7K44gsYXcZwAARC8j6f3lSzB0tN2kMEx/L TB+kpMBbfAoIPKoEc9Y4w9m3NXkQYrLRONh9AFiAnOjULHwkstQfN2ofFlolDfbH hAE6ga6aQJTQ8rDKTL4QkCg+s+qWlicPqs5ikSQfUz2Qiy8FKe7zZlJ0OWpT+zk7 EYRrUSKQcEAjfNS7anlMps2ZXRc1LkLJNHZSl6h2BuFPfIKEV9REpy3Y7sH7vNZZ PWPa9/xM4CX/c/ommy6LqvZikUuUGc56/Hbz65SwG3voivIhOTmM28LiA6z0YXmY E+nr7hyinl51raM1RSHojJB22oOW+GwV7GgWYIjUgIEMDOhN10FcGNfTeC65PCXx 5QSEe7EKVF0aHXBYB5SzMGVuxR/BqydDa26jlhVzO3LNvy9FYuqLKUslCrBCmPrt raZNyk8KAsLs+FJq9T2tda0= -----END ENCRYPTED PRIVATE KEY-----"; rsa.ImportFromEncryptedPem(pem, Encoding.UTF8.GetBytes("test")); RSAParameters rsaParameters = rsa.ExportParameters(true); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters, rsaParameters); } }
public static void ImportFromEncryptedPem_Pkcs8Encrypted_Char_Simple() { using (RSA rsa = RSAFactory.Create()) { string pem = @" -----BEGIN ENCRYPTED PRIVATE KEY----- MIIBsTBLBgkqhkiG9w0BBQ0wPjApBgkqhkiG9w0BBQwwHAQIcvgI1lw9LqYCAggA MAwGCCqGSIb3DQIJBQAwEQYFKw4DAgcECFDpLREQXt5pBIIBYOKuM5ljAvCViDL+ nTFq7A/fI9rqdL20TMdf0wy7s43oXmsw5gCStoNEaoVToFCQWYYBRU99mK8YNFA8 1ZJT53SDS7buJ0zX9oDltf2ByXRPI4mn2Il2HZvN2hi9ir1w8M3XoSFSurN9tC8r IOiGkVfK9Ll54knONewNiCNefFZFctRfVMbac5SwHokCkBMHukl0oPrpVuBE8kRo p7XtjM8ILtzLVz0iLqKXiNIf6kRdouCBmCn8VIQgIvPPIHD8vheMXWjN7g69P5n4 1YI4c/acljcofmq1BBPTwvxaETrg2NHW0XMIgAxoaVP8lIIGlNk1glWTYpuMd69L AWvBUt33Sozc+dF0l7NGLAWL2tqkkpyDQuKn6UgYz/vxkFeQAVfSuaJVR+fUlHg0 N4lD7/hJq7b+yYPhlN3Fvvt8M9MtRg1TLAve67CA2v4TITHB06M/ELe3y42bZuLW CA7ffFk= -----END ENCRYPTED PRIVATE KEY-----"; rsa.ImportFromEncryptedPem(pem, "test"); RSAParameters rsaParameters = rsa.ExportParameters(true); ImportExport.AssertKeyEquals(TestData.DiminishedDPParameters, rsaParameters); } }