PowerBuilder
PowerBuilder
RSA Signature/Verify with .key and .cer
See more RSA Examples
Demonstrates how to use a .key file (private key) and digital certificate (.cer, public key) to create and verify an RSA signature.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_PrivKey
oleobject loo_Rsa
string ls_StrData
string ls_HexSig
oleobject loo_Cert
oleobject loo_PubKey
oleobject loo_Rsa2
li_Success = 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
if li_rc < 0 then
destroy loo_PrivKey
MessageBox("Error","Connecting to COM object failed")
return
end if
// Load the private key from an RSA .key file:
li_Success = loo_PrivKey.LoadPemFile("privateKey.key")
if li_Success = 0 then
Write-Debug loo_PrivKey.LastErrorText
destroy loo_PrivKey
return
end if
loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")
// Import the private key into the RSA component:
li_Success = loo_Rsa.UsePrivateKey(loo_PrivKey)
if li_Success = 0 then
Write-Debug loo_Rsa.LastErrorText
destroy loo_PrivKey
destroy loo_Rsa
return
end if
// Create the signature as a hex string:
loo_Rsa.EncodingMode = "hex"
ls_StrData = "This is the string to be signed."
// Sign the string using the sha256 hash algorithm.
// Other valid choices are "md2", "sha1", "sha384",
// "sha512", and "md5".
ls_HexSig = loo_Rsa.SignStringENC(ls_StrData,"sha256")
Write-Debug ls_HexSig
// Load a digital certificate from a .cer file:
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Cert.LoadFromFile("myCert.cer")
if li_Success = 0 then
Write-Debug loo_Cert.LastErrorText
destroy loo_PrivKey
destroy loo_Rsa
destroy loo_Cert
return
end if
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")
loo_Cert.GetPublicKey(loo_PubKey)
// Now verify using a new instance of the RSA object:
loo_Rsa2 = create oleobject
li_rc = loo_Rsa2.ConnectToNewObject("Chilkat.Rsa")
// Import the public key into the RSA object:
li_Success = loo_Rsa2.UsePublicKey(loo_PubKey)
if li_Success = 0 then
Write-Debug loo_Rsa2.LastErrorText
destroy loo_PrivKey
destroy loo_Rsa
destroy loo_Cert
destroy loo_PubKey
destroy loo_Rsa2
return
end if
// The signature is a hex string, so make sure the EncodingMode is correct:
loo_Rsa2.EncodingMode = "hex"
// Verify the signature:
li_Success = loo_Rsa2.VerifyStringENC(ls_StrData,"sha256",ls_HexSig)
if li_Success = 0 then
Write-Debug loo_Rsa2.LastErrorText
destroy loo_PrivKey
destroy loo_Rsa
destroy loo_Cert
destroy loo_PubKey
destroy loo_Rsa2
return
end if
Write-Debug "Success."
destroy loo_PrivKey
destroy loo_Rsa
destroy loo_Cert
destroy loo_PubKey
destroy loo_Rsa2