Capricorn 76
Shows some of the cryptographic functions available from ScriptENGINE.
Cryptography provides a way to encrypt and decrypt sensitive data.
This could be stuff you're sending across a network, or data you're reading/writing to disk.

The cryptographic interface also exposes some utility functions like
compression, checksums, hashing and message authentication.

---------------------------
---------------------------
-- Cryptography example
-- Copyright (c) 2007-09 Capricorn 76 Pty. Ltd.
--
-- Shows some of the cryptographic functions available from ScriptENGINE.
-- Cryptography provides a way to encrypt and decrypt sensitive data.
-- This could be stuff you're sending across a network, or data you're reading/writing to disk.
--
-- The cryptographic interface also exposes some utility functions like
-- compression, checksums, hashing and message authentication.
--
-- NOTE:
-- The ./Demos/runDemo.e76script is a utility script used to run all the examples.
-- It loads the world, calls the OnWorldLoad(), OnWorldUnload() functions, and runs the graphics engine loop, waiting for the user to press escape.
---------------------------
---------------------------

---------------------------
-- Load the cryptographic module into the engine
---------------------------
require('luacrypto');

local subGui;

---------------------------
-- runDemo.e76script calls this function when the world is loaded
---------------------------
function OnWorldLoad(worldName)
    --print('OnWorldLoad');

    ---------------------------
    -- The SDK world editor creates a worldIds.e76script when the world is saved. It contains all the entity ids.
    -- We load the IDs so that we can refer to the world entities using friendly names.
    ---------------------------
    IApp:loadScript(IWorld:getNameLong() .. '/worldIds');

    -- Load example's GUI
    OnWorldLoadGui();

    ---------------------------
    -- Subscribe to graphics 'gui' events.
    -- Our callback will be fired by the graphics engine,
    -- whenever a gui event occurs (eg. button clicks).
    -- We will handle the callback and perform our crypto test accordingly.
    --
    -- The returned identifier is later used to unsubscribe.
    ---------------------------
    subGui = IEvents:subscribeGui('OnGui');
end

---------------------------
-- Load this example's GUI
---------------------------
function OnWorldLoadGui()
    ---------------------------
    -- Let's show some help text
    ---------------------------
    IGraphics:loadGui('crypto');
end

---------------------------
-- runDemo.e76script calls this function when the world is unloaded
---------------------------
function OnWorldUnload(worldName)
    --print('OnWorldUnload');

    ---------------------------
    -- Unsubscribe events
    ---------------------------
    IEvents:unsubscribe(subGui);
end

------------------------------
-- PRIVATE FUNCTION
-- Add text to graphical list.
------------------------------
local function writeToList(text)
    IGraphics:addListboxItem('listOutput', text);
