Sample code for 30+ languages & platforms
PowerBuilder

Duplicating OpenSSL rsautl (creating RSA signatures)

See more RSA Examples

Demonstrates how to duplicate OpenSSL rsautil RSA signatures.

The Chilkat RSA component's methods for creating RSA signatures (SignBytes, SignBytesENC, SignString, and SignStringENC) are very different from OpenSSL's rsautl command. First, we'll explain what Chilkat's signing methods do, and then what OpenSSL's rsautl does. New signing methods have been added to Chilkat RSA to duplicate OpenSSL rsautl: OpenSslSignBytes, OpenSslSignBytesENC, OpenSslSignString, and OpenSslSignStringENC.

Here's what Chilkat's RSA Sign* methods do:
(Note: Chilkat RSA's Sign* methods generate signatures according to RSA Laboratories' "PKCS #1 v2.0: RSA Cryptography Standard".)

  1. Input data is hashed using the hashing algorithm specified.
  2. The hash is padded using either PKCS1 v1.5 or PKCS1 PSS padding. PKCS v1.5 involves encoding to ASN.1 before padding.
  3. The padded hash is finally RSA signed (i.e. modular exponentiation) and the signature is returned.

OpenSSL rsautl is very different. Here's what it does:

  1. The input data is not hashed. It must be a small enough amount of data such that it can be padded and signed. PKCS v1.5 padding is always used. The data is not ASN.1 encoded before padding.
  2. The PKCS v1.5 padded data is RSA signed and the signature is returned.
The new OpenSsl* methods are designed to duplicate OpenSSL's rsautil signature functionality.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_PrivKey
oleobject loo_Rsa
string ls_StrData
oleobject loo_Bd
string ls_HexSig
oleobject loo_PubKey
oleobject loo_Rsa2
oleobject loo_Bd2
string ls_OriginalData

li_Success = 0

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

loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")
if li_rc < 0 then
    destroy loo_PrivKey
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Load the private key from an RSA PEM file:
li_Success = loo_PrivKey.LoadPemFile("private.pem")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_PrivKey
    return
end if

loo_Rsa = create oleobject
li_rc = loo_Rsa.ConnectToNewObject("Chilkat.Rsa")

li_Success = loo_Rsa.UsePrivateKey(loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Rsa.LastErrorText
    destroy loo_PrivKey
    destroy loo_Rsa
    return
end if

ls_StrData = "secret"
loo_Bd = create oleobject
li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData")

loo_Bd.AppendString(ls_StrData,"utf-8")

li_Success = loo_Rsa.SignRawBd(loo_Bd)
ls_HexSig = loo_Bd.GetEncoded("hex")
Write-Debug ls_HexSig

// Recover the data using the corresponding public key:
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")

// Load the public key from a PEM file:
li_Success = loo_PubKey.LoadFromFile("public.pem")
if li_Success = 0 then
    Write-Debug loo_PubKey.LastErrorText
    destroy loo_PrivKey
    destroy loo_Rsa
    destroy loo_Bd
    destroy loo_PubKey
    return
end if

loo_Rsa2 = create oleobject
li_rc = loo_Rsa2.ConnectToNewObject("Chilkat.Rsa")

li_Success = loo_Rsa2.UsePublicKey(loo_PubKey)
if li_Success = 0 then
    Write-Debug loo_Rsa2.LastErrorText
    destroy loo_PrivKey
    destroy loo_Rsa
    destroy loo_Bd
    destroy loo_PubKey
    destroy loo_Rsa2
    return
end if

loo_Bd2 = create oleobject
li_rc = loo_Bd2.ConnectToNewObject("Chilkat.BinData")

loo_Bd2.AppendEncoded(ls_HexSig,"hex")

// Recover the original data.
li_Success = loo_Rsa2.VerifyRawBd(loo_Bd2)
if li_Success = 0 then
    Write-Debug loo_Rsa2.LastErrorText
    destroy loo_PrivKey
    destroy loo_Rsa
    destroy loo_Bd
    destroy loo_PubKey
    destroy loo_Rsa2
    destroy loo_Bd2
    return
end if

ls_OriginalData = loo_Bd2.GetString("utf-8")
Write-Debug ls_OriginalData


destroy loo_PrivKey
destroy loo_Rsa
destroy loo_Bd
destroy loo_PubKey
destroy loo_Rsa2
destroy loo_Bd2