Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPrivKey
LOCAL oRsa
LOCAL cStrData
LOCAL cHexSig
LOCAL oCert
LOCAL oPubKey
LOCAL oRsa2
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oPrivKey := CreateObject("Chilkat.PrivateKey")
// Load the private key from an RSA PEM file:
nSuccess := oPrivKey:LoadAnyFormatFile("raul_privateKey.key", "a0123456789")
IF (nSuccess == 0)
? oPrivKey:LastErrorText
oPrivKey:destroy()
RETURN
ENDIF
oRsa := CreateObject("Chilkat.Rsa")
// Import the private key into the RSA component:
nSuccess := oRsa:UsePrivateKey(oPrivKey)
IF (nSuccess == 0)
? oRsa:LastErrorText
oPrivKey:destroy()
oRsa:destroy()
RETURN
ENDIF
// This example will sign a string, and receive the signature
// in a hex-encoded string. Therefore, set the encoding mode
// to "hex":
oRsa:EncodingMode := "hex"
cStrData := "This is the string to be signed."
// Sign the string using the sha256 hash algorithm.
// Other valid choices are sha1, sha384, sha512 and others.
cHexSig := oRsa:SignStringENC(cStrData, "sha256")
IF (oRsa:LastMethodSuccess == 0)
? oRsa:LastErrorText
oPrivKey:destroy()
oRsa:destroy()
RETURN
ENDIF
? cHexSig
// Now verify with the public key.
// This example shows how to use the public key from
// a digital certificate (.cer file)
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadFromFile("raul_publicKey.cer")
IF (nSuccess == 0)
? oCert:LastErrorText
oPrivKey:destroy()
oRsa:destroy()
oCert:destroy()
RETURN
ENDIF
oPubKey := CreateObject("Chilkat.PublicKey")
oCert:GetPublicKey(oPubKey)
oRsa2 := CreateObject("Chilkat.Rsa")
nSuccess := oRsa2:UsePublicKey(oPubKey)
IF (nSuccess == 0)
? oRsa2:LastErrorText
oPrivKey:destroy()
oRsa:destroy()
oCert:destroy()
oPubKey:destroy()
oRsa2:destroy()
RETURN
ENDIF
// Verify the signature against the original data:
oRsa2:EncodingMode := "hex"
nSuccess := oRsa2:VerifyStringENC(cStrData, "sha256", cHexSig)
IF (nSuccess == 0)
? oRsa2:LastErrorText
oPrivKey:destroy()
oRsa:destroy()
oCert:destroy()
oPubKey:destroy()
oRsa2:destroy()
RETURN
ENDIF
? "Signature verified!"
// Verify with incorrect data:
nSuccess := oRsa2:VerifyStringENC("something else", "sha256", cHexSig)
IF (nSuccess != 1)
? "Signature not verified! (which was expected in this case)"
ELSE
? "Hmmm... that's not right..."
ENDIF
oPrivKey:destroy()
oRsa:destroy()
oCert:destroy()
oPubKey:destroy()
oRsa2:destroy()