|
import 'package:flutter/material.dart'; |
|
|
|
// Function to encrypt with circular shift |
|
String encrypt(String plaintext, int shift) { |
|
String encrypted = ''; |
|
|
|
for (int i = 0; i < plaintext.length; i++) { |
|
String char = plaintext[i]; |
|
|
|
if (char.contains(RegExp(r'[A-Z]'))) { |
|
int newCode = ((char.codeUnitAt(0) - 65 + shift) % 26 + 26) % 26 + 65; |
|
encrypted += String.fromCharCode(newCode); |
|
} else if (char.contains(RegExp(r'[a-z]'))) { |
|
int newCode = ((char.codeUnitAt(0) - 97 + shift) % 26 + 26) % 26 + 97; |
|
encrypted += String.fromCharCode(newCode); |
|
} else { |
|
encrypted += char; |
|
} |
|
} |
|
|
|
return encrypted; |
|
} |
|
|
|
// Function to decrypt (just reverse the shift) |
|
String decrypt(String encryptedText, int shift) { |
|
return encrypt(encryptedText, -shift); |
|
} |
|
|
|
void main() { |
|
runApp(EncryptionApp()); |
|
} |
|
|
|
class EncryptionApp extends StatefulWidget { |
|
@override |
|
_EncryptionAppState createState() => _EncryptionAppState(); |
|
} |
|
|
|
class _EncryptionAppState extends State<EncryptionApp> { |
|
String encryptedText = ''; |
|
String decryptedText = ''; |
|
|
|
final plaintextController = TextEditingController(); |
|
final keyController = TextEditingController(); |
|
|
|
void encryptText() { |
|
int shift = int.tryParse(keyController.text) ?? 0; |
|
setState(() { |
|
encryptedText = encrypt(plaintextController.text, shift); |
|
}); |
|
} |
|
|
|
void decryptText() { |
|
int shift = int.tryParse(keyController.text) ?? 0; |
|
setState(() { |
|
decryptedText = decrypt(encryptedText, shift); |
|
}); |
|
} |
|
|
|
@override |
|
Widget build(BuildContext context) { |
|
return MaterialApp( |
|
debugShowCheckedModeBanner: false, |
|
theme: ThemeData( |
|
primarySwatch: Colors.blue, |
|
scaffoldBackgroundColor: Colors.blueGrey[400], |
|
), |
|
home: Scaffold( |
|
appBar: AppBar( |
|
title: const Center(child: Text('Encryption & Decryption')), |
|
elevation: 5, |
|
), |
|
body: Padding( |
|
padding: const EdgeInsets.all(20.0), |
|
child: Column( |
|
mainAxisAlignment: MainAxisAlignment.center, |
|
crossAxisAlignment: CrossAxisAlignment.stretch, |
|
children: <Widget>[ |
|
TextField( |
|
controller: plaintextController, |
|
decoration: const InputDecoration( |
|
labelText: 'Enter Text', |
|
border: OutlineInputBorder(), |
|
filled: true, |
|
fillColor: Colors.white, |
|
), |
|
), |
|
const SizedBox(height: 10), |
|
TextField( |
|
controller: keyController, |
|
decoration: const InputDecoration( |
|
labelText: 'Enter Key (positive or negative)', |
|
border: OutlineInputBorder(), |
|
filled: true, |
|
fillColor: Colors.white, |
|
), |
|
keyboardType: TextInputType.number, |
|
), |
|
const SizedBox(height: 20), |
|
ElevatedButton( |
|
onPressed: encryptText, |
|
style: ElevatedButton.styleFrom( |
|
backgroundColor: Colors.blueAccent, |
|
padding: const EdgeInsets.symmetric(vertical: 15), |
|
shape: RoundedRectangleBorder( |
|
borderRadius: BorderRadius.circular(10), |
|
), |
|
), |
|
child: const Text('Encrypt', style: TextStyle(fontSize: 18)), |
|
), |
|
const SizedBox(height: 10), |
|
Text( |
|
'Encrypted: $encryptedText', |
|
textAlign: TextAlign.center, |
|
style: |
|
const TextStyle(fontSize: 16, fontWeight: FontWeight.bold), |
|
), |
|
const SizedBox(height: 20), |
|
ElevatedButton( |
|
onPressed: decryptText, |
|
style: ElevatedButton.styleFrom( |
|
backgroundColor: Colors.greenAccent[700], |
|
padding: const EdgeInsets.symmetric(vertical: 15), |
|
shape: RoundedRectangleBorder( |
|
borderRadius: BorderRadius.circular(10), |
|
), |
|
), |
|
child: const Text('Decrypt', style: TextStyle(fontSize: 18)), |
|
), |
|
const SizedBox(height: 10), |
|
Text( |
|
'Decrypted: $decryptedText', |
|
textAlign: TextAlign.center, |
|
style: |
|
const TextStyle(fontSize: 16, fontWeight: FontWeight.bold), |
|
), |
|
], |
|
), |
|
), |
|
), |
|
); |
|
} |
|
} |