r/dailyprogrammer • u/jnazario 2 0 • May 16 '18
[2018-05-16] Challenge #361 [Intermediate] ElsieFour low-tech cipher
Description
ElsieFour (LC4) is a low-tech authenticated encryption algorithm that can be computed by hand. Rather than operating on octets, the cipher operates on this 36-character alphabet:
#_23456789abcdefghijklmnopqrstuvwxyzEach of these characters is assigned an integer 0–35. The cipher uses a 6x6 tile substitution-box (s-box) where each tile is one of these characters. A key is any random permutation of the alphabet arranged in this 6x6 s-box. Additionally a marker is initially placed on the tile in the upper-left corner. The s-box is permuted and the marked moves during encryption and decryption.
See the illustrations from the paper (album).
Each tile has a positive "vector" derived from its value: (N % 6, N / 6), referring to horizontal and vertical movement respectively. All vector movement wraps around, modulo-style.
To encrypt a single character, locate its tile in the s-box, then starting from that tile, move along the vector of the tile under the marker. This will be the ciphertext character (the output).
Next, the s-box is permuted. Right-rotate the row containing the plaintext character. Then down-rotate the column containing the ciphertext character. If the tile on which the marker is sitting gets rotated, marker goes with it.
Finally, move the marker according to the vector on the ciphertext tile.
Repeat this process for each character in the message.
Decryption is the same, but it (obviously) starts from the ciphertext character, and the plaintext is computed by moving along the negated vector (left and up) of the tile under the marker. Rotation and marker movement remains the same (right-rotate on plaintext tile, down-rotate on ciphertext tile).
If that doesn't make sense, have a look at the paper itself. It has pseudo-code and a detailed step-by-step example.
Input Description
Your program will be fed two lines. The first line is the encryption key. The second line is a message to be decrypted.
Output Description
Print the decrypted message.
Sample Inputs
s2ferw_nx346ty5odiupq#lmz8ajhgcvk79b
tk5j23tq94_gw9c#lhzs
#o2zqijbkcw8hudm94g5fnprxla7t6_yse3v
b66rfjmlpmfh9vtzu53nwf5e7ixjnpSample Outputs
aaaaaaaaaaaaaaaaaaaa
be_sure_to_drink_your_ovaltineChallenge Input
9mlpg_to2yxuzh4387dsajknf56bi#ecwrqv
grrhkajlmd3c6xkw65m3dnwl65n9op6k_o59qeqBonus
Also add support for encryption. If the second line begins with % (not in the cipher alphabet), then it should be encrypted instead.
7dju4s_in6vkecxorlzftgq358mhy29pw#ba
%the_swallow_flies_at_midnight
hemmykrc2gx_i3p9vwwitl2kvljizIf you want to get really fancy, also add support for nonces and signature authentication as discussed in the paper. The interface for these is up to you.
Credit
This challenge was suggested by user /u/skeeto, many thanks! If you have any challenge ideas, please share them in /r/dailyprogrammer_ideas and there's a good chance we'll use them.
9
u/zatoichi49 May 16 '18 edited May 16 '18
Method:
Convert the key string into a 6x6 array, and create a dictionary of the vectors for each tile. For each character in the input text, find the row/col position and subtract (for decryption) or add (for encryption) the vectors of the marker to find the row/col position of the decrypted/encrypted character. Add the character to the results string. Rotate the relevant row and column, and move the marker to its new position. Repeat for the remaining characters, and return the final results string. (EDIT: Added comments)
Python 3 (with Bonus):
Output:
6
u/skeeto -9 8 May 16 '18
C, but rather than paste code I'll link to my repository:
https://github.com/skeeto/elsiefour/blob/master/lc4.h
The cipher requires searching for the position of the input. I used a reverse lookup table instead, for two reasons:
2
u/[deleted] May 17 '18 edited May 17 '18
[C++11] with bonus, all cases passed. The function "vectoring" helps finding the encoded/decoded character regardless of the direction of the operation: it turns out you can convert a negated vector by just finding the complementary values of the marker with respect to 6, that's what negatedMarker is for. So for example, subtracting a marker vector with values (1, 2) is equal to adding a vector of (6 - 1, 6 - 2) = (5, 4), after the coordinates get moduloed the result is the same.
Edit: a bit of refactoring here and there. I like refactoring.
2
u/Gprime5 May 17 '18 edited May 17 '18
Python 3
With encryption bonus.
2
u/ruincreep May 17 '18
Perl 6
2
u/Philboyd_Studge 0 1 May 19 '18
Java. Late to the party, but this was fun. Took a while to figure out I needed
Math.floorModfor the reverse movement. Encrypts and Decrypts.1
u/technobach May 16 '18
Python 3, with encrypt and decrypt
1
u/thestoicattack May 16 '18 edited May 16 '18
C++17 with bonus. Sorry it's damned long.
Points of interest:
constexpr ifon theDecrypttemplate parameter means we can share almost all the code between encryption and decryption. Picking out a column to rotate was handled with the local lambdaelemwhich include fancydecltypereturn type deduction so that we can use its result as an lvalue reference. Delegating constructor for theGridPointertype.Also it wasn't working for a long time, and it took a while to find the bug: in
gen, when we callshiftRow, we also have to make sure to shift the ciphertext letter's value if necessary.1
u/BambaiyyaLadki May 17 '18
Haskell solution (no bonus):
https://repl.it/repls/RecentPoshCores
1
u/popillol May 17 '18
Go / Golang with bonus.
Output
1
u/DerpinDementia May 17 '18 edited May 17 '18
Python 3 (decrypt only)
My first daily programmer submission. Took me too long to figure out but hey, at least decrypt works so I can get encrypt working eventually. All feedback welcome!
Challenge
1
u/Gavin_Song May 17 '18 edited May 17 '18
Accidentally posted my solution in the other subreddit post
Before anyone asks, I made the npm package (https://github.com/Gavin-Song/elsie-four-cipher) Relevant code snippets are below:
https://github.com/Gavin-Song/elsie-four-cipher/blob/master/src/index.js
https://github.com/Gavin-Song/elsie-four-cipher/blob/master/src/key.js
Config:
All the code condensed into 1 file
1
u/mr_stivo May 19 '18
perl
1
u/Scara95 May 21 '18
Pike with bonus
A bit late
i/o:
1
u/[deleted] May 22 '18
F# with bonus
I need to work on my reading comprehension because implementing the decryption took forever because I misunderstood the rules. You're supposed to downshift on the ciphered text and rightshift on the plain text regardless of decryption/encryption.
1
u/octolanceae May 24 '18
C++17
A bit late to the party here. Had trouble with this one, mostly because I didn't read the paper closely. Decrypt worked fine, but encrypt did not. As they say, "Reading is Fundamental" and "The devil is in the details".
Outputs
1
u/samtaylor7 May 25 '18
Ruby
With encryption bonus. A bit late but this was a lot of fun. Gist
1
u/thatsokayy May 28 '18 edited May 28 '18
Python3 with bonus
1
u/eternalblue227 May 28 '18 edited May 28 '18
Java No Bonus
Output
Feedback would be appreciated.
1
u/[deleted] Jun 06 '18 edited Jun 06 '18
Anyone got any advice?
Here is my attempts output https://pastebin.com/DDxnNyH8
@index 4 the ciphered message should be tk5j2, not tk5ju
Looks like a logic error, but I am not sure how else I should be doing this cipher. Any advice? Here is my code
https://github.com/willkillson/Challenge-361-Intermediate-Java
1
u/KeenWolfPaw Jun 20 '18
My first intermediate challenge completed :D.
C
Running instructions here
0
u/[deleted] May 16 '18
[deleted] — view deleted comment
0
u/[deleted] May 16 '18
[deleted] — view deleted comment