Skip to content

Instantly share code, notes, and snippets.

@omarzer0
Created October 27, 2022 16:39
Show Gist options
  • Save omarzer0/c8bfaccb293dcbde13f0f1527c40edf0 to your computer and use it in GitHub Desktop.
Save omarzer0/c8bfaccb293dcbde13f0f1527c40edf0 to your computer and use it in GitHub Desktop.
Encrypt And Decrypt Task of flutter sessions at Roqay
import 'dart:convert';
import 'dart:io';
void main() {
stdout.write("text: ");
String text = stdin.readLineSync(encoding: utf8) ?? "";
stdout.write("how many shifts: ");
int shiftNumber = int.tryParse(stdin.readLineSync(encoding: utf8) ?? "")!;
stdout.write("operation is (1 for encrypt, 0 to decrypt): ");
bool operation = int.tryParse(stdin.readLineSync(encoding: utf8) ?? "")! == 1;
encryptAndDecrypt(text, shiftNumber, operation);
}
void encryptAndDecrypt(String text, int shiftNumber, bool isEncrypt) {
/// Session Solution
if (isEncrypt) {
print(String.fromCharCodes(text.codeUnits.map((e) => (e + shiftNumber))));
} else {
print(String.fromCharCodes(text.codeUnits.map((e) => (e - shiftNumber))));
}
/// My Solution
// var cypheredText = text.split('').map((element) {
// int x = element.codeUnitAt(0);
// if (isEncrypt) {
// return x + shiftNumber;
// } else {
// return x - shiftNumber;
// }
// }).map((e) => String.fromCharCode(e));
// print(cypheredText.join());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment