【问题标题】:Asymmetric zip file encryption with dart使用 dart 的非对称 zip 文件加密
【发布时间】:2020-12-18 11:30:37
【问题描述】:

我有一个应用程序将数据逐行写入文件。完成后,需要压缩数据并将其传输到服务器。我想确保压缩之后但将数据传输到服务器之前,数据是安全的。因此,我想加密 zip(或内容)。另外,我想用非对称加密,因为源代码会被别人看到。

在颤振/飞镖中有没有办法做到这一点?

我的替代解决方案是将数据读回应用程序,加密,再次写入,然后压缩。你有什么想法?

【问题讨论】:

  • 使用 非对称 加密,例如 RSA,只能加密有限长度的数据。最大长度对应于密钥长度,即对于 2048 位密钥,最大长度为 256 字节。对于较长的数据,必须使用 对称 加密(例如 AES),如果要使用非对称方法(例如 AES)加密对称密钥,则必须使用组合(混合加密)。将其发送给其他方。在加密过程中,处理二进制数据并返回二进制数据。因此,压缩原则上可以根据要求在加密之前或之后进行。
  • 所以按照我的理解,不可能用非对称加密来加密整个文件?您对在将数据发送到服务器之前保持数据安全的建议是什么?
  • 关于Q1:如果数据量太大(最迟如果大于密钥大小)是不可能的,关于Q2:使用对称加密(例如AES)或混合加密进行加密(如上)。也许 HTTPS 是你的一个选择,here

标签: flutter dart encryption password-encryption


【解决方案1】:

正如@Topaco 准确指出的那样,大文件的非对称加密会带来严重的性能缺陷。

可以通过将文件分成更小的数据块并加密每个部分来实现。但同样,不建议这样做。

也就是说,您使用 Flutter 的 rsa_encrypt 包加密/解密 StringRSA

import 'package:rsa_encrypt/rsa_encrypt.dart';
import 'package:pointycastle/api.dart' as crypto;

//Future to hold our KeyPair
Future<crypto.AsymmetricKeyPair> futureKeyPair;

//to store the KeyPair once we get data from our future
crypto.AsymmetricKeyPair keyPair;

Future<crypto.AsymmetricKeyPair<crypto.PublicKey, crypto.PrivateKey>> getKeyPair()
{
var helper = RsaKeyHelper();
return helper.computeRSAKeyPair(helper.getSecureRandom());
}

/*
- Generate KeyPair with the function getKeyPair() store the returned value in futureKeyPair.

- Once we get data from the future we can store that data in keyPair (Now we have acces to our private and public key).

- In order to view our keys as "a string" we need to use two functions encodePrivateKeyToPemPKCS1(keyPair.privateKey) & encodePublicKeyToPemPKCS1(keyPair.publicKey).

- In order to encrypt and decrypt strings you can use two functions

- encrypt() : use this function to encrypt a string, pass your string as first argument and a public key as the second one. [IMPORTANT]: this will return a string so you should store the returned value in a variable.

- decrypt() : use this function to decrypt an encrypted String, pass your encrypted String as first argument and a private key as the second. this will also return a string dont forget to store it :)
*/

加密文件的一种解决方案是使用带有随机密钥的安全对称加密算法,然后使用非对称算法对其进行加密。这种方法通常被称为hybrid cryptosystem

【讨论】:

    猜你喜欢
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2012-08-08
    相关资源
    最近更新 更多