Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

RSA Sign Binary Data and Verify (Recover the Original Data)

See more RSA Examples

Demonstrates how to RSA sign binary data and then verify/recover the original data.

Note: This example uses methods introduced in Chilkat v9.5.0.77.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPrivKey
LOCAL oRsa
LOCAL cOriginalData
LOCAL oBd
LOCAL oPubKey
LOCAL oRsa2
LOCAL nBVerified

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  Load an RSA private key for signing.
oPrivKey := CreateObject("Chilkat.PrivateKey")
nSuccess := oPrivKey:LoadEncryptedPemFile("qa_data/pem/rsa_passwd.pem", "passwd")
IF (nSuccess == 0)
    ? oPrivKey:LastErrorText
    oPrivKey:destroy()
    RETURN
ENDIF

oRsa := CreateObject("Chilkat.Rsa")
oRsa:UsePrivateKey(oPrivKey)

//  We have some binary data (in hex) to sign
cOriginalData := "0102030405060708090A"
oBd := CreateObject("Chilkat.BinData")
oBd:AppendEncoded(cOriginalData, "hex")

//  If successful, the contents of bd are replaced with the RSA signature.
nSuccess := oRsa:SignRawBd(oBd)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oPrivKey:destroy()
    oRsa:destroy()
    oBd:destroy()
    RETURN
ENDIF

//  Show the RSA signature in base64
? oBd:GetEncoded("base64")

//  ------------------------------------------
//  Get the public key from the private key
oPubKey := CreateObject("Chilkat.PublicKey")
oPrivKey:ToPublicKey(oPubKey)

//  Verify the signature and extract the original data.
oRsa2 := CreateObject("Chilkat.Rsa")
oRsa2:UsePublicKey(oPubKey)

nBVerified := oRsa2:VerifyRawBd(oBd)
? "signature verified: " + Str(nBVerified)

//  Show the original data:
? "original data: " + oBd:GetEncoded("hex")

oPrivKey:destroy()
oRsa:destroy()
oBd:destroy()
oPubKey:destroy()
oRsa2:destroy()