简体   繁体   English

如何在 flutter 中存储密钥对?

[英]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.

相关问题 HTTPS连接-如何管理密钥对 - HTTPS connections - How are the keypairs managed 如何使用Flutter将整数存储到Firestore - How to store integers to Firestore with Flutter 如何在Flutter App中本地存储多个变量 - How to store multiple variables locally in Flutter App 如何在 flutter 中存储 API 机密 - How to store API-secrets in flutter Flutter - FCM:如何将后台消息存储到 SQLite - Flutter - FCM: how to store background message to SQLite 如何在 flutter 中为 Play 商店构建一个已知的 apk? - How to build a known apk for play store in flutter? 如何在 Flutter 中存储来自 DatePicker 的数据 - How to store the data from the DatePicker in Flutter 如何正确检索和存储数据以缓存抖动 - How to properly retrieve and store data to cache flutter 如何使用dio在flutter中存储对对象的响应 - how to store response to object in flutter using dio 如何存储列表<object>在 Flutter 中使用 sharedPreferences?<div id="text_translate"><p> 我想在 file_picker 库的帮助下保存 PlatformFile 列表并使用 sharedpreferences 存储它们,然后将它们加载回来。 列表 -&gt; 列表</p></div></object> - How to store List<Object> in Flutter using sharedPreferences?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM