diff --git a/Talk-In Webapp/lib/presentation/screens/group_chat_screen.dart b/Talk-In Webapp/lib/presentation/screens/group_chat_screen.dart index 2c47faf..27971c4 100644 --- a/Talk-In Webapp/lib/presentation/screens/group_chat_screen.dart +++ b/Talk-In Webapp/lib/presentation/screens/group_chat_screen.dart @@ -6,6 +6,7 @@ import 'package:firebase_database/ui/firebase_animated_list.dart'; import 'package:flutter/material.dart'; import 'package:flutter_chat_bubble/chat_bubble.dart'; import 'package:talk_in_web/presentation/screens/group_profile.dart'; +import 'package:talk_in_web/services/encryption_service.dart'; import 'package:url_launcher/url_launcher.dart'; import '../../services/data_service.dart'; @@ -81,7 +82,7 @@ class _GroupChatScreenState extends State { maxWidth: MediaQuery.of(context).size.width * 0.7, ), child: data["tag"]=="text"? Text( - data["Message"]!=null?data["Message"].toString():"Waiting for the message...", + data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.white), ): data["tag"] == "image"? Column( children: [ @@ -95,7 +96,7 @@ class _GroupChatScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.white)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.white)), ], ) : data["tag"] == "document"? Column( children: [ @@ -109,7 +110,7 @@ class _GroupChatScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.white)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.white)), ], ) : Column( children: [ @@ -123,7 +124,7 @@ class _GroupChatScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.white)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.white)), ], ), ), @@ -138,7 +139,7 @@ class _GroupChatScreenState extends State { maxWidth: MediaQuery.of(context).size.width * 0.7, ), child: data["tag"]=="text"? Text( - data["Message"]!=null?data["Message"].toString():"Waiting for the message...", + data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.black), ): data["tag"] == "image"? Column( children: [ @@ -152,7 +153,7 @@ class _GroupChatScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.black)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.black)), ], ) : data["tag"] == "document"? Column( children: [ @@ -166,7 +167,7 @@ class _GroupChatScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.black)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.black)), ], ) : Column( children: [ @@ -180,7 +181,7 @@ class _GroupChatScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.black)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.black)), ], ), ), @@ -249,7 +250,7 @@ class _GroupChatScreenState extends State { TextButton(onPressed: (){ Navigator.of(c).pop(); //DataService().addChatToDatabase(context, messageId, "", UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"image"); - DataService().addGroupChatToDatabase(context, "", UserService.userData!["id"].toString(), "image", DateTime.now(), groupData["id"].toString()); + DataService().addGroupChatToDatabase(context, vigenereEncrypt(" ", "KEY"), UserService.userData!["id"].toString(), "image", DateTime.now(), groupData["id"].toString()); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -258,7 +259,7 @@ class _GroupChatScreenState extends State { if(controller.text.isNotEmpty){ Navigator.of(c).pop(); //DataService().addChatToDatabase(context, messageId, controller.text, UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"image"); - DataService().addGroupChatToDatabase(context, controller.text, UserService.userData!["id"].toString(), "image", DateTime.now(), groupData["id"].toString()); + DataService().addGroupChatToDatabase(context, vigenereEncrypt(controller.text, "KEY"), UserService.userData!["id"].toString(), "image", DateTime.now(), groupData["id"].toString()); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -293,7 +294,7 @@ class _GroupChatScreenState extends State { TextButton(onPressed: (){ Navigator.of(c).pop(); //DataService().addChatToDatabase(context, messageId, "", UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"document"); - DataService().addGroupChatToDatabase(context, "", UserService.userData!["id"].toString(), "document", DateTime.now(), groupData["id"].toString()); + DataService().addGroupChatToDatabase(context, vigenereEncrypt(" ", "KEY"), UserService.userData!["id"].toString(), "document", DateTime.now(), groupData["id"].toString()); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -302,7 +303,7 @@ class _GroupChatScreenState extends State { if(controller.text.isNotEmpty){ Navigator.of(c).pop(); //DataService().addChatToDatabase(context, messageId, controller.text, UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"document"); - DataService().addGroupChatToDatabase(context, controller.text, UserService.userData!["id"].toString(), "document", DateTime.now(), groupData["id"].toString()); + DataService().addGroupChatToDatabase(context, vigenereEncrypt(controller.text, "KEY"), UserService.userData!["id"].toString(), "document", DateTime.now(), groupData["id"].toString()); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -337,7 +338,7 @@ class _GroupChatScreenState extends State { TextButton(onPressed: (){ Navigator.of(c).pop(); //DataService().addChatToDatabase(context, messageId, "", UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"audio"); - DataService().addGroupChatToDatabase(context, "", UserService.userData!["id"].toString(), "audio", DateTime.now(), groupData["id"].toString()); + DataService().addGroupChatToDatabase(context, vigenereEncrypt(" ", "KEY"), UserService.userData!["id"].toString(), "audio", DateTime.now(), groupData["id"].toString()); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -346,7 +347,7 @@ class _GroupChatScreenState extends State { if(controller.text.isNotEmpty){ Navigator.of(c).pop(); //DataService().addChatToDatabase(context, messageId, controller.text, UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"audio"); - DataService().addGroupChatToDatabase(context, controller.text, UserService.userData!["id"].toString(), "audio", DateTime.now(), groupData["id"].toString()); + DataService().addGroupChatToDatabase(context, vigenereEncrypt(controller.text, "KEY"), UserService.userData!["id"].toString(), "audio", DateTime.now(), groupData["id"].toString()); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -384,7 +385,7 @@ class _GroupChatScreenState extends State { if(messageController.text.isNotEmpty){ //DataService().addChatToDatabase(context,messageId, messageController.text, UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"text"); - DataService().addGroupChatToDatabase(context, messageController.text, UserService.userData!["id"].toString(), "text", DateTime.now(), groupData["id"].toString()); + DataService().addGroupChatToDatabase(context, vigenereEncrypt(messageController.text, "KEY"), UserService.userData!["id"].toString(), "text", DateTime.now(), groupData["id"].toString()); messageController.clear(); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); diff --git a/Talk-In Webapp/lib/presentation/screens/messaging_screen.dart b/Talk-In Webapp/lib/presentation/screens/messaging_screen.dart index 1bcc189..668bf00 100644 --- a/Talk-In Webapp/lib/presentation/screens/messaging_screen.dart +++ b/Talk-In Webapp/lib/presentation/screens/messaging_screen.dart @@ -6,6 +6,7 @@ import 'package:firebase_database/ui/firebase_animated_list.dart'; import 'package:flutter/material.dart'; import 'package:flutter_chat_bubble/chat_bubble.dart'; import 'package:talk_in_web/presentation/screens/profile_screen.dart'; +import 'package:talk_in_web/services/encryption_service.dart'; import 'package:talk_in_web/services/user_service.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -105,7 +106,7 @@ class _MessagingScreenState extends State { child: data["tag"]=="text"? Column( children: [ Text( - data["Message"]!=null?data["Message"].toString():"Waiting for the message...", + data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold), ), // Row( @@ -128,7 +129,7 @@ class _MessagingScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold)), // Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, // children: [ @@ -149,7 +150,7 @@ class _MessagingScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold)), // Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, // children: [ @@ -170,7 +171,7 @@ class _MessagingScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold)), // Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, // children: [ @@ -199,7 +200,7 @@ class _MessagingScreenState extends State { child: data["tag"]=="text"? Column( children: [ Text( - data["Message"]!=null?data["Message"].toString():"Waiting for the message...", + data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold), ), // Row( @@ -222,7 +223,7 @@ class _MessagingScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold)), // Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, // children: [ @@ -243,7 +244,7 @@ class _MessagingScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold)), // Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, // children: [ @@ -264,7 +265,7 @@ class _MessagingScreenState extends State { ) ], ), - Text(data["Message"]!=null?data["Message"].toString():"Waiting for the message...", style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold)), + Text(data["Message"]!=null?vigenereDecrypt(data["Message"].toString(), "KEY"):"Waiting for the message...", style: TextStyle(color: Colors.black,fontWeight: FontWeight.bold)), // Row( // mainAxisAlignment: MainAxisAlignment.spaceBetween, // children: [ @@ -345,7 +346,7 @@ class _MessagingScreenState extends State { actions: [ TextButton(onPressed: (){ Navigator.of(c).pop(); - DataService().addChatToDatabase(context, messageId, "", UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"image"); + DataService().addChatToDatabase(context, messageId, vigenereEncrypt(" ", "KEY"), UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"image"); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -353,7 +354,7 @@ class _MessagingScreenState extends State { TextButton(onPressed: (){ if(controller.text.isNotEmpty){ Navigator.of(c).pop(); - DataService().addChatToDatabase(context, messageId, controller.text, UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"image"); + DataService().addChatToDatabase(context, messageId, vigenereEncrypt(controller.text, "KEY"), UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"image"); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -387,7 +388,7 @@ class _MessagingScreenState extends State { actions: [ TextButton(onPressed: (){ Navigator.of(c).pop(); - DataService().addChatToDatabase(context, messageId, "", UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"document"); + DataService().addChatToDatabase(context, messageId, vigenereEncrypt(" ", "KEY"), UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"document"); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -395,7 +396,7 @@ class _MessagingScreenState extends State { TextButton(onPressed: (){ if(controller.text.isNotEmpty){ Navigator.of(c).pop(); - DataService().addChatToDatabase(context, messageId, controller.text, UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"document"); + DataService().addChatToDatabase(context, messageId, vigenereEncrypt(controller.text, "KEY"), UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"document"); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -429,7 +430,7 @@ class _MessagingScreenState extends State { actions: [ TextButton(onPressed: (){ Navigator.of(c).pop(); - DataService().addChatToDatabase(context, messageId, "", UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"audio"); + DataService().addChatToDatabase(context, messageId, vigenereEncrypt(" ", "KEY"), UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"audio"); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -437,7 +438,7 @@ class _MessagingScreenState extends State { TextButton(onPressed: (){ if(controller.text.isNotEmpty){ Navigator.of(c).pop(); - DataService().addChatToDatabase(context, messageId, controller.text, UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"audio"); + DataService().addChatToDatabase(context, messageId, vigenereEncrypt(controller.text, "KEY"), UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"audio"); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); }); @@ -474,7 +475,7 @@ class _MessagingScreenState extends State { onPressed: () async{ if(messageController.text.isNotEmpty){ - DataService().addChatToDatabase(context,messageId, messageController.text, UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"text"); + DataService().addChatToDatabase(context,messageId, vigenereEncrypt(messageController.text, "KEY"), UserService.userData!["id"].toString(), friendData["id"].toString(),DateTime.now(),"text"); messageController.clear(); Timer(Duration(milliseconds: 500), () { scrollController.jumpTo(scrollController.position.maxScrollExtent); diff --git a/Talk-In Webapp/lib/services/encryption_service.dart b/Talk-In Webapp/lib/services/encryption_service.dart new file mode 100644 index 0000000..6699f87 --- /dev/null +++ b/Talk-In Webapp/lib/services/encryption_service.dart @@ -0,0 +1,64 @@ +String vigenereEncrypt(String text, String keyword) { + String fullKeyword = _generateFullKeyword(text, keyword); + return String.fromCharCodes( + text.codeUnits.asMap().entries.map((entry) { + int idx = entry.key; + int char = entry.value; + int shift = _getShift(fullKeyword[idx] as int); + + if (char >= 65 && char <= 90) { + return 65 + (char - 65 + shift) % 26; + } else if (char >= 97 && char <= 122) { + return 97 + (char - 97 + shift) % 26; + } else { + return char; + } + }).toList() + ); +} + +String vigenereDecrypt(String text, String keyword) { + String fullKeyword = _generateFullKeyword(text, keyword); + return String.fromCharCodes( + text.codeUnits.asMap().entries.map((entry) { + int idx = entry.key; + int char = entry.value; + int shift = _getShift(fullKeyword[idx] as int); + + if (char >= 65 && char <= 90) { + return 65 + (char - 65 - shift + 26) % 26; + } else if (char >= 97 && char <= 122) { + return 97 + (char - 97 - shift + 26) % 26; + } else { + return char; + } + }).toList() + ); +} + +String _generateFullKeyword(String text, String keyword) { + int textLength = text.length; + int keywordLength = keyword.length; + StringBuffer fullKeyword = StringBuffer(); + + for (int i = 0, j = 0; i < textLength; i++) { + if (text.codeUnitAt(i) >= 65 && text.codeUnitAt(i) <= 90 || + text.codeUnitAt(i) >= 97 && text.codeUnitAt(i) <= 122) { + fullKeyword.write(keyword[j % keywordLength]); + j++; + } else { + fullKeyword.write(' '); + } + } + return fullKeyword.toString(); +} + +int _getShift(int charCode) { + if (charCode >= 65 && charCode <= 90) { + return charCode - 65; + } else if (charCode >= 97 && charCode <= 122) { + return charCode - 97; + } else { + return 0; + } +} \ No newline at end of file