Skip to content

Repository files navigation

RSA Encryption Program in Assembly

A multi-architecture implementation of RSA encryption in assembly language, supporting x86_64 and ARM64 processors.

License: MIT Architectures Platform

RSA Mechanism

Table of Contents

Overview

Submitted by:

Name Role
Francis James Lagang Student
Margaret Grace Docdoc Student
Simone Montañez Student

Final Project for CS 3103, DCISM, University of San Carlos. December 2024.

What is RSA?

RSA is a public-key cryptosystem widely used for secure data transmission. It is based on the practical difficulty of factoring the product of two large prime numbers.

Note

This implementation is for educational purposes and demonstrates the basic principles of RSA encryption using small prime numbers.

Key Components

In this implementation, we use:

Component Value Description
p 7 First prime number
q 13 Second prime number
n 91 Modulus (p × q)
φ(n) 72 Euler's totient ((p-1) × (q-1))
e 5 Public exponent
d 29 Private exponent

How RSA Works

  1. Key Generation

    graph LR
       A["Choose p,q"] --> B["Calculate n = p×q"]
       B --> C["Calculate phi(n)"]
       C --> D["Choose e"]
       D --> E["Calculate d"]
    
    Loading
  2. Encryption

    $$c = m^e \bmod n$$

    Where:

    • m is the message
    • c is the ciphertext
  3. Decryption

    $$m = c^d \bmod n$$

    Where:

    • c is the ciphertext
    • m is the original message

System Requirements

This implementation supports multiple architectures:

  • macOS (x86_64)
  • Linux (x86_64)
  • ARM64 systems (Apple Silicon, etc.)

Architecture Support

The repository includes three versions:

File Architecture Purpose
rsa-encrypt.asm x86_64 macOS native
rsa-encrypt-linux-x86_64.asm x86_64 Linux/Docker
rsa-encrypt-arm64.asm ARM64 Docker ARM

Important

All versions implement identical RSA encryption logic but use architecture-specific assembly instructions and system calls.

Running with Docker

For x86_64 Systems

# Build the image
docker build -f Dockerfile.x86_64 -t rsa-encrypt-x86 .

# Run the container
docker run -it rsa-encrypt-x86

For ARM64 Systems (e.g., Apple Silicon Macs)

# Build the image
docker build -t rsa-encrypt-arm .

# Run the container
docker run -it rsa-encrypt-arm

Tip

The Docker containers work on any system with Docker installed, regardless of the OS.

Program Features

  • Two-digit number input (0-99)
  • RSA parameter display
  • Real-time encryption
  • Instant decryption
  • Input validation

How to Use

Native macOS Build (x86_64)
# Assemble
nasm -f macho64 rsa-encrypt.asm

# Link
ld -o rsa-encrypt rsa-encrypt.o -macosx_version_min 10.12 -no_pie -L/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib -lSystem

# Run
./rsa-encrypt

Examples

Example 1: Regular Case
RSA Parameters:
  p (first prime) = 7
  q (second prime) = 13
  n (modulus) = 91 (7 × 13)
  e (public exponent) = 5
  d (private exponent) = 29
  φ(n) = 72 = (7-1) × (13-1)
------------------------
Enter first digit (0-9): 5
Enter second digit (0-9): 3
Original number: 53
Encrypted number: 79
Decrypted number: 53
Example 2: Fixed Point Case
Original number: 21
Encrypted number: 21
Decrypted number: 21

[!NOTE]
This is a "fixed point" where the number encrypts to itself.

Python Verification Code

A Python script (rsa_verify.py) is included to verify the RSA encryption and decryption operations. This script shows the step-by-step calculations and verifies the results using Python's built-in functions.

Running the Python Script

python3 rsa_verify.py

Test Cases

The script includes three test cases:

  1. Message = 11
    • Shows standard RSA encryption/decryption
  2. Message = 53
    • Demonstrates encryption with a larger number
  3. Message = 21
    • Demonstrates a "fixed point" where the encrypted value equals the original message
    • This occurs because 21^5 mod 91 = 21

Each test case shows:

  • Step-by-step encryption process
  • Final encrypted value
  • Decryption verification
  • Comparison between manual calculation and Python's built-in function

Documentation and Demonstration

Console Results

Console Results The image above shows the assembly program's output for test cases:

  • Input: 11 -> Encrypted: 72
  • Input: 53 -> Encrypted: 79
  • Input: 21 -> Encrypted: 21 (fixed point)

Python Verification

Python Verification The Python verification script confirms the assembly program's results, showing:

  • Step-by-step encryption process
  • Matching results between assembly and Python implementations
  • Successful decryption back to original values

Program Demo

Watch Program Demo

This video demonstrates:

  • How to compile and run the assembly program
  • Interactive input/output process
  • Real-time encryption results

Security Note

Caution

This is an educational implementation. Production RSA systems require:

  • Much larger prime numbers (2048+ bits)
  • Proper padding schemes
  • Secure random number generation
  • Additional security measures

University of San Carlos - Department of Computer and Information Sciences and Mathematics

About

A NASM Assembly program that takes 2 digits then encrypts them using RSA algorithm. Note that this is much simpler version of the algorithm.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages