flutter - 使用 dart 的非对称 zip 文件加密
问题描述
我有一个将数据逐行写入文件的应用程序。完成后,需要压缩数据并将其传输到服务器。我想确保在压缩之后但在将数据传输到服务器之前,数据是安全的。因此,我想加密 zip(或内容)。此外,我想使用非对称加密,因为源代码会被其他人查看。
有没有办法在颤振/飞镖中做到这一点?
我的替代解决方案是将数据读回应用程序,加密,再次写入,然后压缩。你觉得呢?你有没有什么想法?
解决方案
正如@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 :)
*/
加密文件的解决方案是使用带有随机密钥的安全对称加密算法,然后使用非对称算法对其进行加密。这种方法通常被称为混合密码系统。
推荐阅读
- java - 通过一个 JdbcTemplate 方法执行任何 sql?
- python - 比较Python中的数据框日期列值?
- python - 当带有 multiprossesing 的 python tkinter 打开 4 个新的 gui 时该怎么办
- c++ - 功能组的文档注释
- mule - 用于基于 REST GET API 的 id 进行重复数据删除的 Mulesoft 连接器
- r - 读取csv文件时如何指定文本列?
- c# - 是否有可能配置 Visual Studio 格式以忽略变量分配周围的空格
- c# - Azure Durable Function 错误处理:有没有办法确定您正在进行的重试?
- firebase - 使用 Firebase 和 React Native 进行短信验证
- php - 如何在多个站点地图中正确显示链接