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

Duplicate openssl rsautl -verify -inkey pubKey.pem -pubin -in rsautl.sig -out originalFile.dat

See more OpenSSL Examples

Demonstrates how to duplicate this OpenSSL command:
openssl rsautl -verify -inkey pubKey.pem -pubin -in rsautl.sig -out originalFile.dat
The signature file contains the contents of the original data, and the original data is output to "originalFile.dat".

The openssl rsautl command signs the data directly, and therefore the maximum amount of data that can signed is very small. (To sign any amount of data, use openssl dgst -sign.) The maximum amount of data that can be signed (with rsautl) is the key size minus the overhead for the padding. The overhead size depends on the padding. With OAEP padding, the overhead is 42 bytes. With the default PKCSv1.5 padding, the overhead is 11 bytes.

Given a 2048-bit RSA key (256 bytes) and using PKCSv1.5 padding, the max data size that can be signed is 256 - 11 = 245 bytes.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

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

local pubKey = chilkat.PublicKey.new()

-- Load the public key from an PEM file:
local success = pubKey:load_from_file("pubKey.pem")
if not success then
    print(pubKey.last_error_text)
    return
end

-- Load the signature.
local bd = chilkat.BinData.new()
success = bd:load_file("rsautl.sig")

local rsa = chilkat.Rsa.new()

-- Import the public key into the RSA component:
success = rsa:use_public_key(pubKey)
if not success then
    print(rsa.last_error_text)
    return
end

-- OpenSSL uses big-endian.
rsa.little_endian = false

-- If the signature is verified, then the signature contents of bd
-- are replaced with the extracted original data.
success = rsa:verify_raw_bd(bd)
if not success then
    print(rsa.last_error_text)
    print("The signature was invalid.")
    return
end

print("The signature was verified.")

-- Save the original data that was extracted from the signature:
success = bd:write_file("originalFile.dat")
if not success then
    print("Failed to save extracted data.")
    return
end