如何用RSA加密大文件?

时间:2016-10-25 15:24:42

标签: encryption go rsa public-key-encryption encryption-asymmetric

代码https://play.golang.org/p/CUEqjsJq5c

错误:

panic: crypto/rsa: message too long for RSA public key size

goroutine 1 [running]:
panic(0x4a6d80, 0xc420010420)
    /usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
    /tmp/sample.go:28 +0xfa

文件大小811字节(用于测试加密自我源文件)。我想加密一些更大的文件,1..500 mb。我可以使用RSA或需要使用其他方法吗?

2 个答案:

答案 0 :(得分:4)

RSA只能加密小于密钥长度的数据。

答案是使用AES等对称算法加密数据,该算法旨在加密小型和大型数据。

如果需要RSA公钥/私钥对,则使用RSA加密对称(AES)密钥。这被称为混合加密,实质上是锄头HTTPS加密数据。

但除非需要公钥/私钥对,否则RSA可能无需加密对称密钥。在一般情况下,只使用对称(AES)和密钥。公钥/私钥对的用例是什么?

答案 1 :(得分:3)

如果您不想对文件进行分块,一种方法是空中创建随机对称密钥R,使用随机对称密钥EF=Sym(F, R)加密大文件并发送加密的文件EF,带有对称密钥,并由非对称RSA公钥Pub(R)加密。

加密:

+---------------------+      +--------------------+
|                     |      |                    |
| generate random key |      |   the large file   |
|        (R)          |      |        (F)         |
|                     |      |                    |
+--------+--------+---+      +----------+---------+
         |        |                     |
         |        +------------------+  |
         |                           |  |
         v                           v  v
+--------+------------+     +--------+--+------------+
|                     |     |                        |
| encrypt (R) with    |     | encrypt (F)            |
| your RSA public key |     | with symmetric key (R) |
|                     |     |                        |
|  ASym(PublicKey, R) |     |     EF = Sym(F, R)     |
|                     |     |                        |
+----------+----------+     +------------+-----------+
           |                             |
           +------------+ +--------------+
                        | |
                        v v
         +--------------+-+---------------+
         |                                |
         |   send this files to the peer  |
         |                                |
         |     ASym(PublicKey, R) + EF    |
         |                                |
         +--------------------------------+

解密:

   +----------------+        +--------------------+
   |                |        |                    |
   | EF = Sym(F, R) |        | ASym(PublicKey, R) |
   |                |        |                    |
   +-----+----------+        +---------+----------+
         |                             |
         |                             |
         |                             v
         |   +-------------------------+-----------------+
         |   |                                           |
         |   |             restore key (R)               |
         |   |                                           |
         |   | R <= ASym(PrivateKey, ASym(PublicKey, R)) |
         |   |                                           |
         |   +---------------------+---------------------+
         |                         |
         v                         v
     +---+-------------------------+---+
     |                                 |
     |       restore the file (F)      |
     |                                 |
     |      F <= Sym(Sym(F, R), R)     |
     |                                 |
     +---------------------------------+