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

RSA Encrypt with Modulus and Exponent

See more RSA Examples

Demonstrates how to RSA encrypt with a given modulus and exponent.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oRsa
LOCAL cModulus
LOCAL cExponent
LOCAL oXml
LOCAL oPubKey
LOCAL nUsePrivateKey
LOCAL cPlainText
LOCAL cEncryptedStrBase64

nSuccess := 0

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

oRsa := CreateObject("Chilkat.Rsa")

//  Assuming you already have a base64 modulus and exponent,
//  wrap it in XML like this:
cModulus := "qMBRpdYrAy5aMmo31NErUizh5sbweguSmh4wlK6uJEIDl+kwTlROnE34KOFExeTbJSX0WygPi+vWl0yNq7buIMUKpytossAAWut5khO3CQJxTk7G2gnEPNUUXHiExGgNrLzcSLv8YIlfVALhoRWyC67KOL+a+3taNq3h+BHeWhM="
cExponent := "AQAB"

oXml := CreateObject("Chilkat.Xml")
oXml:Tag := "RSAPublicKey"
oXml:NewChild2("Modulus", cModulus)
oXml:NewChild2("Exponent", cExponent)

oPubKey := CreateObject("Chilkat.PublicKey")
nSuccess := oPubKey:LoadFromString(oXml)
IF (nSuccess == 0)
    ? oPubKey:LastErrorText
    oRsa:destroy()
    oXml:destroy()
    oPubKey:destroy()
    RETURN
ENDIF

nSuccess := oRsa:UsePublicKey(oPubKey)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oRsa:destroy()
    oXml:destroy()
    oPubKey:destroy()
    RETURN
ENDIF

nUsePrivateKey := 0
cPlainText := "message in a bottle"

oRsa:EncodingMode := "base64"
cEncryptedStrBase64 := oRsa:EncryptStringENC(cPlainText, nUsePrivateKey)
? cEncryptedStrBase64

oRsa:destroy()
oXml:destroy()
oPubKey:destroy()