Sheerpower Logo

F.10  Random Byte Generator for Cryptographic Use


Random Byte Generator for Cryptographic Use

This tutorial introduces rndbytes$(), a built-in SheerPower function that returns a string of cryptographically secure random binary bytes. It's especially useful for cryptographic applications like One-Time Pad (OTP) encryption.

Syntax:

otp_key$ = rndbytes$(length)

Parameters:

  • length — the number of random binary bytes to generate.

Returns:

A string containing exactly length cryptographically random binary bytes.

Example Use Case:

In this example, we’ll use rndbytes$() to create a one-time pad key for encrypting a message. The encrypted result is then Base64-encoded to make it email-safe.

phrase$ = "meet me in the Philippines" otp_key$ = rndbytes$(len(phrase$)) encrypted$ = xor$(phrase$, otp_key$) for_emailing$ = base64encode$(encrypted$) print 'Original: '; phrase$ print 'Base64 Encrypted: '; for_emailing$ // Now decrypt encrypted_binary$ = base64decode$(for_emailing$) decrypted_phrase$ = xor$(encrypted_binary$, otp_key$) print 'Decrypted: '; decrypted_phrase$

This encrypts the phrase by XORing it with a random byte string of the same length — a method known as One-Time Pad (OTP) encryption.

The result is then encoded in Base64 so it can be safely included in emails or transmitted over text-based channels. To decrypt, simply decode the base64 and then apply xor$() again using the same key.

Step Value
Original Phrase "meet me in the Philippines"
rndbytes$(len(phrase$)) e.g., binary: \xA1\x03\xFF\x2C...
xor$(phrase$, otp_key$) Encrypted binary string
base64encode$(...) "oQI/LC..." (email-safe output)
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.