首页 > 解决方案 > 使用 dart 的非对称 zip 文件加密

问题描述

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

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

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

标签: flutterdartencryptionpassword-encryption

解决方案


正如@Topaco 准确指出的那样,大文件的非对称加密具有重要的性能缺陷。

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

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

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 :)
*/

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


推荐阅读