Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oRsa
LOCAL oPrivKey
LOCAL cStrData
LOCAL cHexSig
LOCAL oPubKey
nSuccess := 0
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
oRsa := CreateObject("Chilkat.Rsa")
// Generate a 2048-bit RSA key.
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oRsa:GenKey(2048, oPrivKey)
oRsa:UsePrivateKey(oPrivKey)
// Return the signature in hex.
oRsa:EncodingMode := "hex"
cStrData := "This is the string to be signed."
// Sign the SHA256 hash of the string.
cHexSig := oRsa:SignStringENC(cStrData, "sha256")
? cHexSig
// Now verify the signature:
oPubKey := CreateObject("Chilkat.PublicKey")
oPrivKey:ToPublicKey(oPubKey)
oRsa:UsePublicKey(oPubKey)
nSuccess := oRsa:VerifyStringENC(cStrData, "sha256", cHexSig)
IF (nSuccess == 1)
? "Signature verified!"
ELSE
? oRsa:LastErrorText
ENDIF
// Try it with an invalid signature:
nSuccess := oRsa:VerifyStringENC(cStrData, "sha256", "not a valid sig")
IF (nSuccess == 1)
? "Signature verified!"
ELSE
? "Signature validation failed!"
ENDIF
// Try it with invalid data:
nSuccess := oRsa:VerifyStringENC("Not the original data", "sha256", cHexSig)
IF (nSuccess == 1)
? "Signature verified!"
ELSE
? "Signature validation failed!"
ENDIF
oRsa:destroy()
oPrivKey:destroy()
oPubKey:destroy()