PowerBuilder
PowerBuilder
Generate RSA Key and Sign a String
See more RSA Examples
Demonstrates how to generate a new RSA public/private key pair and use it to generate a signature for a string. The (binary) digital signature is returned as a hexidecimalized string.Chilkat PowerBuilder Downloads
integer li_rc
integer li_Success
oleobject loo_Rsa
oleobject loo_PrivKey
string ls_StrData
string ls_HexSig
oleobject loo_PubKey
li_Success = 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")
if li_rc < 0 then
destroy loo_Rsa
MessageBox("Error","Connecting to COM object failed")
return
end if
// Generate a 2048-bit RSA key.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
li_Success = loo_Rsa.GenKey(2048,loo_PrivKey)
loo_Rsa.UsePrivateKey(loo_PrivKey)
// Return the signature in hex.
loo_Rsa.EncodingMode = "hex"
ls_StrData = "This is the string to be signed."
// Sign the SHA256 hash of the string.
ls_HexSig = loo_Rsa.SignStringENC(ls_StrData,"sha256")
Write-Debug ls_HexSig
// Now verify the signature:
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")
loo_PrivKey.ToPublicKey(loo_PubKey)
loo_Rsa.UsePublicKey(loo_PubKey)
li_Success = loo_Rsa.VerifyStringENC(ls_StrData,"sha256",ls_HexSig)
if li_Success = 1 then
Write-Debug "Signature verified!"
else
Write-Debug loo_Rsa.LastErrorText
end if
// Try it with an invalid signature:
li_Success = loo_Rsa.VerifyStringENC(ls_StrData,"sha256","not a valid sig")
if li_Success = 1 then
Write-Debug "Signature verified!"
else
Write-Debug "Signature validation failed!"
end if
// Try it with invalid data:
li_Success = loo_Rsa.VerifyStringENC("Not the original data","sha256",ls_HexSig)
if li_Success = 1 then
Write-Debug "Signature verified!"
else
Write-Debug "Signature validation failed!"
end if
destroy loo_Rsa
destroy loo_PrivKey
destroy loo_PubKey