OWASP O2 Platform Blog

O2 Script: automating PuttyGen to create Public and Private Keys

Once we have Tortoise installed (using an O2 Script) the next step is to create the SSH RSA keys using PuttyGen (which is include in TortoiseGit).

This script automates the whole process where it:

  • asks the user for the following details: Key Name, PassPhrase, Target directory
  • starts the puttygen process
  • uses windows Automation to populate the required values and click on the buttons (puttygen needs some extra mouse movement in order to generate the key)
  • saves the public and private key

This is how it is invoked

var gitHub = new API_GitHub();


Here is the code that drives the automation

        public static API_GitHub putty_generateKeys(this API_GitHub gitHub, string keyPassPhrase, string publicKey, string privateKey)
            if (keyPassPhrase.inValid() || publicKey.inValid() || privateKey.inValid())
                "the keyPassPhrase, publicKey, privateKey needs to be valid".error();
                return gitHub;
            var puttyGenPath = @"C:\Program Files\TortoiseGit\bin\puttygen.exe";
            if (puttyGenPath.fileExists().isFalse())
                "Error: could not find puttyGen in TortoiseGit folder: {0}".error(puttyGenPath);
                return gitHub;
            var process = puttyGenPath.startProcess();
            var puttyGen = new API_GuiAutomation(process);  
            var window = puttyGen.windows()[0];
            var generateButton = window.button("Generate").click();
            /// lets move the mouse a bit to create some randomness for PuttyGen
            //once the key is generated we need to put in the passphrase
            window.textBox("Key passphrase:").set_Text(keyPassPhrase ?? "");
            window.textBox("Confirm passphrase:").set_Text(keyPassPhrase ?? ""); 
            //Saving private Key
            window.button("Save private key").click();
            var saveAsWindow = puttyGen.window("Save private key as:");
            saveAsWindow.textBox("File name:").set_Text(privateKey);
            //Saving public Key
            window.button("Save public key").click();
            saveAsWindow = puttyGen.window("Save public key as:");
            saveAsWindow.textBox("File name:").set_Text(publicKey);
            "The keys were saved to {0} and {1}: {0}".info(publicKey, privateKey);
            return gitHub;

March 16, 2011 - Posted by | Interoperability


  1. […] we had TortoiseSVN  installed and used PuttyGen to create the SSH key we now need to add the SSH Key to the GitHub website, which is done by automating the […]

    Pingback by O2 Script: adding the SSH to GitHub « O2Platform.com for Developers | March 18, 2011 | Reply

  2. […] installing  TortoiseGIT ,  using PuttyGen to create the SSH key and adding the SSH Key to GitHub the final step in this series is to create GIT Clone locally […]

    Pingback by O2 Script: Creating a Git Clone using TortoiseGIT « O2Platform.com for Developers | March 18, 2011 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: