RosettaCodeData/Task/Caesar-cipher/Elena/caesar-cipher.elena

76 lines
1.5 KiB
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
import system'routines;
import system'math;
import extensions;
import extensions'text;
const string Letters = "abcdefghijklmnopqrstuvwxyz";
const string BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string TestText = "Pack my box with five dozen liquor jugs.";
const int Key = 12;
class Encrypting : Enumerator
{
2023-09-01 09:35:06 -07:00
int _key;
Enumerator _enumerator;
2023-07-01 11:58:00 -04:00
constructor(int key, string text)
{
2023-09-01 09:35:06 -07:00
_key := key;
_enumerator := text.enumerator();
2023-07-01 11:58:00 -04:00
}
2023-09-01 09:35:06 -07:00
bool next() => _enumerator;
2023-07-01 11:58:00 -04:00
2023-09-01 09:35:06 -07:00
reset() => _enumerator;
2023-07-01 11:58:00 -04:00
2023-09-01 09:35:06 -07:00
enumerable() => _enumerator;
2023-07-01 11:58:00 -04:00
2023-09-01 09:35:06 -07:00
get Value()
2023-07-01 11:58:00 -04:00
{
2023-09-01 09:35:06 -07:00
var ch := *_enumerator;
2023-07-01 11:58:00 -04:00
var index := Letters.indexOf(0, ch);
if (-1 < index)
{
2024-03-06 22:25:12 -08:00
^ Letters[(_key+index).mod(26)]
2023-07-01 11:58:00 -04:00
}
else
{
index := BigLetters.indexOf(0, ch);
if (-1 < index)
{
2024-03-06 22:25:12 -08:00
^ BigLetters[(_key+index).mod(26)]
2023-07-01 11:58:00 -04:00
}
else
{
^ ch
}
}
}
}
extension encryptOp
{
encrypt(key)
= new Encrypting(key, self).summarize(new StringWriter());
decrypt(key)
= new Encrypting(26 - key, self).summarize(new StringWriter());
}
2026-02-01 16:33:20 -08:00
public Program()
2023-07-01 11:58:00 -04:00
{
2025-08-11 18:05:26 -07:00
Console.printLine("Original text :",TestText);
2023-07-01 11:58:00 -04:00
2024-03-06 22:25:12 -08:00
var encryptedText := TestText.encrypt(Key);
2023-07-01 11:58:00 -04:00
2025-08-11 18:05:26 -07:00
Console.printLine("Encrypted text:",encryptedText);
2023-07-01 11:58:00 -04:00
2024-03-06 22:25:12 -08:00
var decryptedText := encryptedText.decrypt(Key);
2023-07-01 11:58:00 -04:00
2025-08-11 18:05:26 -07:00
Console.printLine("Decrypted text:",decryptedText);
2023-07-01 11:58:00 -04:00
2025-08-11 18:05:26 -07:00
Console.readChar()
2023-07-01 11:58:00 -04:00
}