Up to this point, our RSA experiments used small integer messages such as: message = 9
But RSA is useful because computers ultimately represent files as bytes. So let’s make the experiment more tangible by creating an actual text file:
echo "This is the original message." > plain.txtThen we will use our deliberately tiny RSA key:
p = 11
q = 13
n = 143
e = 7
d = 103
Public key = (7, 143)
Private key = (103, 143)We will:
- Read
plain.txt. - Encrypt it with the public key.
- Look inside
cipher.txt. - Decrypt it with the private key.
- Compare the recovered text with the original.
- Create a toy RSA signature for the file.
- Store the signature separately and also create a human-readable signed copy.
- Verify the signature with the public key.
- Modify the message and see verification fail in the normal case.
Important: This is textbook RSA built specifically for learning. An 8-bit RSA key, byte-at-a-time RSA encryption, and the toy checksum used below provide no real security.
9.1 Create Plaintext#
Run:
echo "This is the original message." > plain.txtInspect it:
cat plain.txtResult:
This is the original message.Because
echoadds a newline, the file actually contains the sentence followed by a newline byte.
9.2 Encrypt by Bytes#
We will encrypt each byte in the plain text file byte-by-byte. Why can’t we encrypt the entire file as one number?
Our modulus is:
n = 143For our simple textbook RSA operation, each message integer must satisfy:
0 <= m < 143The entire text file converted into one integer would be vastly larger than 143.
So for this deliberately tiny experiment, we encrypt each byte separately:
plain.txt
|
v
byte byte byte byte ...
| | | |
v v v v
RSA RSA RSA RSA ...
| | | |
v v v v
numbers in cipher.txtThis works for our chosen ASCII message because its byte values are below 143.
It is not a general way to encrypt arbitrary files. i
UTF-8 and binary files can contain byte values up to 255, which are too large for n=143.
9.3 RSA-8 Demo#
Save this as:
rsa8_file_demo.py# rsa8_file_demo.py
# Educational textbook RSA only.
# DO NOT use this for real security.
# ------------------------------------------------------------
# Our RSA-8 key
# ------------------------------------------------------------
p = 11
q = 13
n = 143
e = 7
d = 103
# ------------------------------------------------------------
# Read plaintext
# ------------------------------------------------------------
with open("plain.txt", "rb") as f:
plaintext = f.read()
print("Original bytes:")
print(plaintext)
# ------------------------------------------------------------
# Encrypt one byte at a time
# ciphertext = message^e mod n
# ------------------------------------------------------------
ciphertext = []
for byte in plaintext:
if byte >= n:
raise ValueError(
f"Byte {byte} is too large for RSA modulus {n}"
)
encrypted = pow(byte, e, n)
ciphertext.append(encrypted)
# Store decimal numbers deliberately so we can inspect them.
with open("cipher.txt", "w") as f:
f.write(" ".join(str(x) for x in ciphertext))
print()
print("Ciphertext:")
print(ciphertext)
# ------------------------------------------------------------
# Decrypt one integer at a time
# plaintext = ciphertext^d mod n
# ------------------------------------------------------------
decrypted = []
for encrypted in ciphertext:
byte = pow(encrypted, d, n)
decrypted.append(byte)
decrypted_bytes = bytes(decrypted)
with open("decrypted.txt", "wb") as f:
f.write(decrypted_bytes)
print()
print("Decrypted:")
print(decrypted_bytes)
# ------------------------------------------------------------
# Create a TOY message representative for signing
# ------------------------------------------------------------
#
# A real RSA signature uses a cryptographic hash and a defined
# signature scheme such as RSA-PSS.
#
# Our n=143 is far too small for that. For the experiment only,
# reduce the sum of all bytes into the RSA message space.
message_value = sum(plaintext) % n
# ------------------------------------------------------------
# Toy textbook RSA signature
# signature = message_value^d mod n
# ------------------------------------------------------------
signature = pow(message_value, d, n)
with open("signature.txt", "w") as f:
f.write(str(signature))
# ------------------------------------------------------------
# Create a human-readable signed copy
# ------------------------------------------------------------
with open("plain.signed.txt", "wb") as f:
f.write(plaintext)
f.write(b"\n--- RSA-8 DEMO SIGNATURE ---\n")
f.write(str(signature).encode("ascii"))
f.write(b"\n")
print()
print("Message representative:", message_value)
print("Signature:", signature)
# ------------------------------------------------------------
# Verify signature using public key
# recovered = signature^e mod n
# ------------------------------------------------------------
verified_value = pow(signature, e, n)
print("Value recovered from signature:", verified_value)
if verified_value == message_value:
print("Signature: VALID")
else:
print("Signature: INVALID")9.4 Examine It#
python3 rsa8_file_demo.pyThe script produces:
plain.txt
cipher.txt
decrypted.txt
signature.txt
plain.signed.txtConceptually:
plain.txt
|
+----------+----------+
| |
v v
RSA encryption toy checksum
public (7,143) mod 143
| |
v v
cipher.txt message value
| |
v v
RSA decryption RSA signing
private (103,143) private d=103
| |
v v
decrypted.txt signature.txt
|
v
public verification
e=79.4.1 Inside cipher.txt#
This is one reason for storing the ciphertext as decimal text rather than opaque binary data.
Run:
cat cipher.txtYou will see a sequence such as:
<number> <number> <number> <number> ...Each number corresponds to one plaintext byte after:
c = m^7 mod 143For example, the first character is:
'T'ASCII represents T as decimal:
84Our toy encryption calculates:
84^7 mod 143and stores the resulting integer in cipher.txt.
The next character is independently encrypted, then the next, and so on.
This makes the transformation visible:
plain byte
|
| ^7 mod 143
v
cipher integer9.4.2 Decrypt cipher.txt#
For each encrypted integer, the script performs:
m = c^103 mod 143and converts the recovered values back into bytes.
Inspect the result:
cat decrypted.txtIt should show:
This is the original message.Now compare the files exactly:
cmp plain.txt decrypted.txtIf they are identical, cmp normally produces no output.
You can make the result more explicit:
cmp plain.txt decrypted.txt && echo "Files are identical"Expected:
Files are identicalSo we have now applied our toy RSA key to an actual file:
plain.txt
|
| e = 7
v
cipher.txt
|
| d = 103
v
decrypted.txt
|
v
same content as plain.txt9.4.3 Signing#
Encryption and signing solve different problems.
Our encryption experiment is:
plaintext
|
public operation
|
v
ciphertext
|
private operation
|
v
plaintextThe toy signature experiment instead creates a small representative of the message:
plain.txt
|
v
sum of bytes mod 143
|
v
message representative
|
private operation
|
v
signatureVerification uses the public exponent:
signature
|
| ^7 mod 143
v
recovered representative
|
| compare
v
representative calculated from plain.txtIf they match, our toy verifier reports:
Signature: VALID9.4.4 Detached Signature#
Run:
cat signature.txtThe file contains just our small RSA signature integer:
<signature number>This is a detached signature in the broad conceptual sense:
plain.txt
signature.txtThe document remains separate from its signature.
In real systems, a signature also normally exists separately from the original document or is carried in a defined signed-data/container format.
9.4.5 Signed Copy#
Let look at our human-readable signed copy
For demonstration, the script also creates:
plain.signed.txtInspect it:
cat plain.signed.txtIt will look conceptually like:
This is the original message.
--- RSA-8 DEMO SIGNATURE ---
<signature number>This format is something we invented for this lab. It is not an OpenSSH, PKCS, CMS, PGP, or other standard signed-file format.
The purpose is simply to make the two objects visible:
+--------------------------------+
| This is the original message. |
| |
| --- RSA-8 DEMO SIGNATURE --- |
| <signature> |
+--------------------------------+The original message remains readable. The signature is additional data used to test integrity/authenticity in our toy experiment.
9.5 Message Temparing#
What happens if the message changes?
A useful signature experiment is to keep the old signature and change the message.
For example:
echo "Hacked!" >> plain.txtNow the plaintext bytes have changed.
Therefore our toy message representative will normally change:
old plain.txt
|
v
old representative
|
v
old signature
modified plain.txt
|
v
new representativeThe old signature still recovers the old representative:
old signature^e mod nbut verification compares that against the representative calculated from the modified file.
Normally:
old representative != new representativeand the result is:
Signature: INVALID9.6 Checksum#
Our toy checksum is terrible cryptography:
message_value = sum(plaintext) % 143Many different files can produce exactly the same value.
For example, rearranging bytes does not even change their sum.
So our demonstration can have trivial collisions:
different message A
|
v
same representative
^
|
different message BA real digital signature instead starts with a cryptographic hash such as SHA-256 and then applies a standardized signature scheme.
For RSA, a modern signature construction is RSA-PSS.
Our RSA-8 key simply has nowhere near enough room for a real SHA-256-based RSA-PSS structure.
That limitation is itself useful to understand:
9.7 A Byte-at-a-time#
Why byte-at-a-time in RSA is not real encryption?
Our encryption method exposes another important problem.
If the same plaintext byte appears repeatedly, textbook deterministic RSA produces the same ciphertext integer every time.
For example, every ASCII space has the same byte value:
32and therefore our demo always computes:
32^7 mod 143The output is always the same.
An observer can therefore see repetition patterns in cipher.txt.
Real RSA encryption does not encrypt arbitrary files one byte at a time this way.
Modern RSA encryption uses a randomized encoding scheme such as RSA-OAEP, and RSA is normally used to protect a small secret such as a randomly generated symmetric key rather than directly encrypting an entire large file.
Conceptually:
REAL HYBRID APPROACH
large file
|
| fast symmetric encryption
v
encrypted file
random symmetric key
|
| RSA public key
v
encrypted keyOur RSA-8 experiment intentionally removes all of that complexity so that we can see the underlying modular arithmetic.
9.8 Four files, four lessons#
At the end of the experiment, these files each tell us something different:
| File | Purpose |
|---|---|
plain.txt | Original message |
cipher.txt | Visible textbook-RSA ciphertext integers |
decrypted.txt | Reconstructed plaintext |
signature.txt | Detached toy RSA signature |
plain.signed.txt | Human-readable lab representation of message + signature |
The entire experiment can be summarized as:
RSA-8 LAB
p=11 q=13
\ /
\ /
n=143
/ \
/ \
e=7 d=103
PUBLIC PRIVATE
| |
+---------+--------+--------+---------+
| | |
v v v
SSH-style key encryption signing
representation | |
| v v
| cipher.txt signature.txt
| | |
| v |
| decryption |
| | |
| v |
| decrypted.txt |
| |
+---------------- verification <------+ Revision: 🔑 Key Takeaway#
Our 8-bit key is not secure, but it is mathematically functional.
We have now used the same key material to demonstrate:
p = 11
q = 13
n = 143
e = 7
d = 103Key construction
p * q = n
11 * 13 = 143Public and private exponents
Public = (7,143)
Private = (103,143)Encryption
c = m^7 mod 143Decryption
m = c^103 mod 143Toy signing
signature = representative^103 mod 143Toy verification
recovered = signature^7 mod 143SSH-style public-key serialization
(e,n)
|
v
SSH binary fields
|
v
Base64
|
v
ssh-rsa AAAA...The important distinction is now clear:
MATHEMATICALLY FUNCTIONAL
!=
CRYPTOGRAPHICALLY SECUREOur 8-bit RSA key really performs RSA modular arithmetic. It can encrypt our restricted ASCII-byte experiment, decrypt it, create a textbook RSA signature, verify that signature, and be represented using an SSH-style public-key encoding.
But it is deliberately unsuitable for real security because:
n=143is trivial to factor.- The keyspace is tiny enough to precompute.
- Byte-at-a-time textbook RSA leaks patterns.
- The toy checksum is not collision resistant.
- There is no secure encryption padding such as OAEP.
- There is no real signature scheme such as RSA-PSS.
- Modern SSH implementations correctly refuse such a tiny RSA modulus.
plain.txt -> cipher.txt -> decrypted.txtproves the RSA arithmetic is reversible with the matching keys, whileplain.txt + signature.txt -> verifydemonstrates the private-sign/public-verify relationship; neither makes an 8-bit RSA key secure.


