Exemplo n.º 1
0
        static void TestSSHKeyGen()
        {
            var rsa = new RSACryptoServiceProvider(256 * 8);

            var sshRsa = PemKeyUtils.ExportPublicKeySSH(rsa);

            Console.WriteLine(sshRsa);

            var privPem = RSAKeys.ExportPrivateKey(rsa);

            Console.WriteLine(privPem);
        }
Exemplo n.º 2
0
        // done id_rsa (pem) => RSACryptoServiceProvider
        // done id_rsa.pub (openssh) => RSACryptoServiceProvider
        // done priv RSACryptoServiceProvider => id_rsa (pem)
        // done priv RSACryptoServiceProvider => id_rsa.pem (pem)
        // done priv RSACryptoServiceProvider => id_rsa.pub (openssh)

        static void Main(string[] args)
        {
            // ssh-keygen -t rsa -C "*****@*****.**"
            var privPemFile = @"c:\temp\id_rsa_test";
            var pubSSHFile  = @"c:\temp\id_rsa_test.pub";
            var pubPemFile  = @"c:\temp\temp.pem";

            // ssh-keygen -f c:\temp\id_rsa_test.pub -e -m pem > id_rsa_test1.pem
            // var pubPem1File = @"c:\temp\id_rsa_test1.pem";
            //  "c:\Program Files\Git\usr\bin\openssl.exe" rsa -in c:\temp\id_rsa_test -pubout > id_rsa_test2.pem
            var pubPem2File = @"c:\temp\id_rsa_test2.pem";

            try
            {
                // id_rsa (pem) => RSACryptoServiceProvider
                var rsaPri = PemKeyUtils.GetRSAProviderFromPemFile(privPemFile);
                //Console.WriteLine(rsaPri);

                TestSSHKeyGen();

                var cer       = new X509Certificate2(Convert.FromBase64String("MIIDBTCCAe2gAwIBAgIQbiJkXaenk61AKixVocnLRTANBgkqhkiG9w0BAQsFADAtMSswKQYDVQQDEyJhY2NvdW50cy5hY2Nlc3Njb250cm9sLndpbmRvd3MubmV0MB4XDTE5MTAwNTAwMDAwMFoXDTI0MTAwNDAwMDAwMFowLTErMCkGA1UEAxMiYWNjb3VudHMuYWNjZXNzY29udHJvbC53aW5kb3dzLm5ldDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAJ2H9Y6Z+3BXUCtlbmXr6H5owYy15XTl3vdpOZL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zJmOTg4YmYtODZmMS00MWFmLTkxYWItMmQ3Y2QwMTFkYjQ3IiwidW5pcXVlX25hbWUiOiJzdXdhdGNoQG1pY3Jvc29mdC5jb20iLCJ1cG4iOiJzdXdhdGNoQG1pY3Jvc29mdC5jb20iLCJ1dGkiOiIteVhESHl4UVQwMllNVWpLcXNjaEFBIiwidmVyIjoiMS4wIn0.Vw9lr708WLmJAVcyz9mhLjHpblBviwGKnqJJR-HbcMgGN-P37vFUrpzOSi254Zoy3jjHuZsRh5i2AXRxYqTrADyJMRjr7y4DB_waJQ9O49dN9W_qpKFZBkTkr3ZE6j_wIoUH-6D8xFMtMcbMTVVJn9JWq23sgWgr4DUw-qC1sU_1RfsqvwFlCsrjcZZdvN5PiM7TTXjlUEbThmUG1gmxTORjlCrsS6KG7t5Smj4ycZQMCy6Ds26cw-wL6Y2vXKdiON9nknlvUwrsK2kxhR5e4SI91A33PeqEmqXlxpxMp630nyk5RMBd_4tCnkEDlZet5L_MNPeZsasdAUssjdDh5A";
                var validated = ValidateToken(jwt, new X509SecurityKey(cer));
                Console.WriteLine(validated);
                validated = ValidateToken2(jwt, new X509SecurityKey(cer));
                Console.WriteLine(validated);


                // not working BEGIN RSA PUBLIC KEY!!
                // var rsaPub_1 = PemKeyUtils.GetRSAProviderFromPemFile(pubPem1File);
                var rsaPub_2 = PemKeyUtils.GetRSAProviderFromPemFile(pubPem2File);

                jwt       = GenerateJWT(rsaPri);
                validated = ValidateToken(jwt, new RsaSecurityKey(rsaPri));
                Console.WriteLine(validated);
                validated = ValidateToken2(jwt, new RsaSecurityKey(rsaPub_2));
                Console.WriteLine(validated);

                // "c:\Program Files\Git\usr\bin\openssl.exe" rsa -pubin -in c:\temp\temp.pem -text -noout
                // priv RSACryptoServiceProvider => id_rsa (pem)
                // TODO, suwatch:
                var privPem = RSAKeys.ExportPrivateKey(rsaPri);
                Console.WriteLine(privPem == File.ReadAllText(privPemFile));
                //Console.WriteLine(File.ReadAllText(privPemFile));
                //Console.WriteLine(privPem);

                var pubPem = PemKeyUtils.ExportPublicKey(rsaPri);
                Console.WriteLine(pubPem == File.ReadAllText(pubPemFile));
                //Console.WriteLine(pub);
                //File.WriteAllText(pubPemFile, pubPem);

                var rsaPub = PemKeyUtils.GetRSAProviderFromPemFile(pubPemFile);
                //Console.WriteLine(rsaPub);

                // priv RSACryptoServiceProvider => id_rsa.pub (openssh)
                var id_rsa_pub = PemKeyUtils.ExportPublicKeySSH(rsaPub);
                Console.WriteLine(id_rsa_pub == File.ReadAllText(pubSSHFile));
                //Console.WriteLine("'" + id_rsa_pub + "'");
                //Console.WriteLine("'" + File.ReadAllText(pubSSHFile) + "'");

                var privParams = rsaPri.ExportParameters(includePrivateParameters: false);
                var pubParams  = rsaPub.ExportParameters(includePrivateParameters: false);

                // id_rsa.pub (openssh) => RSACryptoServiceProvider
                var rsaPub1    = PemKeyUtils.FromOpenSSHFile(pubSSHFile);
                var pub1Params = rsaPub1.ExportParameters(includePrivateParameters: false);

                // Modulus and exponent represent Public component
                //Console.WriteLine(Convert.ToBase64String(privParams.Exponent));
                //Console.WriteLine(Convert.ToBase64String(privParams.Modulus));
                Console.WriteLine(Convert.ToBase64String(privParams.Exponent) == Convert.ToBase64String(pubParams.Exponent));
                Console.WriteLine(Convert.ToBase64String(privParams.Modulus) == Convert.ToBase64String(pubParams.Modulus));
                Console.WriteLine(Convert.ToBase64String(privParams.Exponent) == Convert.ToBase64String(pub1Params.Exponent));
                Console.WriteLine(Convert.ToBase64String(privParams.Modulus) == Convert.ToBase64String(pub1Params.Modulus));

                var text      = Guid.NewGuid().ToString();
                var signature = SignRS256(text, rsaPri);
                var verified  = VerifyRS256(text, signature, rsaPub);
                Console.WriteLine(verified);

                verified = VerifyRS256(text, signature, rsaPub1);
                Console.WriteLine(verified);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }