encrypt_util.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import 'dart:convert';
  2. import 'package:crypto/crypto.dart';
  3. import 'package:convert/convert.dart';
  4. /// Encrypt Util.
  5. class EncryptUtil {
  6. /// md5 加密
  7. static String encodeMd5(String data) {
  8. var content = Utf8Encoder().convert(data);
  9. var digest = md5.convert(content);
  10. return hex.encode(digest.bytes);
  11. }
  12. /// 异或对称加密
  13. static String xorCode(String res, String key) {
  14. List<String> keyList = key.split(',');
  15. List<int> codeUnits = res.codeUnits;
  16. List<int> codes = [];
  17. for (int i = 0, length = codeUnits.length; i < length; i++) {
  18. int code = codeUnits[i] ^ int.parse(keyList[i % keyList.length]);
  19. codes.add(code);
  20. }
  21. return String.fromCharCodes(codes);
  22. }
  23. /// 异或对称 Base64 加密
  24. static String xorBase64Encode(String res, String key) {
  25. String encode = xorCode(res, key);
  26. encode = encodeBase64(encode);
  27. return encode;
  28. }
  29. /// 异或对称 Base64 解密
  30. static String xorBase64Decode(String res, String key) {
  31. String encode = decodeBase64(res);
  32. encode = xorCode(encode, key);
  33. return encode;
  34. }
  35. /// Base64加密
  36. static String encodeBase64(String data) {
  37. var content = utf8.encode(data);
  38. var digest = base64Encode(content);
  39. return digest;
  40. }
  41. /// Base64解密
  42. static String decodeBase64(String data) {
  43. List<int> bytes = base64Decode(data);
  44. String result = utf8.decode(bytes);
  45. return result;
  46. }
  47. }