end
------------------------------
-- Graphics gui event callback function.
-- The graphics engine will call this function whenever a gui event occurs (eg. button click)
------------------------------
function OnGui(callerId, eventType, userData)

    -------------------------------
    -- Is the Encrypt button clicked?
    -- The button id ('buttonEncrypt') was defined when the button was created (using the SDK GUI editor).
    -- We're using the buttonClicked() utility function to test if the event was a button click,
    -- but we could also load the constantsGuiEventTypes.e76script and compare the eventType with the appropriate event.
    -------------------------------
    if (IGraphics:buttonClicked('buttonEncrypt', callerId, eventType)) then
        -------------------------------
        -- The input string is unicode (a wide-string).
        -- Each character takes 2x the space of a normal string.
        -- Alternatively, convert to a normal string.
        -- The crypto library can handle both string types...
        -------------------------------
        --input = tostring(IGraphics:getControlText('editMessage'));
        input = IGraphics:getControlText('editMessage');

        -------------------------------
        -- Prepare the listbox for the data
        -------------------------------
        IGraphics:clearListbox('listOutput');

        writeToList('Testing hashing algorithms');
        writeToList('----------------------------------------------------');

        -------------------------------
        -- Create example checksums, hashes, hmacs.
        -------------------------------
        md2     = luacrypto.md2(input);
        sha160  = luacrypto.sha160(input);
        hmacMD5 = luacrypto.md5_hmac(input, 'this is my other example key');

        -------------------------------
        -- Update the gui.
        -- Show the user our results.
        -------------------------------
        writeToList('MD2 hash: '                    .. luacrypto.hex_encode(md2));
        writeToList('SHA160 hash: '                 .. luacrypto.hex_encode(sha160));
        writeToList('HMAC authentication code: '    .. luacrypto.hex_encode(hmacMD5));

        -------------------------------
        -- Create example private and public RSA keys & use them to encrypt/decrypt data
        -- NB: RSA is limited to encrypting input data that is smaller than the RSA key length.
        --     It's useful when encrypting/decrypting message authentication codes etc. rather than an entire message.
        -------------------------------
        writeToList('----------------------------------------------------');
        writeToList('RSA Encryption/Decryption');
        writeToList('----------------------------------------------------');

        publicRSAKey, privateRSAKey = luacrypto.rsa_gen_keys(512, 231);

        -------------------------------
        -- Printing the keys to the gui
        -------------------------------
        writeToList('RSA public/private keys created');
        print('Public RSA Key:' .. publicRSAKey);
        print('Private RSA Key:'    .. privateRSAKey);

        -------------------------------
        -- Encrypt the input user message with the public key.
        -- Then decrypt it using the private key...
        --
        -- Public/Private key encryption is a very powerful cryptographic algorithm
        -- and ScriptENGINE makes it simple for you to use it in your applications.
        -- Your public key is free to distribute to recipients, while your
        -- private key should never be compromised.
        --
        -- If someone wants to send you encrypted data, they use your public key to encrypt.
        -- Only your private key will be able to decrypt the data...
        -------------------------------
        writeToList('Encrypting/decrypting using public/private keys');

        encryptRSAdata      = luacrypto.rsa_encrypt(input, publicRSAKey, true);
        decryptRSAdata      = luacrypto.rsa_decrypt(encryptRSAdata, privateRSAKey, false);

        writeToList('Decrypted RSA data:' .. decryptRSAdata);

        -------------------------------
        -- Encrypt/Decrypt using DES algorithm
        -- DES is a symmetric cipher algorithm, meaning that the
        -- key used to the decrypt the data is the same as encrypting.
        --
        -- This is more like what we're used to in the real world,
        -- where a padlock requires the same key to lock/unlock.
        --
        -- Symmetric cipher algorithms suffer from the
        -- problem of transmitting the secret key to recipients
        -- without compromising it... but they are much faster than
        -- the public/private cipher algorithms...
        --
        -- Some networking systems use a public/private key to establish a
        -- connection, create a symmetric 'session' key and transmit it,
        -- and then use the symmetric key to encrypt/decrypt data.
        --
        -- Symmetric ciphers are typically much faster that public/private key algorithms.
        -------------------------------
        writeToList('----------------------------------------------------');
        writeToList('DES Encryption/Decryption');
        writeToList('----------------------------------------------------');

        encrypt     = luacrypto.des112_encrypt(input, 'MyPasswordForDes');
        decrypt     = luacrypto.des112_decrypt(encrypt, 'MyPasswordForDes');

        -------------------------------
        -- Print DES ecryption to the gui
        -------------------------------
        writeToList('Encrypted DES:'    .. luacrypto.hex_encode(encrypt));
        writeToList('Decrypted DES:'    .. decrypt);

        -------------------------------
        -- Encrypt/decrypt using the ARC4 symmetric cipher algorithm.
        -------------------------------
        writeToList('----------------------------------------------------');
        writeToList('ARC4 Encryption/Decryption');
        writeToList('----------------------------------------------------');

        encrypt     = luacrypto.arc4_crypt(input, 'MyPassword');
        decrypt     = luacrypto.arc4_crypt(encrypt, #encrypt, 'MyPassword');

        -------------------------------
        -- Print ARC4 results to the gui
        -------------------------------
        writeToList('Encrypted ARC4:'    .. luacrypto.hex_encode(encrypt));
        writeToList('Decrypted ARC4:' .. decrypt);
    end
end

Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:12 2009)