Unicode C
Unicode C
Convert a PuTTY Private Key (.ppk) to OpenSSH (.pem)
See more SSH Examples
Demonstrates converting a PuTTY format private key to OpenSSH format. The .ppk is imported with FromPuttyPrivateKey and re-exported with ToOpenSshPrivateKey, both unencrypted and encrypted.
Note: The file paths are relative to the application's current working directory. Supply the paths to your own files.
Background: PuTTY and OpenSSH store the same underlying key material in different container formats, so converting between them is a re-encoding rather than a new key — the corresponding public key, and therefore the server-side
authorized_keys entry, is unchanged. This matters when moving between Windows tooling built around PuTTY and Unix tooling that expects PEM. Note that the Password property serves double duty: it decrypts the key on import and encrypts it on export, so set it appropriately for each step.Chilkat Unicode C Downloads
#include <C_CkSshKeyW.h>
void ChilkatSample(void)
{
BOOL success;
HCkSshKeyW key;
const wchar_t *keyStr;
BOOL bEncrypt;
const wchar_t *unencryptedKeyStr;
const wchar_t *encryptedKeyStr;
success = FALSE;
// Demonstrates converting a PuTTY format private key (.ppk) to OpenSSH (.pem) format.
key = CkSshKeyW_Create();
// Set the password before importing an encrypted PuTTY key. If the key is not encrypted it
// makes no difference whether Password is set. This should come from a secure source rather
// than being hard-coded.
CkSshKeyW_putPassword(key,L"myKeyPassword");
// LoadText is a convenience method that reads any text file into a string. It does not itself
// load the key.
keyStr = CkSshKeyW_loadText(key,L"qa_data/putty_private_key.ppk");
if (CkSshKeyW_getLastMethodSuccess(key) == FALSE) {
wprintf(L"%s\n",CkSshKeyW_lastErrorText(key));
CkSshKeyW_Dispose(key);
return;
}
success = CkSshKeyW_FromPuttyPrivateKey(key,keyStr);
if (success == FALSE) {
wprintf(L"%s\n",CkSshKeyW_lastErrorText(key));
CkSshKeyW_Dispose(key);
return;
}
// Export to an unencrypted OpenSSH key.
bEncrypt = FALSE;
unencryptedKeyStr = CkSshKeyW_toOpenSshPrivateKey(key,bEncrypt);
if (CkSshKeyW_getLastMethodSuccess(key) == FALSE) {
wprintf(L"%s\n",CkSshKeyW_lastErrorText(key));
CkSshKeyW_Dispose(key);
return;
}
success = CkSshKeyW_SaveText(key,unencryptedKeyStr,L"qa_output/unencrypted_openssh.pem");
if (success == FALSE) {
wprintf(L"%s\n",CkSshKeyW_lastErrorText(key));
CkSshKeyW_Dispose(key);
return;
}
// Export to an encrypted OpenSSH key. The Password property supplies the passphrase used to
// encrypt the output.
bEncrypt = TRUE;
CkSshKeyW_putPassword(key,L"myExportPassword");
encryptedKeyStr = CkSshKeyW_toOpenSshPrivateKey(key,bEncrypt);
if (CkSshKeyW_getLastMethodSuccess(key) == FALSE) {
wprintf(L"%s\n",CkSshKeyW_lastErrorText(key));
CkSshKeyW_Dispose(key);
return;
}
success = CkSshKeyW_SaveText(key,encryptedKeyStr,L"qa_output/encrypted_openssh.pem");
if (success == FALSE) {
wprintf(L"%s\n",CkSshKeyW_lastErrorText(key));
CkSshKeyW_Dispose(key);
return;
}
wprintf(L"Done!\n");
CkSshKeyW_Dispose(key);
}