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

76 lines
1.5 KiB
Text
Raw Permalink Normal View History

2019-09-12 10:33:56 -07:00
import system'routines;
import system'math;
import extensions;
import extensions'text;
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
const string Letters = "abcdefghijklmnopqrstuvwxyz";
const string BigLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const string TestText = "Pack my box with five dozen liquor jugs.";
const int Key = 12;
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
class Encrypting : Enumerator
2013-04-10 16:57:12 -07:00
{
2019-09-12 10:33:56 -07:00
int theKey;
Enumerator theEnumerator;
constructor(int key, string text)
{
theKey := key;
theEnumerator := text.enumerator();
}
bool next() => theEnumerator;
reset() => theEnumerator;
enumerable() => theEnumerator;
get()
{
var ch := theEnumerator.get();
var index := Letters.indexOf(0, ch);
if (-1 < index)
{
^ Letters[(theKey+index).mod:26]
}
else
{
index := BigLetters.indexOf(0, ch);
if (-1 < index)
{
^ BigLetters[(theKey+index).mod:26]
}
else
{
^ ch
}
}
}
2014-01-17 05:32:22 +00:00
}
2017-09-23 10:01:46 +02:00
extension encryptOp
2015-11-18 06:14:39 +00:00
{
2019-09-12 10:33:56 -07:00
encrypt(key)
= new Encrypting(key, self).summarize(new StringWriter());
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
decrypt(key)
= new Encrypting(26 - key, self).summarize(new StringWriter());
2015-11-18 06:14:39 +00:00
}
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
public program()
{
console.printLine("Original text :",TestText);
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
var encryptedText := TestText.encrypt:Key;
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
console.printLine("Encrypted text:",encryptedText);
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
var decryptedText := encryptedText.decrypt:Key;
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
console.printLine("Decrypted text:",decryptedText);
2013-04-10 16:57:12 -07:00
2019-09-12 10:33:56 -07:00
console.readChar()
}