问题描述
我使用 Famedly Matrix SDK 创建了一个 Flutter Chat 应用程序。在这里,我想集成端到端加密,但我无法使其正常工作。我创建了一个用于测试的最小应用程序。下面是源代码。发送未加密的消息按预期工作。我也可以创建加密房间并向其发送消息,但它们在 rom 中都显示为未加密。
一般来说,加密使用的是我用官方 JS SDK 和 React 编写的 JavaScript 版本。
也许你们中的某些人会注意到这里缺少什么。
pubspec.yml
name: chat_app
description: A new Flutter project.
publish_to: "none" # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
Flutter:
sdk: Flutter
Flutter_olm: ^1.1.2
Flutter_openssl_crypto: ^0.0.1
cupertino_icons: ^1.0.2
matrix_api_lite: ^0.3.3
matrix: ^0.1.5
moor: 4.3.2
database: ^0.3.3
dev_dependencies:
Flutter_test:
sdk: Flutter
Flutter:
uses-material-design: true
main.dart
import 'package:Flutter/material.dart';
import 'package:matrix/encryption.dart';
import 'package:matrix/matrix.dart';
Client? client;
void main() {
client = Client("HappyChat",enableE2eeRecovery: true,verificationMethods: {keyverificationMethod.numbers},supportedLoginTypes: {AuthenticationTypes.password});
if (client == null) {
throw ('The client is not initialized');
}
var uri = Uri.parse("http://192.168.178.20:8008");
client!.homeserver = uri;
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',theme: ThemeData(
primarySwatch: Colors.blue,),home: MyHomePage(title: 'Flutter Demo Home Page'),);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key,required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
String _title = 'My Title';
final roomId = '!RezBsiFvjPJAMnpYpc:cloudbucket1.matrix.host';
Room? _room;
_MyHomePageState() {
this._title = "CONNECTING";
}
Future<void> loginClient() async {
try {
await client!.login(
identifier: AuthenticationUserIdentifier(user: 'ppulwey'),password: 'MyPassword');
print("Client supports encryption ${client!.encryptionEnabled}"); //* This is always false!!!
this._room = client!.getRoomById(roomId);
if (this._room == null) {
throw ('Room not found');
}
print("Room is ${this._room!.encrypted ? "already" : "not"} encrypted");
if (!this._room!.encrypted) {
this._room!.enableEncryption();
}
final joinedRooms = await client!.getJoinedRooms();
if (!joinedRooms.contains(this.roomId)) {
this._room!.join();
}
} catch (e) {
print(e);
}
}
Future<void> sendMessage() async {
try {
// * Also tried this
// var eventContent = Map<String,dynamic>();
// eventContent['body'] = 'Hallo du Äffchen';
//eventContent['msgtype'] = MessageTypes.Text;
//_room!.sendEvent(eventContent,type: 'm.room.message');
_room!.sendTextEvent('Encrypted Now');
} catch (e) {
setState(() {
_title = 'FAIL';
});
print(e);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_title),body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
Text(
'You have pushed the button this many times:',Text(
'$_counter',style: Theme.of(context).textTheme.headline4,ElevatedButton(
onpressed: loginClient,child: const Text('Initialize'))
],floatingActionButton: FloatingActionButton(
onpressed: sendMessage,tooltip: 'Increment',child: Icon(Icons.add),);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)