微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在flutter中查找或创建private.pem和public.pem

如何解决如何在flutter中查找或创建private.pem和public.pem

我想在拍打中使用rsa 我有下面的代码扑打

但是我不知道 test / private.pem test / public.pem 它是如何颤动的

当然,我有用Java制成的私钥和公用密钥,长度为1024

我可以把它们放在这里吗?否,是否必须创建PEM文件?如何生成PEM文件

感谢您的帮助

Future<void> main () async {

  final publicKey = await parseKeyFromFile<RSAPublicKey>('test/public.pem');
  final privKey = await parseKeyFromFile<RSAPrivateKey>('test/private.pem');

  final plainText = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit';
  final encrypter = Encrypter(RSA(publicKey: publicKey,privateKey: privKey));

  final encrypted = encrypter.encrypt(plainText);
  final decrypted = encrypter.decrypt(encrypted);

  print(decrypted); // Lorem ipsum dolor sit amet,consectetur adipiscing elit
  print(encrypted.base64); // kO9EbgbrSwiq0EYz0aBdljHSC/rci2854Qa+nugbhKjidlezNplsEqOxR+pr1RtICZGAtv0YGevJBaraHS17eHuj7GXo1CM3PR6pjGxrorcwR5Q7/bVEePESsimMbhHWF+AkDIX4v0CwKx9lgaTBgC8/yJKiLmQkyDCj64J3JSE=
}

解决方法

你可以使用 pointy castle 包来做到这一点:https://pub.dev/packages/pointycastle 只需生成一个密钥对(下面的代码)并在您的应用程序中使用它。祝你好运!

import 'package:pointycastle/export.dart';
import 'dart:math';
import 'dart:typed_data';


AsymmetricKeyPair<RSAPublicKey,RSAPrivateKey> generateRSAkeyPair(
    SecureRandom secureRandom,{int bitLength = 2048}) {
  // Create an RSA key generator and initialize it

  final keyGen = RSAKeyGenerator()
    ..init(ParametersWithRandom(
        RSAKeyGeneratorParameters(BigInt.parse('65537'),bitLength,64),secureRandom));

  // Use the generator

  final pair = keyGen.generateKeyPair();

  // Cast the generated key pair into the RSA key types

  final myPublic = pair.publicKey as RSAPublicKey;
  final myPrivate = pair.privateKey as RSAPrivateKey;

  return AsymmetricKeyPair<RSAPublicKey,RSAPrivateKey>(myPublic,myPrivate);
}

SecureRandom exampleSecureRandom() {
  final secureRandom = FortunaRandom();

  final seedSource = Random.secure();
  final seeds = <int>[];
  for (int i = 0; i < 32; i++) {
    seeds.add(seedSource.nextInt(255));
  }
  secureRandom.seed(KeyParameter(Uint8List.fromList(seeds)));

  return secureRandom;
}

// here is how you generate key pair
final pair = generateRSAkeyPair(exampleSecureRandom());

final public = pair.publicKey; // to get public
final private = pair.privateKey; // i know,you get it :D

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。