Skip to content

Instantly share code, notes, and snippets.

@Detective-Khalifah
Last active February 7, 2025 12:21
Show Gist options
  • Save Detective-Khalifah/91c1c45fe206716e2dde975aa186be3d to your computer and use it in GitHub Desktop.
Save Detective-Khalifah/91c1c45fe206716e2dde975aa186be3d to your computer and use it in GitHub Desktop.
A simple Encryption and Decryption program in Dart (and Flutter)

string_encryption_app

A simple string Encryption and Decryption using the Caesar Cipher algorithm.

How to Run

  1. Download the files.
  2. Run dart encryption_service.dart to execute the Dart program on console.
  3. Alternatively, run flutter pub get to install dependencies and flutter run to launch the Flutter app.

Example

  • Input: "Hello", Key: "key123"
  • Encrypted: "Hkrru"
  • Decrypted: "Hello"
class EncryptionService {
/// A static method that encrypts plain text using some passed key.
/// Call `EncryptionService.encryptText(plainText, key)` to execute,
/// ensuring to pass the `plainText` that needs encrypting, and a `key`.
static String encryptText(String plainText, String key) {
// Algorithm: Shift each character by the length of the key
int shift = key.length;
String encryptedText = "";
for (int i = 0; i < plainText.length; i++) {
encryptedText += String.fromCharCode(plainText.codeUnitAt(i) + shift);
}
return encryptedText;
}
/// A static method that decrypts encoded text using a passed key.
/// Call `EncryptionService.decryptText(plainText, key)` to execute,
/// ensuring to pass the `plainText` that needs encrypting, and the `key`.
static String decryptText(String encryptedText, String key) {
// Reverse the encryption process
int shift = key.length;
String plainText = "";
for (int i = 0; i < encryptedText.length; i++) {
plainText += String.fromCharCode(encryptedText.codeUnitAt(i) - shift);
}
return plainText;
}
}
// Example usage
void main() {
String plaintext = "Hello";
String key = "key123";
String encrypted = EncryptionService.encryptText(plaintext, key);
print("Encrypted: $encrypted"); // Output: Encrypted: Khoor
String decrypted = EncryptionService.decryptText(encrypted, key);
print("Decrypted: $decrypted"); // Output: Decrypted: Hello
}
import 'package:flutter/material.dart';
import 'encryption_service.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.lightBlue),
useMaterial3: true,
),
home: const MyHomePage(title: 'String (d)e(n)cryption'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController _inputController = TextEditingController();
final TextEditingController _keyController = TextEditingController();
String _result = '';
void _encrypt() {
String input = _inputController.text;
String key = _keyController.text;
setState(() {
_result = EncryptionService.encryptText(input, key);
});
// _inputController.clear();
}
void _decrypt() {
String input = _inputController.text;
String key = _keyController.text;
setState(() {
_result = EncryptionService.decryptText(input, key);
});
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
spacing: 16,
children: <Widget>[
TextField(
controller: _inputController,
decoration: InputDecoration(
labelText: 'Input String',
border: OutlineInputBorder(),
),
),
TextField(
controller: _keyController,
decoration: InputDecoration(
labelText: 'Secret Key',
border: OutlineInputBorder(),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: _encrypt,
child: Text('Encrypt'),
),
ElevatedButton(
onPressed: _decrypt,
child: Text('Decrypt'),
),
],
),
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
spacing: 8,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Result:',
style:
TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
Tooltip(
message: 'Double Tap or long press to copy result',
child: SelectableText(
_result,
style: TextStyle(fontSize: 16),
toolbarOptions: ToolbarOptions(copy: true),
),
),
],
),
Text(
'(Double tap/Long press to copy)',
style: TextStyle(fontSize: 12, color: Colors.grey),
),
],
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment