Sample code for 30+ languages & platforms
PowerBuilder

Generate DSA SSH Key

See more SSH Key Examples

Generates a DSA SSH key and saves to various public and private key file formats (OpenSSH and PuTTY).

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Key
long li_NumBits
string ls_ExportedKey
long li_ExportEncrypted

li_Success = 0

loo_Key = create oleobject
li_rc = loo_Key.ConnectToNewObject("Chilkat.SshKey")
if li_rc < 0 then
    destroy loo_Key
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  numBits should be a minimum of 1024 and
//  a multiple of 64.  Typical values are
//  1024 or 2048. 
li_NumBits = 2048
li_Success = loo_Key.GenerateDsaKey(li_NumBits)
if li_Success <> 1 then
    Write-Debug "Invalid numBits passed to DSA key generation method."
    destroy loo_Key
    return
end if

//  Note: Generating a public/private key pair is CPU intensive
//  and may take a short amount of time (more than few seconds,
//  but less than a minute).

//  Export the DSA private key to OpenSSH, PuTTY, and XML and save. 
li_ExportEncrypted = 0
ls_ExportedKey = loo_Key.ToOpenSshPrivateKey(li_ExportEncrypted)
//  Chilkat provides a SaveText method for convenience...
li_Success = loo_Key.SaveText(ls_ExportedKey,"privkey_openssh_unencrypted.pem")

//  Export with encryption to OpenSSH private key format:
loo_Key.Password = "secret"
li_ExportEncrypted = 1
ls_ExportedKey = loo_Key.ToOpenSshPrivateKey(li_ExportEncrypted)
li_Success = loo_Key.SaveText(ls_ExportedKey,"privkey_openssh_encrypted.pem")

//  Export the DSA private key to unencrypted PuTTY format:
li_ExportEncrypted = 0
ls_ExportedKey = loo_Key.ToPuttyPrivateKey(li_ExportEncrypted)
li_Success = loo_Key.SaveText(ls_ExportedKey,"privkey_putty_unencrypted.ppk")

//  Export the DSA private key to encrypted PuTTY format:
loo_Key.Password = "secret"
li_ExportEncrypted = 1
ls_ExportedKey = loo_Key.ToPuttyPrivateKey(li_ExportEncrypted)
li_Success = loo_Key.SaveText(ls_ExportedKey,"privkey_putty_encrypted.ppk")

//  Export private key to XML:
ls_ExportedKey = loo_Key.ToXml()
li_Success = loo_Key.SaveText(ls_ExportedKey,"privkey.xml")

//  ----------------------------------------------------
//  Now for the public key....
//  ----------------------------------------------------

//  The Secure Shell (SSH) Public Key File Format
//  is documented in RFC 4716.
ls_ExportedKey = loo_Key.ToRfc4716PublicKey()
li_Success = loo_Key.SaveText(ls_ExportedKey,"pubkey_rfc4716.pub")

//  OpenSSH has a separate public-key file format, which 
//  is also supported by Chilkat SshKey:
ls_ExportedKey = loo_Key.ToOpenSshPublicKey()
li_Success = loo_Key.SaveText(ls_ExportedKey,"pubkey_openSsh.pub")

Write-Debug "Finished."


destroy loo_Key