[英]How to store Keypairs in flutter?
I am working on a Flutter messaging application with Firebase as database.我正在使用 Firebase 作为数据库开发 Flutter 消息传递应用程序。 In order to encrypt messages sent between 2 users, I have used rsa-encrypt package to generate Public and Private Keypairs at the start of the application.
为了加密 2 个用户之间发送的消息,我在应用程序开始时使用rsa-encrypt package 生成公钥和私钥对。
Code used to generated Keypairs using EncryptionData class:用于使用 EncryptionData class 生成密钥对的代码:
import 'package:rsa_encrypt/rsa_encrypt.dart';
import 'package:pointycastle/api.dart' as crypto;
Future<crypto.AsymmetricKeyPair> futureKeyPair;
crypto.AsymmetricKeyPair keyPair;
class EncryptFunctions{
Future<crypto.AsymmetricKeyPair<crypto.PublicKey,crypto.PrivateKey>> getKeyPair(){
var helper = RsaKeyHelper();
return helper.computeRSAKeyPair(helper.getSecureRandom());
}
}
Main.dart is something like this: Main.dart 是这样的:
void main(){
EncryptFunctions encryptFunctions = new EncryptFunctions();
futureKeyPair = encryptFunctions.getKeyPair();
runApp(MyApp());
}
In order to encrypt or decrypt strings, we use encrypt() and decrypt() methods from the same package. The futureKeyPair contains both Public and Private keys which can be accessed by using为了加密或解密字符串,我们使用来自同一个 package 的 encrypt() 和 decrypt() 方法。futureKeyPair 包含公钥和私钥,可以通过使用
keyPair = await futureKeyPair();
How do I store the private key securely on the device in order to accomplish End To End Encryption.如何将私钥安全地存储在设备上以完成端到端加密。 This is my first question here on Stackoverflow.
这是我在 Stackoverflow 上的第一个问题。 Sorry for any formatting mistakes.
对于任何格式错误,我们深表歉意。
I am working on a Flutter messaging application with Firebase as database.我正在使用 Firebase 作为数据库的 Flutter 消息传递应用程序。 In order to encrypt messages sent between 2 users, I have used rsa-encrypt package to generate Public and Private Keypairs at the start of the application.
为了加密两个用户之间发送的消息,我使用rsa-encrypt package 在应用程序开始时生成公钥和私钥对。
Code used to generated Keypairs using EncryptionData class:用于使用 EncryptionData class 生成密钥对的代码:
import 'package:rsa_encrypt/rsa_encrypt.dart';
import 'package:pointycastle/api.dart' as crypto;
Future<crypto.AsymmetricKeyPair> futureKeyPair;
crypto.AsymmetricKeyPair keyPair;
class EncryptFunctions{
Future<crypto.AsymmetricKeyPair<crypto.PublicKey,crypto.PrivateKey>> getKeyPair(){
var helper = RsaKeyHelper();
return helper.computeRSAKeyPair(helper.getSecureRandom());
}
}
Main.dart is something like this: Main.dart 是这样的:
void main(){
EncryptFunctions encryptFunctions = new EncryptFunctions();
futureKeyPair = encryptFunctions.getKeyPair();
runApp(MyApp());
}
In order to encrypt or decrypt strings, we use encrypt() and decrypt() methods from the same package.为了加密或解密字符串,我们使用来自同一个 package 的 encrypt() 和 decrypt() 方法。 The futureKeyPair contains both Public and Private keys which can be accessed by using
futureKeyPair 包含可以通过使用访问的公钥和私钥
keyPair = await futureKeyPair();
How do I store the private key securely on the device in order to accomplish End To End Encryption.如何将私钥安全地存储在设备上以完成端到端加密。 This is my first question here on Stackoverflow.
这是我在 Stackoverflow 上的第一个问题。 Sorry for any formatting mistakes.
很抱歉任何格式错误。
Android has KeyChain and KeyStore api: Android 有 KeyChain 和 KeyStore api:
https://developer.android.com/reference/android/security/KeyChain https://developer.android.com/reference/android/security/KeyChain
https://developer.android.com/training/articles/keystore.html https://developer.android.com/training/articles/keystore.html
iOS has Keychain: iOS 有钥匙串:
https://developer.apple.com/documentation/security/keychain_services https://developer.apple.com/documentation/security/keychain_services
I don't know any flutter package that abstracts these APIs, as of December 2020.截至 2020 年 12 月,我不知道有任何 flutter package 抽象了这些 API。
Unless you want to write your own package, the closest thing you might want to use could be flutter_secure_storage package which stores the key pair in Keystore for android and in Keychain for iOS, then encrypt the data using the key before storing in SharedPreference (on Android).除非您想编写自己的 package,否则您可能想要使用的最接近的东西可能是flutter_secure_storage package,它将密钥对存储在 android 的密钥库中和 iOS 的钥匙串中,然后在存储到 SharedPreference 之前使用密钥加密数据(在 Android 上) ). Just check out the code for details.
只需查看代码即可了解详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.