Skip to content

Instantly share code, notes, and snippets.

@oyeniyi66
Created February 6, 2025 09:04
Show Gist options
  • Save oyeniyi66/d075f861afbfdc856c3870afee8e05d1 to your computer and use it in GitHub Desktop.
Save oyeniyi66/d075f861afbfdc856c3870afee8e05d1 to your computer and use it in GitHub Desktop.

Flutter Encryption & Decryption App

πŸ“Œ Description

This is a simple Flutter app that encrypts and decrypts text using a circular shift algorithm based on a given numeric key.

πŸ”Ή How It Works

  • If the key is positive, letters shift forward (A β†’ B, Z β†’ A).
  • If the key is negative, letters shift backward (A β†’ Z, C β†’ A).
  • Special characters & numbers remain unchanged.

πŸš€ How to Run

  1. Click on the Appetize.io link to open the app:
    πŸ‘‰ Run the App

  2. Follow these steps:

    • Enter text in the input field.
    • Provide a numeric key (positive or negative).
    • Click "Encrypt" to shift letters based on the key.
    • Click "Decrypt" to revert to the original text.

Enjoy using the app!

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),
),
],
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment