Blog | G5 Cyber Security

Asymmetric Encryption & Symmetric Keys: Security Risks

TL;DR

Wrapping asymmetric encryption (like RSA) around symmetric encryption is a common and generally good practice. However, it’s not foolproof. Incorrect implementation can introduce serious vulnerabilities. This guide explains the risks and how to avoid them.

Understanding the Approach

You typically use asymmetric encryption to securely exchange a randomly generated symmetric key (like AES). Then, you use that symmetric key for the bulk of your data encryption/decryption because it’s much faster. The risk isn’t in the concept itself, but in how you do it.

Risks & Solutions

  1. Padding Oracle Attacks (RSA specifically)
  • Key Exchange Vulnerabilities
  • Weak Random Number Generation (RNG)
  • Insufficient Key Lengths
  • Side-Channel Attacks
  • Example Scenario (Python with PyCryptodome)

    This shows a basic, secure example using RSA to encrypt an AES key and then use AES for data encryption.

    from Cryptodome.Cipher import PKCS1_OAEP, AES
    import os
    
    # Generate a random AES key
    key = os.urandom(16) # 128-bit AES key
    
    # RSA public and private keys (obtained securely elsewhere)
    pub_key, priv_key = None, None  # Replace with actual key loading code
    
    # Encrypt the AES key using RSA
    cipher_rsa = PKCS1_OAEP.new(pub_key)
    encrypted_key = cipher_rsa.encrypt(key)
    
    # Decrypt the AES key using RSA
    decipher_rsa = PKCS1_OAEP.new(priv_key)
    decrypted_key = decipher_rsa.decrypt(encrypted_key)
    
    # Encrypt data with AES
    cipher_aes = AES.new(decrypted_key, AES.MODE_CBC) # Use a secure mode like CBC
    plaintext = b'This is secret data'
    ciphertext = cipher_aes.encrypt(plaintext)
    
    print("Original Data:", plaintext)
    print("Encrypted Data:", ciphertext)
    

    Important Considerations

    Exit mobile version