Skip to content

Commit

Permalink
[fea] 实现了融合门户的数据加密功能, 优化import
Browse files Browse the repository at this point in the history
  • Loading branch information
steven12138 committed May 7, 2024
1 parent 8ef8317 commit bb64402
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/auth/view/login/reset_done_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:we_pei_yang_flutter/commons/themes/template/wpy_theme_data.dart'
import 'package:we_pei_yang_flutter/commons/util/router_manager.dart';
import 'package:we_pei_yang_flutter/commons/util/text_util.dart';
import 'package:we_pei_yang_flutter/commons/widgets/w_button.dart';

import '../../../commons/themes/wpy_theme.dart';

class ResetDoneWidget extends StatelessWidget {
Expand Down
113 changes: 113 additions & 0 deletions lib/commons/network/cas_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:convert/convert.dart';
import 'package:pointycastle/export.dart';
import 'package:we_pei_yang_flutter/commons/network/wpy_dio.dart';
import 'package:xml/xml.dart';

class DesService {
static Uint8List padPKCS7(Uint8List src, int blockSize) {
final padLength = blockSize - (src.length % blockSize);
final padded = Uint8List(src.length + padLength)
..setRange(0, src.length, src);
padded.fillRange(src.length, padded.length, padLength);
return padded;
}

static String desEncode(String plainText, String keyString, String ivString) {
final keyBytes = utf8.encode(keyString);
final ivBytes = utf8.encode(ivString);

final key = Uint8List.fromList(keyBytes);
final iv = Uint8List.fromList(ivBytes);

final cipher = CBCBlockCipher(DESedeEngine())
..init(true, ParametersWithIV(KeyParameter(key), iv));

final paddedText =
padPKCS7(Uint8List.fromList(utf8.encode(plainText)), cipher.blockSize);
final encryptedBytes = _processBlocks(cipher, paddedText);
return hex.encode(encryptedBytes).toUpperCase();
}

static Uint8List _processBlocks(BlockCipher cipher, Uint8List input) {
final output = Uint8List(input.length);
for (int offset = 0; offset < input.length; offset += cipher.blockSize) {
cipher.processBlock(input, offset, output, offset);
}
return output;
}
}

class EncryptedPathInterceptor extends InterceptorsWrapper {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
options.headers["User-Agent"] =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3";
final source_string = "method=${options.path}"
"&${options.queryParameters.entries.map((e) => "${e.key}=${e.value}").join("&")}";

options.path = getEncryptedString(source_string);
options.queryParameters = {};
print("encrypted_path: ${options.path}");
return handler.next(options);
}

String getEncryptedString(String source) {
return DesService.desEncode(source, "neusofteducationplatform", "01234567");
}
}

class XMLSerializerInterceptor extends InterceptorsWrapper {
Map<String, dynamic> parseXml(XmlElement element) {
final map = <String, dynamic>{};
for (var child in element.children) {
if (child is XmlElement) {
if (child.children.isEmpty ||
child.children.every((c) => c is XmlText && c.value.isEmpty)) {
map[child.name.local] = '';
} else if (child.children.length == 1 &&
child.children.single is XmlText) {
map[child.name.local] = child.children.single.value;
} else {
map[child.name.local] = parseXml(child);
}
}
}
return map;
}

@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
final data = response.data;
if (data is String) {
final xml = XmlDocument.parse(data);
response.data = parseXml(xml.rootElement);
}
return handler.next(response);
}
}

class CasDio extends DioAbstract {
@override
String get baseUrl => "https://f.tju.edu.cn/tp_up/up/mobile/ifs/";

@override
List<Interceptor> get interceptors => [
EncryptedPathInterceptor(),
XMLSerializerInterceptor(),
];
}

final casDio = CasDio();

class CasService {
static Future<String> getQRContent(String sid) async {
final response = await casDio.get(
"getAccountQRcodeInfo",
queryParameters: {"ID_NUMBER": sid},
);
return response.data["message"];
}
}
1 change: 1 addition & 0 deletions lib/commons/themes/wpy_theme.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:we_pei_yang_flutter/commons/preferences/common_prefs.dart';
Expand Down
2 changes: 1 addition & 1 deletion lib/home/home_router.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:flutter/material.dart' show Widget;
import "package:flutter/src/widgets/basic.dart";
import 'view/web_views/game_page.dart';

import 'view/home_page.dart';
import 'view/lost_and_found_home_page.dart';
import 'view/map_calendar_page.dart';
import 'view/web_views/fifty_two_hz_page.dart';
import 'view/web_views/game_page.dart';
import 'view/web_views/news_page.dart';
import 'view/web_views/notices_page.dart';
import 'view/web_views/wiki_page.dart';
Expand Down
2 changes: 2 additions & 0 deletions lib/home/view/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:provider/provider.dart';
import 'package:we_pei_yang_flutter/auth/network/auth_service.dart';
import 'package:we_pei_yang_flutter/commons/channel/push/push_manager.dart';
import 'package:we_pei_yang_flutter/commons/channel/statistics/umeng_statistics.dart';
import 'package:we_pei_yang_flutter/commons/network/cas_service.dart';
import 'package:we_pei_yang_flutter/commons/preferences/common_prefs.dart';
import 'package:we_pei_yang_flutter/commons/themes/template/wpy_theme_data.dart';
import 'package:we_pei_yang_flutter/commons/util/toast_provider.dart';
Expand Down Expand Up @@ -238,6 +239,7 @@ class _HomePageState extends State<HomePage> with TickerProviderStateMixin {
canPop: false,
onPopInvoked: (didPop) {
if (didPop) return;

CommonPreferences.lastActivityDialogShownDate.value = "";
if (_tabController.index == 0) {
if (_lastPressedAt == null ||
Expand Down
5 changes: 4 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ dependencies:
sdk: flutter

async: ^2.8.2
# amap_location_fluttify: ^0.22.0
# amap_location_fluttify: ^0.22.0

pointycastle: ^3.1.1
xml: ^6.5.0

badges: ^3.1.1

Expand Down

0 comments on commit bb64402

Please sign in to comment.