Sample code for 30+ languages & platforms
B4X 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 B4X Downloads

B4X
Dim success As Boolean = False

'  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.
Dim privKey As ChilkatPrivateKey
privKey.Initialize("privKey")
success = privKey.LoadEncryptedPemFile("qa_data/pem/rsa_passwd.pem", "passwd")
If success = False Then
    Log(privKey.LastErrorText)
    Return
End If


Dim rsa As ChilkatRsa
rsa.Initialize
rsa.UsePrivateKey(privKey)

'  We have some binary data (in hex) to sign
Dim originalData As String = "0102030405060708090A"
Dim bd As ChilkatBinData
bd.Initialize
bd.AppendEncoded(originalData, "hex")

'  If successful, the contents of bd are replaced with the RSA signature.
success = rsa.SignRawBd(bd)
If success = False Then
    Log(rsa.LastErrorText)
    Return
End If


'  Show the RSA signature in base64
Log(bd.GetEncoded("base64"))

'  ------------------------------------------
'  Get the public key from the private key
Dim pubKey As ChilkatPublicKey
pubKey.Initialize
privKey.ToPublicKey(pubKey)

'  Verify the signature and extract the original data.
Dim rsa2 As ChilkatRsa
rsa2.Initialize
rsa2.UsePublicKey(pubKey)

Dim bVerified As Boolean = rsa2.VerifyRawBd(bd)
Log("signature verified: " & bVerified)

'  Show the original data:
Log("original data: " & bd.GetEncoded("hex"))