encrpyt.dart 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // ignore_for_file: constant_identifier_names
  2. import 'dart:convert';
  3. import 'package:dart_des/dart_des.dart';
  4. import 'package:dart_sm/dart_sm.dart';
  5. import 'package:encrypt/encrypt.dart';
  6. import 'package:hex/hex.dart';
  7. import 'package:pointycastle/asymmetric/api.dart';
  8. /// JSON-RPC 加密配置
  9. class JsonRpcEncryptConfig {
  10. /// 是否启用
  11. final bool enable;
  12. /// 加密方式
  13. final String encryptMode;
  14. /// 秘钥
  15. final String encryptKey;
  16. JsonRpcEncryptConfig({
  17. this.enable = false,
  18. this.encryptMode = "sm2",
  19. this.encryptKey = "",
  20. });
  21. Map<String, dynamic> toJson() {
  22. return {
  23. "enable": enable,
  24. "encryptMode": encryptMode,
  25. "encryptKey": encryptKey,
  26. };
  27. }
  28. }
  29. abstract class JsonRpcEncryptUtils {
  30. /// RSA公钥
  31. static const _RSA_PUB_KEY = "-----BEGIN PUBLIC KEY-----\n"
  32. "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAln7CnpiqYguFVRUL5oCdFAQL6ypYQdnd84XN++mRvjkdjtpbCdGEj6S2U2Vn+Bt+vHL5bvUk6gtTx5FpV0Z8kDvDgBRweIvtJTZLx6ZjbI7habxclW9aB3b0SW3zuFuHMteDivTI0lkZ6y0QRiG7nMZYsi18IjReBssXsx38fpgIBAHIbkb98wtEffBbrbwfdxQnfc8r9Hy7iIbfVP0h7nR0FDMu1vYqi5PsnYDw681iO6GNxV6VHA6wcWkMokLZn51OP+OQmomJPElWfPgLDp1AxBeRhuHFlXslcE550bzWUs3iBylTgXwdTGqppCjCXhSLBiOnTc0ggHpDQHh17wIDAQAB\n" +
  33. "-----END PUBLIC KEY-----";
  34. /// RSA加密
  35. static String rsaEncrypt(String message) {
  36. final publicKey = RSAKeyParser().parse(_RSA_PUB_KEY) as RSAPublicKey;
  37. final encrypter = Encrypter(RSA(publicKey: publicKey));
  38. // Encrypt the message using RSA-OAEP encryption
  39. final encrypted = encrypter.encrypt(message);
  40. final encryptedText = encrypted.base64;
  41. return encryptedText;
  42. }
  43. /// DES解密
  44. static String desDecrypt(String ciphertext, String key) {
  45. // IV
  46. const List<int> iv = [1, 2, 3, 4, 5, 6, 7, 8];
  47. final ivHexString = String.fromCharCodes(iv);
  48. final ivString = utf8.encode(ivHexString);
  49. final DES desCBC = DES(
  50. key: key.codeUnits,
  51. mode: DESMode.CBC,
  52. iv: ivString,
  53. paddingType: DESPaddingType.PKCS7,
  54. );
  55. final ciphertextHexDecoded = HEX.decode(ciphertext);
  56. final decryptedBytes = desCBC.decrypt(ciphertextHexDecoded);
  57. final decryptedString = utf8.decode(decryptedBytes);
  58. return decryptedString;
  59. }
  60. /// 国密2加密
  61. static String sm2Encrypt(String message, String key) {
  62. // 1 - C1C3C2; 0 - C1C2C3; 默认为1
  63. const int cipherMode = 1; // 特别注意,此处前后端需保持一致
  64. // 加密结果
  65. String encryptData = SM2.encrypt(message, key, cipherMode: cipherMode);
  66. // 加密后的密文前需要添加04,后端才能正常解密
  67. encryptData = "04$encryptData";
  68. return encryptData;
  69. }
  70. /// 国密4解密
  71. static String sm4Decrypt(String cipherText, String key) {
  72. return SM4.decrypt(cipherText, key: key);
  73. }
  74. }