Encryption Fundamentals: Understanding RSA and AES

Encryption Fundamentals: Understanding RSA and AES

Aymen Isfiaya
March 15, 2024
4 min read

Encryption Fundamentals: Understanding RSA and AES

In today's digital world, encryption is the backbone of secure communication. Whether you're sending a message, storing sensitive data, or browsing the web, encryption ensures that only authorized parties can access the information. Two of the most widely used encryption methods are RSA (asymmetric) and AES (symmetric), each with its own strengths and ideal use cases.

Introduction to Encryption Types

RSA is an asymmetric encryption algorithm that uses a pair of keys (public and private) for secure data exchange.

AES is a symmetric encryption algorithm that uses a single key for both encryption and decryption, making it faster for large data.

This guide will break down how RSA and AES work, compare their differences, and explain when to use each with practical code examples in JavaScript.

What is RSA Encryption?

How RSA Works

RSA (Rivest-Shamir-Adleman) is an asymmetric encryption algorithm, meaning it uses two mathematically linked keys:

  • Public Key: Used to encrypt data (can be shared openly).
  • Private Key: Used to decrypt data (must be kept secret).

RSA relies on the difficulty of factoring large prime numbers. Here's a simplified flow:

  1. Key Generation: Two large prime numbers are multiplied to create a public-private key pair.
  2. Encryption: Anyone can encrypt data using the public key.
  3. Decryption: Only the private key holder can decrypt the data.

Common Use Cases

  • SSL/TLS handshakes (used in HTTPS)
  • Digital signatures (verifying message authenticity)
  • Secure key exchange (e.g., sending an AES key securely)

RSA Key Generation Example (JavaScript)

const { generateKeyPairSync } = require('crypto');

// Generate RSA key pair (2048-bit)
const { publicKey, privateKey } = generateKeyPairSync('rsa', {
  modulusLength: 2048,
  publicKeyEncoding: { type: 'spki', format: 'pem' },
  privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
});

console.log('Public Key:\n', publicKey);
console.log('\nPrivate Key:\n', privateKey);

What is AES Encryption?

How AES Works

AES (Advanced Encryption Standard) is a symmetric encryption algorithm, meaning the same key is used for both encryption and decryption. It processes data in fixed-size blocks (128 bits) and supports key sizes of 128, 192, or 256 bits (AES-256 being the most secure).

Common block cipher modes:

  • CBC (Cipher Block Chaining): Requires an Initialization Vector (IV) for randomness.
  • GCM (Galois/Counter Mode): Provides both encryption and authentication.

Common Use Cases

  • File and disk encryption (e.g., BitLocker)
  • Database security (encrypting sensitive fields)
  • HTTPS data encryption (after RSA key exchange)

AES Encryption Example (JavaScript)

const { createCipheriv, createDecipheriv, randomBytes } = require('crypto');

// Generate a random key and IV
const key = randomBytes(32); // AES-256 key (32 bytes)
const iv = randomBytes(16); // Initialization Vector (16 bytes)

// Encrypt
const cipher = createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update('Hello, AES!', 'utf8', 'hex');
encrypted += cipher.final('hex');

console.log('Encrypted:', encrypted);

// Decrypt
const decipher = createDecipheriv('aes-256-cbc', key, iv);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');

console.log('Decrypted:', decrypted);

RSA vs. AES: Key Differences

FeatureRSA (Asymmetric)AES (Symmetric)
Key TypePublic & PrivateSingle Shared Key
SpeedSlow (for large data)Fast
Use CaseKey exchange, digital signaturesBulk data encryption
SecurityRelies on factoring primesRelies on key secrecy

When to Use RSA vs. AES

Use RSA for:

  • Securely exchanging keys (e.g., sending an AES key)
  • Digital signatures (verifying sender identity)

Use AES for:

  • Encrypting large files or databases
  • Real-time communication (e.g., HTTPS data transfer)

Hybrid Encryption (RSA + AES Together)

Most modern systems (like HTTPS) use both RSA and AES:

  1. RSA exchanges an AES key securely
  2. AES encrypts the actual data for efficiency

Hybrid Encryption Example (Conceptual Flow)

  1. Client generates an AES key
  2. Client encrypts the AES key with the server's RSA public key
  3. Server decrypts the AES key using its RSA private key
  4. Both parties now use AES for fast, secure communication

Conclusion

RSA is ideal for secure key exchange and digital signatures but is slow for large data.

AES is blazing fast for encrypting bulk data but requires secure key sharing.

Best Practice: Use RSA to exchange an AES key, then switch to AES for data encryption (hybrid approach).

By understanding these encryption methods, you can implement the right security strategy for your applications.

Related Articles