cipher.dart 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import 'dart:async';
  2. import 'dart:convert';
  3. import 'dart:io';
  4. import 'dart:math';
  5. import 'package:cookie_jar/cookie_jar.dart';
  6. import 'package:crypto/crypto.dart';
  7. import 'package:encrypt/encrypt.dart' as encrypt;
  8. import 'package:encrypt/encrypt_io.dart';
  9. import 'package:flutter_secure_storage/flutter_secure_storage.dart';
  10. import 'package:path_provider/path_provider.dart';
  11. import 'package:pointycastle/asymmetric/api.dart';
  12. import 'package:pointycastle/export.dart';
  13. import 'package:pointycastle/src/platform_check/platform_check.dart';
  14. import 'api.dart';
  15. import 'global.dart';
  16. class RSAPublicKeyStore extends RSAPublicKey {
  17. RSAPublicKeyStore(BigInt modulus, BigInt exponent) : super(modulus, exponent);
  18. Map<String, dynamic> toJson() => {"modulus": modulus.toString(), "exponent": exponent.toString()};
  19. factory RSAPublicKeyStore.fromJson(Map<String, dynamic> map) {
  20. return RSAPublicKeyStore(BigInt.parse(map["modulus"]), BigInt.parse(map["exponent"]));
  21. }
  22. Future<void> save(String key) async {
  23. try {
  24. final storage = FlutterSecureStorage();
  25. storage.write(key: key, value: jsonEncode(toJson()));
  26. } catch (e) {
  27. debug('save $key error: $e');
  28. }
  29. }
  30. static Future<RSAPublicKeyStore?> load(String key) async {
  31. RSAPublicKeyStore? result;
  32. try {
  33. final storage = FlutterSecureStorage();
  34. final json = await storage.read(key: key);
  35. result = RSAPublicKeyStore.fromJson(jsonDecode(json!));
  36. } catch (e) {
  37. debug('load $key error: $e');
  38. }
  39. return result;
  40. }
  41. }
  42. class RSAPrivateKeyStore extends RSAPrivateKey {
  43. RSAPrivateKeyStore(BigInt modulus, BigInt privateExponent, BigInt p, BigInt q)
  44. : super(modulus, privateExponent, p, q);
  45. Map<String, dynamic> toJson() => {
  46. "modulus": modulus.toString(),
  47. "privateExponent": privateExponent.toString(),
  48. "p": p.toString(),
  49. "q": q.toString(),
  50. };
  51. factory RSAPrivateKeyStore.fromJson(Map<String, dynamic> map) {
  52. return RSAPrivateKeyStore(
  53. BigInt.parse(map["modulus"]),
  54. BigInt.parse(map["privateExponent"]),
  55. BigInt.parse(map["p"]),
  56. BigInt.parse(map["q"]),
  57. );
  58. }
  59. Future<void> save(String key) async {
  60. try {
  61. final storage = FlutterSecureStorage();
  62. storage.write(key: key, value: jsonEncode(toJson()));
  63. } catch (e) {
  64. debug('save $key error: $e');
  65. }
  66. }
  67. static Future<RSAPrivateKeyStore?> load(String key) async {
  68. RSAPrivateKeyStore? result;
  69. try {
  70. final storage = FlutterSecureStorage();
  71. final json = await storage.read(key: key);
  72. result = RSAPrivateKeyStore.fromJson(jsonDecode(json!));
  73. } catch (e) {
  74. debug('load $key error: $e');
  75. }
  76. return result;
  77. }
  78. }
  79. AsymmetricKeyPair<RSAPublicKeyStore, RSAPrivateKeyStore> generateRSAKeyPair({SecureRandom? secureRandom, int bitLength = 2048}) {
  80. // Create an RSA key generator and initialize it
  81. secureRandom ??= getSecureRandom();
  82. final keyGen = RSAKeyGenerator()
  83. ..init(ParametersWithRandom(RSAKeyGeneratorParameters(BigInt.parse('65537'), bitLength, 64), secureRandom));
  84. // Use the generator
  85. final pair = keyGen.generateKeyPair();
  86. // Cast the generated key pair into the RSA key types
  87. final myPublic = pair.publicKey as RSAPublicKeyStore;
  88. final myPrivate = pair.privateKey as RSAPrivateKeyStore;
  89. return AsymmetricKeyPair<RSAPublicKeyStore, RSAPrivateKeyStore>(myPublic, myPrivate);
  90. }
  91. SecureRandom getSecureRandom() {
  92. final secureRandom = SecureRandom('Fortuna')..seed(KeyParameter(encrypt.SecureRandom(32).bytes));
  93. return secureRandom;
  94. }
  95. Future<AsymmetricKeyPair<RSAPublicKeyStore, RSAPrivateKeyStore>> getRSAKeyPair(String username) async {
  96. final _privateKey = 'e2ee chat private key of $username';
  97. final _publicKey = 'e2ee chat public key of $username';
  98. try {
  99. final privateKey = await RSAPrivateKeyStore.load(_privateKey);
  100. final publicKey = await RSAPublicKeyStore.load(_publicKey);
  101. if (privateKey != null && publicKey != null) {
  102. return AsymmetricKeyPair<RSAPublicKeyStore, RSAPrivateKeyStore>(publicKey, privateKey);
  103. }
  104. } catch (e) {
  105. debug('get rsa Public key failed: $e');
  106. }
  107. final pair = generateRSAKeyPair();
  108. pair.publicKey.save(_publicKey);
  109. pair.privateKey.save(_privateKey);
  110. await Api().addPublicKey(jsonEncode(pair.publicKey.toJson()));
  111. return pair;
  112. }