Skip to content

Commit 2da6934

Browse files
keiravillekodekotp
authored andcommitted
Add affine-cipher exercise
1 parent f4e4b2d commit 2da6934

File tree

11 files changed

+1194
-1
lines changed

11 files changed

+1194
-1
lines changed

config.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"prerequisites": [],
5151
"difficulty": 2,
5252
"status": "deprecated"
53-
},
53+
},
5454
{
5555
"slug": "trinary",
5656
"name": "Trinary",
@@ -412,6 +412,14 @@
412412
"prerequisites": [],
413413
"difficulty": 4
414414
},
415+
{
416+
"slug": "affine-cipher",
417+
"name": "Affine Cipher",
418+
"uuid": "e0127be0-4664-44d0-9fbf-6fd9d9d944e0",
419+
"practices": [],
420+
"prerequisites": [],
421+
"difficulty": 5
422+
},
415423
{
416424
"slug": "all-your-base",
417425
"name": "All Your Base",
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Instructions
2+
3+
Create an implementation of the affine cipher, an ancient encryption system created in the Middle East.
4+
5+
The affine cipher is a type of monoalphabetic substitution cipher.
6+
Each character is mapped to its numeric equivalent, encrypted with a mathematical function and then converted to the letter relating to its new numeric value.
7+
Although all monoalphabetic ciphers are weak, the affine cipher is much stronger than the Atbash cipher, because it has many more keys.
8+
9+
[//]: # " monoalphabetic as spelled by Merriam-Webster, compare to polyalphabetic "
10+
11+
## Encryption
12+
13+
The encryption function is:
14+
15+
```text
16+
E(x) = (ai + b) mod m
17+
```
18+
19+
Where:
20+
21+
- `i` is the letter's index from `0` to the length of the alphabet - 1.
22+
- `m` is the length of the alphabet.
23+
For the Latin alphabet `m` is `26`.
24+
- `a` and `b` are integers which make up the encryption key.
25+
26+
Values `a` and `m` must be _coprime_ (or, _relatively prime_) for automatic decryption to succeed, i.e., they have number `1` as their only common factor (more information can be found in the [Wikipedia article about coprime integers][coprime-integers]).
27+
In case `a` is not coprime to `m`, your program should indicate that this is an error.
28+
Otherwise it should encrypt or decrypt with the provided key.
29+
30+
For the purpose of this exercise, digits are valid input but they are not encrypted.
31+
Spaces and punctuation characters are excluded.
32+
Ciphertext is written out in groups of fixed length separated by space, the traditional group size being `5` letters.
33+
This is to make it harder to guess encrypted text based on word boundaries.
34+
35+
## Decryption
36+
37+
The decryption function is:
38+
39+
```text
40+
D(y) = (a^-1)(y - b) mod m
41+
```
42+
43+
Where:
44+
45+
- `y` is the numeric value of an encrypted letter, i.e., `y = E(x)`
46+
- it is important to note that `a^-1` is the modular multiplicative inverse (MMI) of `a mod m`
47+
- the modular multiplicative inverse only exists if `a` and `m` are coprime.
48+
49+
The MMI of `a` is `x` such that the remainder after dividing `ax` by `m` is `1`:
50+
51+
```text
52+
ax mod m = 1
53+
```
54+
55+
More information regarding how to find a Modular Multiplicative Inverse and what it means can be found in the [related Wikipedia article][mmi].
56+
57+
## General Examples
58+
59+
- Encrypting `"test"` gives `"ybty"` with the key `a = 5`, `b = 7`
60+
- Decrypting `"ybty"` gives `"test"` with the key `a = 5`, `b = 7`
61+
- Decrypting `"ybty"` gives `"lqul"` with the wrong key `a = 11`, `b = 7`
62+
- Decrypting `"kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx"` gives `"thequickbrownfoxjumpsoverthelazydog"` with the key `a = 19`, `b = 13`
63+
- Encrypting `"test"` with the key `a = 18`, `b = 13` is an error because `18` and `26` are not coprime
64+
65+
## Example of finding a Modular Multiplicative Inverse (MMI)
66+
67+
Finding MMI for `a = 15`:
68+
69+
- `(15 * x) mod 26 = 1`
70+
- `(15 * 7) mod 26 = 1`, ie. `105 mod 26 = 1`
71+
- `7` is the MMI of `15 mod 26`
72+
73+
[mmi]: https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
74+
[coprime-integers]: https://en.wikipedia.org/wiki/Coprime_integers
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"AffineCipher.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
14+
]
15+
},
16+
"blurb": "Create an implementation of the Affine cipher, an ancient encryption algorithm from the Middle East.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Affine_cipher"
19+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
unit AffineCipher;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
function encode(const phrase : string; const a, b : longword) : string;
8+
function decode(const phrase : string; const a, b : longword) : string;
9+
10+
implementation
11+
12+
uses SysUtils;
13+
14+
function inverse(const a : longword) : longword;
15+
begin
16+
result := 0;
17+
if a = 1 then result := 1;
18+
if a = 3 then result := 9;
19+
if a = 5 then result := 21;
20+
if a = 7 then result := 15;
21+
if a = 9 then result := 3;
22+
if a = 11 then result := 19;
23+
if a = 15 then result := 7;
24+
if a = 17 then result := 23;
25+
if a = 19 then result := 11;
26+
if a = 21 then result := 5;
27+
if a = 23 then result := 17;
28+
if a = 25 then result := 25;
29+
if result = 0 then
30+
raise Exception.Create('a and m must be coprime.');
31+
end;
32+
33+
function process(const phrase : string; const a, b : longword; group : integer) : string;
34+
var
35+
c : char;
36+
encoded : char;
37+
remaining : integer;
38+
begin
39+
remaining := group;
40+
result := '';
41+
for c in phrase do
42+
begin
43+
if (c >= '0') and (c <= '9') then
44+
encoded := c
45+
else if (c >= 'A') and (c <= 'Z') then
46+
encoded := chr(((ord(c) - ord('A')) * a + b) mod 26 + ord('a'))
47+
else if (c >= 'a') and (c <= 'z') then
48+
encoded := chr(((ord(c) - ord('a')) * a + b) mod 26 + ord('a'))
49+
else
50+
continue;
51+
52+
if remaining = 0 then
53+
begin
54+
result := result + ' ';
55+
remaining := group;
56+
end;
57+
58+
result := result + encoded;
59+
remaining := remaining - 1;
60+
end;
61+
end;
62+
63+
function encode(const phrase : string; const a, b : longword) : string;
64+
begin
65+
inverse(a);
66+
result := process(phrase, a, b, 5);
67+
end;
68+
69+
function decode(const phrase : string; const a, b : longword) : string;
70+
var
71+
inv : longword;
72+
begin
73+
inv := inverse(a);
74+
result := process(phrase, inv, (26 - inv) * b, -1);
75+
end;
76+
77+
end.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[2ee1d9af-1c43-416c-b41b-cefd7d4d2b2a]
13+
description = "encode -> encode yes"
14+
15+
[785bade9-e98b-4d4f-a5b0-087ba3d7de4b]
16+
description = "encode -> encode no"
17+
18+
[2854851c-48fb-40d8-9bf6-8f192ed25054]
19+
description = "encode -> encode OMG"
20+
21+
[bc0c1244-b544-49dd-9777-13a770be1bad]
22+
description = "encode -> encode O M G"
23+
24+
[381a1a20-b74a-46ce-9277-3778625c9e27]
25+
description = "encode -> encode mindblowingly"
26+
27+
[6686f4e2-753b-47d4-9715-876fdc59029d]
28+
description = "encode -> encode numbers"
29+
30+
[ae23d5bd-30a8-44b6-afbe-23c8c0c7faa3]
31+
description = "encode -> encode deep thought"
32+
33+
[c93a8a4d-426c-42ef-9610-76ded6f7ef57]
34+
description = "encode -> encode all the letters"
35+
36+
[0673638a-4375-40bd-871c-fb6a2c28effb]
37+
description = "encode -> encode with a not coprime to m"
38+
39+
[3f0ac7e2-ec0e-4a79-949e-95e414953438]
40+
description = "decode -> decode exercism"
41+
42+
[241ee64d-5a47-4092-a5d7-7939d259e077]
43+
description = "decode -> decode a sentence"
44+
45+
[33fb16a1-765a-496f-907f-12e644837f5e]
46+
description = "decode -> decode numbers"
47+
48+
[20bc9dce-c5ec-4db6-a3f1-845c776bcbf7]
49+
description = "decode -> decode all the letters"
50+
51+
[623e78c0-922d-49c5-8702-227a3e8eaf81]
52+
description = "decode -> decode with no spaces in input"
53+
54+
[58fd5c2a-1fd9-4563-a80a-71cff200f26f]
55+
description = "decode -> decode with too many spaces"
56+
57+
[b004626f-c186-4af9-a3f4-58f74cdb86d5]
58+
description = "decode -> decode with a not coprime to m"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
unit AffineCipher;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
function encode(const phrase : string; const a, b : longword) : string;
8+
function decode(const phrase : string; const a, b : longword) : string;
9+
10+
implementation
11+
12+
uses SysUtils;
13+
14+
function encode(const phrase : string; const a, b : longword) : string;
15+
begin
16+
17+
raise ENotImplemented.Create('Please implement your solution.'); result := Copy(phrase, a, b);
18+
19+
end;
20+
21+
function decode(const phrase : string; const a, b : longword) : string;
22+
begin
23+
24+
raise ENotImplemented.Create('Please implement your solution.'); result := Copy(phrase, a, b);
25+
26+
end;
27+
28+
end.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SHELL = /bin/bash
2+
MAKEFLAGS += --no-print-directory
3+
DESTDIR = build
4+
EXECUTABLE = $(DESTDIR)/test
5+
COMMAND = fpc -l- -v0 -g -gl -Sa -Cr -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"
6+
7+
.ONESHELL:
8+
9+
test:
10+
@mkdir -p "./$(DESTDIR)"
11+
@cp -r ./lib "./$(DESTDIR)"
12+
@$(COMMAND) && ./$(EXECUTABLE) $(test)
13+
14+
clean:
15+
@rm -fr "./$(DESTDIR)"

0 commit comments

Comments
 (0)