PowerBuilder
PowerBuilder
RSA Sign with PKCS8 Encrypted Key
See more RSA Examples
Demonstrates how to load a private key from an encrypted PKCS8 file and create an RSA digital signature (and then verify it).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 PEM file:
li_Success = loo_PrivKey.LoadAnyFormatFile("raul_privateKey.key","a0123456789")
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
// This example will sign a string, and receive the signature
// in a hex-encoded string. Therefore, set the encoding mode
// to "hex":
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 sha1, sha384, sha512 and others.
ls_HexSig = loo_Rsa.SignStringENC(ls_StrData,"sha256")
if loo_Rsa.LastMethodSuccess = 0 then
Write-Debug loo_Rsa.LastErrorText
destroy loo_PrivKey
destroy loo_Rsa
return
end if
Write-Debug ls_HexSig
// Now verify with the public key.
// This example shows how to use the public key from
// a digital certificate (.cer file)
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")
li_Success = loo_Cert.LoadFromFile("raul_publicKey.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)
loo_Rsa2 = create oleobject
li_rc = loo_Rsa2.ConnectToNewObject("Chilkat.Rsa")
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
// Verify the signature against the original data:
loo_Rsa2.EncodingMode = "hex"
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 "Signature verified!"
// Verify with incorrect data:
li_Success = loo_Rsa2.VerifyStringENC("something else","sha256",ls_HexSig)
if li_Success <> 1 then
Write-Debug "Signature not verified! (which was expected in this case)"
else
Write-Debug "Hmmm... that's not right..."
end if
destroy loo_PrivKey
destroy loo_Rsa
destroy loo_Cert
destroy loo_PubKey
destroy loo_Rsa2