-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathCaesarCipher.dart
110 lines (87 loc) · 2.58 KB
/
CaesarCipher.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Caeser cipher is a type of substitution cipher, i.e., each letter of
a given text is replaced by a letter some fixed number[key] of positions down the alphabet
*/
import 'dart:io';
/* Encrypts the message by replacing individual
letters by a letter 'key' places down the alphabet */
String encrypt(String plain, int key) {
int length = plain.length;
List<int> encrypt = [];
//Iterate through whole string and replace letters
for(int index = 0; index < length; index++){
int char = plain[index].codeUnitAt(0);
if(plain[index].codeUnitAt(0) <= 91){
encrypt.add(((char-65) + key) % 26 + 65);
}
else if(plain[index].codeUnitAt(0) >= 97){
encrypt.add(((char-97) + key) % 26 + 65);
}
}
String encrypted_string = String.fromCharCodes(encrypt);
return encrypted_string;
}
/* Decrypts the message by replacing individual
letters by a letter 'key' places up the alphabet */
String decrypt(String plain, int key) {
int length = plain.length;
List<int> decrypt = [];
//Iterate through whole string and replace letters
for(int index = 0; index < length; index++){
int char = plain[index].codeUnitAt(0);
if(plain[index].codeUnitAt(0) <= 91){
decrypt.add(((char-65) - key) % 26 + 65);
}
else if(plain[index].codeUnitAt(0) >= 97){
decrypt.add(((char-97) - key) % 26 + 65);
}
}
String decrypted_string = String.fromCharCodes(decrypt);
return decrypted_string;
}
void main() {
print("Enter 1 to ENCRYPT and 2 to DECRYPT : ");
int choice = int.parse(stdin.readLineSync()!);
if(choice == 1){
print("Enter message to ENCRYPT : ");
String toEncrypt = stdin.readLineSync()!;
print("Enter key value:");
int key = int.parse(stdin.readLineSync()!);
// call encrypt function
String encrypted = encrypt(toEncrypt, key);
print("\nEncrypted message is $encrypted");
}
else if(choice == 2){
print("Enter message to DECRYPT : ");
String toDecrypt = stdin.readLineSync()!;
print("Enter key value:");
int key = int.parse(stdin.readLineSync()!);
// call decrypt function
String decrypted = decrypt(toDecrypt, key);
print("\nDecrypted message is $decrypted");
}
else{
print("Please enter either 1 or 2");
}
}
/*
Time Complexity: O(n)
Space Complexity: O(n)
Sample I/O:
1)
Enter 1 to ENCRYPT and 2 to DECRYPT :
1
Enter message to ENCRYPT :
NEOALGO
Enter key value:
9
Encrypted message is WNXJUPX
2)
Enter 1 to ENCRYPT and 2 to DECRYPT :
2
Enter message to DECRYPT :
WNXJUPX
Enter key value:
9
Decrypted message is NEOALGO
*/