|
@@ -26,6 +26,9 @@ import 'package:network_info_plus/network_info_plus.dart';
|
26
|
26
|
import 'package:package_info_plus/package_info_plus.dart';
|
27
|
27
|
import 'package:path_provider/path_provider.dart';
|
28
|
28
|
import 'package:permission_handler/permission_handler.dart';
|
|
29
|
+import 'package:pointycastle/asymmetric/pkcs1.dart';
|
|
30
|
+import 'package:pointycastle/asymmetric/rsa.dart';
|
|
31
|
+import 'package:pointycastle/pointycastle.dart';
|
29
|
32
|
|
30
|
33
|
import 'global.dart';
|
31
|
34
|
|
|
@@ -532,6 +535,94 @@ String aesDecrypt(String str) {
|
532
|
535
|
return _encrypter.decrypt(encrptyed, iv: _iv);
|
533
|
536
|
}
|
534
|
537
|
|
|
538
|
+///合并字节数组
|
|
539
|
+Uint8List concatenateBytes(List<Uint8List> byteList) {
|
|
540
|
+ int totalLength = byteList.fold(0, (sum, bytes) => sum + bytes.length);
|
|
541
|
+ Uint8List result = Uint8List(totalLength);
|
|
542
|
+ int offset = 0;
|
|
543
|
+
|
|
544
|
+ for (var bytes in byteList) {
|
|
545
|
+ result.setRange(offset, offset + bytes.length, bytes);
|
|
546
|
+ offset += bytes.length;
|
|
547
|
+ }
|
|
548
|
+
|
|
549
|
+ return result;
|
|
550
|
+}
|
|
551
|
+
|
|
552
|
+///RSA加密,返回BASE64字符串,仅支持PKCS1编码
|
|
553
|
+///
|
|
554
|
+/// [keyBitLength] 密钥长度,应为512,1024,2048,4096中的一个,默认512
|
|
555
|
+String rsaEncrypt(String pubKey, String content, {int keyBitLength = 512}) {
|
|
556
|
+ try {
|
|
557
|
+ final cipher = PKCS1Encoding(RSAEngine())
|
|
558
|
+ ..init(
|
|
559
|
+ true,
|
|
560
|
+ PublicKeyParameter<RSAPublicKey>(
|
|
561
|
+ encrpyt.RSAKeyParser().parse(pubKey) as RSAPublicKey));
|
|
562
|
+
|
|
563
|
+ final utf8Bytes = Uint8List.fromList(utf8.encode(content));
|
|
564
|
+ final inputLen = utf8Bytes.length;
|
|
565
|
+ int offLen = 0;
|
|
566
|
+ int i = 0;
|
|
567
|
+ List<Uint8List> bops = [];
|
|
568
|
+
|
|
569
|
+ int size = (keyBitLength / 8 - 11).toInt();
|
|
570
|
+ while (inputLen - offLen > 0) {
|
|
571
|
+ final chunkSize = (inputLen - offLen > size) ? size : inputLen - offLen;
|
|
572
|
+ final cache = cipher.process(
|
|
573
|
+ Uint8List.sublistView(utf8Bytes, offLen, offLen + chunkSize));
|
|
574
|
+ bops.add(cache);
|
|
575
|
+ i++;
|
|
576
|
+ offLen = size * i;
|
|
577
|
+ }
|
|
578
|
+
|
|
579
|
+ final encryptedData = concatenateBytes(bops);
|
|
580
|
+ final base64Encoded = base64Encode(encryptedData);
|
|
581
|
+
|
|
582
|
+ return base64Encoded;
|
|
583
|
+ } catch (e) {
|
|
584
|
+ loge("RSA加密异常", error: e);
|
|
585
|
+ return "";
|
|
586
|
+ }
|
|
587
|
+}
|
|
588
|
+
|
|
589
|
+///RSA解密,输入BASE64,仅支持PKCS1编码
|
|
590
|
+///
|
|
591
|
+/// [keyBitLength] 密钥长度,应为512,1024,2048,4096中的一个,默认512
|
|
592
|
+String rsaDecrypt(String priKey, String content, {int keyBitLength = 512}) {
|
|
593
|
+ try {
|
|
594
|
+ final cipher = PKCS1Encoding(RSAEngine());
|
|
595
|
+ cipher.init(
|
|
596
|
+ false,
|
|
597
|
+ PrivateKeyParameter<RSAPrivateKey>(
|
|
598
|
+ encrpyt.RSAKeyParser().parse(priKey) as RSAPrivateKey));
|
|
599
|
+
|
|
600
|
+ final encryptedBytes = base64Decode(content);
|
|
601
|
+ final inputLen = encryptedBytes.length;
|
|
602
|
+ int offLen = 0;
|
|
603
|
+ int i = 0;
|
|
604
|
+
|
|
605
|
+ List<Uint8List> byteList = [];
|
|
606
|
+
|
|
607
|
+ int size = keyBitLength ~/ 8;
|
|
608
|
+ while (inputLen - offLen > 0) {
|
|
609
|
+ final int chunkSize =
|
|
610
|
+ (inputLen - offLen > size) ? size : inputLen - offLen;
|
|
611
|
+ final cache = cipher.process(
|
|
612
|
+ Uint8List.sublistView(encryptedBytes, offLen, offLen + chunkSize));
|
|
613
|
+ byteList.add(cache);
|
|
614
|
+ i++;
|
|
615
|
+ offLen = size * i;
|
|
616
|
+ }
|
|
617
|
+
|
|
618
|
+ final byteArray = concatenateBytes(byteList);
|
|
619
|
+ return utf8.decode(byteArray);
|
|
620
|
+ } catch (e) {
|
|
621
|
+ loge("RSA解密异常", error: e);
|
|
622
|
+ return '';
|
|
623
|
+ }
|
|
624
|
+}
|
|
625
|
+
|
535
|
626
|
///检查新版本
|
536
|
627
|
///
|
537
|
628
|
/// 返回true表示有新版本,false没有新版本
|