r/dailyprogrammer • u/oskar_s • May 19 '12
[5/19/2012] Challenge #54 [easy]
A transposition cipher we'll call the "matrix cipher" can be defined as follows: write each character in the text that you want to encrypt in a matrix of some specified width, where the width is the key of the cipher. So, for instance, if you wanted to encrypt "The cake is a lie!" with the key 3, you would write it like so (the spaces are replaced with underscores for clarity):
T h e
_ c a
k e _
i s _
a _ l
i e !Then to get the ciphertext, you simply read off the columns one by one. So in this case, the ciphertext would be "T_kiaihces_eea__l!", or "T kiaihces eea l!" if you put the spaces back in.
If the text doesn't fit the matrix perfectly, you add in random letters to fill in the last row. So, if we wanted to encode "The cake is a lie!" with key 7, we'd construct this matrix:
T h e _ c a k
e _ i s _ a _
l i e ! v m zHere "v", "m" and "z" have been added in to fill the last row, and the ciphertext is "Telh ieie s!c vaamk z".
Write an implementation of the matrix cipher that can both encode and decode text given the correct key.
BONUS: One of the major tricks code-crackers have used throughout history is the fact that the first parts of many messages often follow a regular pattern. They start with "Hello" or "Greetings", "Transmission from" or something like that (Allied codebreakers during World War II took advantage of the fact that Nazi messages often began with "Heil Hitler").
Use this trick to construct a way to automatically crack messages encrypted with the matrix cipher. That is, given a certain ciphertext to crack and the first few characters of the cleartext, figure out what the entire message is without human input. Your code should just return the correct answer and optionally the key, but nothing else.
Try your code-cracker on this text, using the clue that the message starts with "It seems" (or "It_seems", if you use the underscore):
I_rso_wotTe,taef_h__hl__socaeihtemonraaheamd_svemsp_l_ems_ayiN___Anofeadt.yueo_o
h_..__leaA_.iaastnY.snw__do__d_nyeuhl_foor_eiaotushlvrr.'oapee.avnv_d__he,ey_gOf
___oiunrbpaunieeer_r_l_geos_ctoingoloyfq_rcam__ilainpotlimadufhjv_llt_emiw_aevsd
nrsdriengieysr_p_tc_,tlfteuc_uitwrrawavzo_irhlez_ftrelszloyyry_bir__e_huv_no_ead
eauuyvsbs_mtoe_l.rb_urat_eeh_y_pOsreg_fjnp,rocucee___otn_cpgbmujltaayprgiayr_uep
fb_btt,velyahe_s,eogeraq__ue__ncysr.hcdzoo__ar_duftTcioi'tahkmnarwxeeeegeae_r__jAs you can see, there's plenty of punctuation in this text, but there are no new-lines, it is just one chunk of text. And again, all spaces have been replaced with underscores for clarity, but you should remove those to make the cleartext readable. If you do solve it, please put four spaces before the cleartext if you post it here, to hide it for people who want to solve it themselves.
- Thanks to SwimmingPastaDevil for suggesting this problem at /r/dailyprogrammer_ideas! If you have a problem that you think would be a good challenge, why not head over there and help us out!
4
u/PenguinKenny May 19 '12 edited May 19 '12
Looks like quite a fun challenge. Thank you for the work you and the other mods do here.
EDIT: Bah, I'm stuck :( Am I allowed to post here to ask for help?
8
u/gavin19 May 19 '12
Thanks for this. These challenges are great for hobbyists like me to prevent from falling into the same tasks over and over.
Cheers!
4
u/emcoffey3 0 0 May 19 '12
C#
Key:
Decoded message:
1
u/emcoffey3 0 0 May 19 '12
In your first example, you specify a width of 4 but the matrix is 3 cells wide, but, in your second example, you specify a width of 7 and the matrix is 7 cells wide. Is that a mistake, or am I totally missing something?
1
u/Ozera 0 0 May 21 '12 edited May 22 '12
This is in C:
2
u/Ozera 0 0 May 20 '12
O this is an awesome challenge :D More cipher challenges!
Working on problem now.
2
u/SwimmingPastaDevil 0 0 May 20 '12
Python:
Bonus:
Obligatory: Woohoo my problem got featured.
1
u/bh3 May 19 '12
Python:
1
u/ilmmad May 20 '12 edited May 20 '12
SML
1
u/robotfarts May 20 '12 edited May 20 '12
Only encode:
or
1
u/[deleted] May 20 '12
J
Usage
Decoding the string
1
u/Scroph 0 0 May 20 '12 edited May 20 '12
I made one in PHP, probably not the best language to make something like this in. I haven't tested it thoroughly, but here it is nevertheless :
Cracked text :
1
u/Yuushi May 21 '12
Python:
And the bonus decoded with auto_decode:
1
u/school_throwaway May 21 '12
python, just used a's for the extra characters
1
u/moomesh May 21 '12
Heres my effort, C++ I have to say this took MUCH longer than I expected. I had it sussed in my head straight away, but getting it to work took ages. It works, the decoded message is:
Code:
1
u/ixid 0 0 May 21 '12 edited May 21 '12
A version in D:
And the bonus is:
1
u/leftplusright May 22 '12 edited May 25 '12
Python:
1
u/Medicalizawhat May 24 '12 edited May 24 '12
I did the encoder in Objective-c:
Added decoder:
1
u/eine_person Jul 06 '12 edited Jul 06 '12
If there is still anyone around here, willing to help me with this task:
Here is my solution in Ruby:
So far, so good, the decrypted message is the same as any other in this thread, the only remaining problem is the key-numer
Which must be incorrect, since even the number of padding-characters is higher. Any ideas?
Edit: Mystery solved. I defined the matrix in my encryption-function with switched values (width and length). Dividing the number of characters of the task-message by my "key" gives you the right answer.
0
u/[deleted] May 19 '12
[deleted] — view deleted comment