Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

// 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.
loPrivKey = createobject("CkPrivateKey")
llSuccess = loPrivKey.LoadEncryptedPemFile("qa_data/pem/rsa_passwd.pem","passwd")
if (llSuccess = .F.) then
    ? loPrivKey.LastErrorText
    release loPrivKey
    return
endif

loRsa = createobject("CkRsa")
loRsa.UsePrivateKey(loPrivKey)

// We have some binary data (in hex) to sign
lcOriginalData = "0102030405060708090A"
loBd = createobject("CkBinData")
loBd.AppendEncoded(lcOriginalData,"hex")

// If successful, the contents of bd are replaced with the RSA signature.
llSuccess = loRsa.SignRawBd(loBd)
if (llSuccess = .F.) then
    ? loRsa.LastErrorText
    release loPrivKey
    release loRsa
    release loBd
    return
endif

// Show the RSA signature in base64
? loBd.GetEncoded("base64")

// ------------------------------------------
// Get the public key from the private key
loPubKey = createobject("CkPublicKey")
loPrivKey.ToPublicKey(loPubKey)

// Verify the signature and extract the original data.
loRsa2 = createobject("CkRsa")
loRsa2.UsePublicKey(loPubKey)

llBVerified = loRsa2.VerifyRawBd(loBd)
? "signature verified: " + str(llBVerified)

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


release loPrivKey
release loRsa
release loBd
release loPubKey
release loRsa2