Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 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 .key file:
nSuccess := oPrivKey:LoadPemFile("privateKey.key")
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
// Create the signature as a hex string:
oRsa:EncodingMode := "hex"
cStrData := "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".
cHexSig := oRsa:SignStringENC(cStrData, "sha256")
? cHexSig
// Load a digital certificate from a .cer file:
oCert := CreateObject("Chilkat.Cert")
nSuccess := oCert:LoadFromFile("myCert.cer")
IF (nSuccess == 0)
? oCert:LastErrorText
oPrivKey:destroy()
oRsa:destroy()
oCert:destroy()
RETURN
ENDIF
oPubKey := CreateObject("Chilkat.PublicKey")
oCert:GetPublicKey(oPubKey)
// Now verify using a new instance of the RSA object:
oRsa2 := CreateObject("Chilkat.Rsa")
// Import the public key into the RSA object:
nSuccess := oRsa2:UsePublicKey(oPubKey)
IF (nSuccess == 0)
? oRsa2:LastErrorText
oPrivKey:destroy()
oRsa:destroy()
oCert:destroy()
oPubKey:destroy()
oRsa2:destroy()
RETURN
ENDIF
// The signature is a hex string, so make sure the EncodingMode is correct:
oRsa2:EncodingMode := "hex"
// Verify the signature:
nSuccess := oRsa2:VerifyStringENC(cStrData, "sha256", cHexSig)
IF (nSuccess == 0)
? oRsa2:LastErrorText
oPrivKey:destroy()
oRsa:destroy()
oCert:destroy()
oPubKey:destroy()
oRsa2:destroy()
RETURN
ENDIF
? "Success."
oPrivKey:destroy()
oRsa:destroy()
oCert:destroy()
oPubKey:destroy()
oRsa2:destroy()