コンテンツへスキップ
uencoder

大きなデータを暗号化する方法

RSA 鍵サイズより大きなファイルやデータを、ハイブリッド(エンベロープ)暗号化で暗号化します。

问题

大きなファイルを暗号化する必要がありますが、RSA は数百バイトしか暗号化できず、公開鍵暗号の安全性も確保したい。

前提条件

  • AES と RSA の理解
  • ハイブリッド暗号に関する知識

ステップ

  1. 1. Generate a random symmetric key

    Generate a fresh random AES key (256 bits). This key will encrypt the large data. It is called the session key or data encryption key (DEK). A new DEK should be generated for each file.

    入力
    random AES-256 key
    输出
    32 random bytes
  2. 2. Encrypt the data with AES

    Encrypt the large file using AES-GCM with the session key and a fresh IV. AES can handle data of any size efficiently. Store the IV and authentication tag with the ciphertext.

    入力
    large file + AES key
    输出
    AES ciphertext + IV + tag
  3. 3. Encrypt the session key with RSA

    Encrypt the small AES session key using the recipient's RSA public key with OAEP padding. Store the RSA-encrypted session key alongside the AES ciphertext. The recipient decrypts the session key with their private key, then decrypts the file with AES.

    入力
    AES key + RSA public key
    输出
    RSA-encrypted session key

期待される結果

ハイブリッド暗号化パッケージ。大きなデータは AES で、AES 鍵は RSA で暗号化し、両者の長所を組み合わせます。

よくあるエラー

Trying to encrypt the large file directly with RSA
RSA can only encrypt data up to key_size - padding bytes. Use hybrid encryption for anything larger.
Reusing the AES session key across files
Generate a fresh session key for each encryption. Reusing it with the same IV breaks security.

トラブルシューティング指引

  • If the RSA step fails with 'message too long', the session key exceeds the RSA limit. This should not happen with a 32-byte key and 2048+ bit RSA.
  • Verify the GCM tag after AES decryption before using the data.

セキュリティ上の制限

  • Hybrid encryption is the standard for TLS, PGP, and S/MIME. It provides public-key security with symmetric-key performance.
  • All encryption is local; no keys or data are transmitted.

関連ツール

参考链接