Add a fictional cipher from "supergirl"

This commit is contained in:
zontreck 2024-07-27 02:27:08 -07:00
parent 1525640d19
commit c789219481
6 changed files with 105 additions and 2 deletions

61
lib/utils/DBIKC.dart Normal file
View file

@ -0,0 +1,61 @@
class DoubleBreastedInterruptedKeyCipher {
static String encode(String text) {
// Define the vowel and consonant lists
List<String> vowels = ['A', 'E', 'I', 'O', 'U'];
List<String> consonants = [
'B',
'C',
'D',
'F',
'G',
'H',
'J',
'K',
'L',
'M',
'N',
'P',
'Q',
'R',
'S',
'T',
'V',
'W',
'X',
'Y',
'Z'
];
// Create a map for easy lookups
Map<String, String> vowelMap = {};
Map<String, String> consonantMap = {};
// Fill the maps by mirroring the lists
for (int i = 0; i < vowels.length; i++) {
vowelMap[vowels[i]] = vowels[vowels.length - 1 - i];
}
for (int i = 0; i < consonants.length; i++) {
consonantMap[consonants[i]] = consonants[consonants.length - 1 - i];
}
// Encode the text
StringBuffer encoded = StringBuffer();
for (int i = 0; i < text.length; i++) {
String char = text[i].toUpperCase();
if (vowelMap.containsKey(char)) {
encoded.write(vowelMap[char]);
} else if (consonantMap.containsKey(char)) {
encoded.write(consonantMap[char]);
} else {
encoded.write(char); // Keep non-alphabet characters unchanged
}
}
return encoded.toString();
}
static String decode(String text) {
// The decoding process is the same as encoding for this cipher
return encode(text);
}
}