Example

Cloaking (Encrypting) Strings

Author: Carl Sassenrath
Return to REBOL Cookbook

Newer versions of REBOL include "cloaking" functions for encrypting and decrypting strings. These functions do not provide full strength encryption such as Blowfish, AES, or RSA as found in REBOL/Command, nevertheless they can be useful for hiding passwords and other values. (That's why we call it cloaking rather than encrypting.)

To cloak a string, provide the string and a cloaking key to the ENCLOAK function:


    str: encloak "This is a string" "a-key"

The result is an encrypted string which can be decloaked with the line:


    print decloak str "a-key"

Now you have a simple way save out a hidden string, such as a password:


    key: ask "Cloak key? (do not forget it) "
    data: "string to hide"
    save %data to-binary encloak data key

Note that the cloaked string is converted to a binary value because it may contain special characters that would cause problems when used as text.

To read the data and decloak it:


    key: ask "Cloak key? "
    data: load %data
    data: to-string decloak data key

Of course you can cloak any kind of data using these functions, even binary data such as programs, images, sounds, etc. In those cases you do not need the TO-BINARY and TO-STRING conversions shown above.

Do Not Forget The Key

Keep track of your keys! If you lose your key you may not be able to recover your data. (The key string is SHA1 encoded, which is a strong hashing method.)

It's best to make a habit of writing the key down somewhere safe where you can find it when you forget it. If you don't then be prepared to lose your data (when you forget your key).


2006 REBOL Technologies REBOL.com REBOL.net