r/dailyprogrammer • u/oskar_s • Aug 13 '12
[8/13/2012] Challenge #88 [easy] (Vigenère cipher)
The easy challenge today is to implement the famous Vigenère cipher. The Wikipedia article explains well how it works, but here's a short description anyway:
You take a message that you want to encrypt, for instance "THECAKEISALIE" (lets assume that all characters are upper-case and there are no spaces in the messages, for the sake of simplicity), and a key you want to encrypt it with, for instance "GLADOS". You then write the message with the key repeated over it, like this:
GLADOSGLADOSG
THECAKEISALIE
The key is repeated as often as is needed to cover the entire message.
Now, one by one, each letter of the key is "added" to the letter of the clear-text to produce the cipher-text. That is, if A = 0, B = 1, C = 2, etc, then E + G = K (because E = 4 and G = 6, and 4 + 6 = 10, and K = 10). If the sum is larger than 25 (i.e. larger than Z), it starts from the beginning, so S + K = C (i.e. 18 + 10 = 28, and 28 - 26 is equal to 2, which is C).
For a full chart of how characters combine to form new characters, see here
The cipher text then becomes:
GLADOSGLADOSG
THECAKEISALIE
-------------
ZSEFOCKTSDZAK
Write funtions to both encrypt and decrypt messages given the right key.
As an optional bonus, decrypt the following message, which has been encrypted with a word that I've used in this post:
HSULAREFOTXNMYNJOUZWYILGPRYZQVBBZABLBWHMFGWFVPMYWAVVTYISCIZRLVGOPGBRAKLUGJUZGLN
BASTUQAGAVDZIGZFFWVLZSAZRGPVXUCUZBYLRXZSAZRYIHMIMTOJBZFZDEYMFPMAGSMUGBHUVYTSABB
AISKXVUCAQABLDETIFGICRVWEWHSWECBVJMQGPRIBYYMBSAPOFRIMOLBUXFIIMAGCEOFWOXHAKUZISY
MAHUOKSWOVGBULIBPICYNBBXJXSIXRANNBTVGSNKR
As an additional challenge, attempt to pronounce "Vigenère" properly. I think it's like "Viche-en-ere", but I'm not entirely sure.
18
u/[deleted] Aug 14 '12
J time!
This is short, but uses
&.which is the most magical thing in J, so I'll explain it.6
u/5outh 1 0 Aug 13 '12 edited Aug 13 '12
Haskell (No bonus):
Edit: Major refactor (I hated what I wrote before but now it's very DRY ;) )!
5
u/[deleted] Aug 14 '12 edited Aug 14 '12
Here's my solution in Python. First time getting one of these daily programmer challenges, so I'm happy with myself! If its ugly - its ugly, but I got it!
8
u/lawlrng 0 1 Aug 13 '12 edited Aug 14 '12
Python solution thrown together w/ bonus. =)
Output:
Spaces added by me.
Edit: Cut down the two functions to one because it was bugging me too while driving home.
4
u/daveasaurus Aug 13 '12 edited Aug 14 '12
PYTHON
edit: ^ Added Decode Flag ^
Decoding the bonus (line breaks added by me):
edit, added one liners, (encode at top), decode:
4
u/toxicFork Aug 14 '12
Javascript (with bonus):
3
u/clojure-newbie Aug 14 '12
Clojure (no bonus):
Output:
Note: see my username
3
u/Wedamm Aug 14 '12
Haskell:
3
u/Fapper Aug 16 '12
Ruby!
Would love any feedback. I feel like my C background is causing me to think against Ruby's beautifulness. ಠ_ಠ
Created an extra method to create a key equal to the length of the message:
And then two almost identical encryption and decryption methods (haven't figured out how to take an operator as a parameter yet... lambdas?):
Output:
aaaand working on the bonus!
Posted this before seeing some other solutions... and wow, there some really simple and elegant ways to solve this. :o
2
u/[deleted] Aug 13 '12
mSL: (mIRC - Couldnt be bothered to use another language)
Bonus:
Just to confirm, I
brute forcedthe bonus by going through all the words.Key is:
2
u/paininthebass Aug 13 '12
C++:
2
u/bschlief 0 0 Aug 14 '12 edited Aug 14 '12
Ruby. Still working on the bonus.
edit: mods, thanks for the flair!
edit2: made it so you can decrypt if you pass in :- and encrypt if you pass in :+, defaulted to encrypt.
2
u/oskar_stephens Aug 14 '12
Ruby:
The repetition of the collector in the reduce block feels somewhat unclean. Any tips on how I could increment the index on the source text without explicitly stating index += 1?
2
u/path411 Aug 15 '12 edited Aug 15 '12
JavaScript (No Bonus):
2
u/andkerosine Aug 15 '12 edited Aug 16 '12
Pretty straightforward in Ruby, courtesy of
#cycleand the fact that negative modulus behaves as it should:2
u/larg3-p3nis Aug 16 '12
At long last! Java - with space characters, user prompts and lots of comments.
I would also like some clarification because this really confused me. Using arrays I'll have a character using 0. What if this character is encoded with itself? The encoded version will still be 0. Is this a "logical bug" with this system? (please tell me it is because I spent the last 3 days trying to solve it and just couldn't).
2
u/[deleted] Aug 16 '12
C++:
2
u/cheapshot Aug 18 '12 edited Aug 18 '12
Hi all. I am very new to programming and currently working on JavaScript (the code below) and Ruby.
Here is my very ugly solution. I'm really glad I found this subreddit and will try to complete the challenges as best I can. Thanks!
OUTPUT: ZSEFOCKTSDZAK
2
u/Postscript624 Aug 18 '12
I'm sort of brand new to programming in general, but here's my clumsy Python solution:
Edited: formatting
1
u/[deleted] Aug 14 '12
I'm a Python newbie, so here's my not-as-elegant solution (no bonus):
1
u/prondose 0 0 Aug 13 '12
Perl, w/o strict
1
u/[deleted] Aug 13 '12
C, no bonus
1
u/02471 Aug 13 '12 edited Aug 16 '12
In C without the bonus.
1
u/ixid 0 0 Aug 14 '12 edited Aug 14 '12
D one-liners:
These are templates using the array operations syntax (t[] = t[] + k[]). decode adds 26 to the subtraction so the value doesn't wrap.
1
u/Erocs Aug 14 '12 edited Aug 14 '12
Scala 2.9
Output:
1
u/[deleted] Aug 14 '12
Although this has been solved by Python more than once, I want to give a go, being the stubborn (newb) goat I am. However, I need to ask a question. Does anyone know if i had one list longer than the other, is there a way to loop over the shorter loop until the end of the longer loop?
Does that make sense? God I feel like such a bad programmer when I try these challenges.
1
u/SwimmingPastaDevil 0 0 Aug 14 '12 edited Aug 15 '12
Works for both encode and decode.
Spent way too much time trying to decode using 'GLADOS'. Had look up here for the key.
EDIT
Compressed the fuck out of my code:
Used pivotallever's suggestion to remove some lines.
1
u/school_throwaway Aug 14 '12
Python
1
u/hektor441 Aug 14 '12
In python: alp = map(chr,xrange(65,91))
Output:
1
u/skeeto -9 8 Aug 14 '12 edited Aug 14 '12
In Elisp,
Example usage,
And the bonus, I just made sure there were lots of Es.
keyswas a list of all your words.1
u/goldjerrygold Aug 14 '12
c for easy addition of chars:
1
u/zoidbergLOL Aug 14 '12 edited Aug 16 '12
EDIT:Ruby without bonus.
1
u/fancysuit Aug 14 '12 edited Aug 14 '12
C#
Sorry, I only have time for encrypt right now. Maybe I'll append it later.
Edit: minor improvement
1
u/5hassay Aug 14 '12 edited Aug 14 '12
(Python) code not comparable to the succinct code I am seeing, but anyway here's my module to find the key for the bonus message (not even going to try to fix the actual vigenerecipher module in):
EDIT: Removed pseudo-code commenting I forgot to remove
EDIT: Added bonus message (spoiler!):
1
u/joboscribe Aug 15 '12 edited Aug 15 '12
my Python, which i will post before looking at other people's solutions make me want to cry:
Output
No optional bonus for me.
1
u/ademus4 Aug 15 '12 edited Aug 15 '12
Python (no bonus):
Output:
1
u/[deleted] Aug 16 '12 edited Aug 17 '12
My python solution
1
u/Metaluim 0 0 Aug 16 '12
In C:
1
u/[deleted] Aug 16 '12 edited Aug 16 '12
C++: http://ideone.com/i38Az
The first two functions could be left out if you assumed contiguous ASCII characters (not guaranteed by the standard). I also make sure to only use the % operator on nonnegative numbers to avoid relying on implementation defined behavior. I didn't include any error handling, strings including characters outside the alphabet will just fall over that assertion. I'm also making
1
u/Scroph 0 0 Aug 16 '12
PHP Solution.
Functions :
Bonus :
There was certainly a better way to remove the special characters in the bonus but I was too lazy to look it up.
1
u/Yuushi Aug 18 '12
Haskell:
1
u/[deleted] Aug 18 '12
Not as neat as it can be done in Python, but it's been a while since I last used it. No bonus
Output:
1
u/hydrohawke Aug 18 '12
This is my first time doing one of these. Comparing my solution to others, mine seems a little unwieldy. Any advice on how to make my code leaner would be greatly appreciated.
1
u/[deleted] Aug 19 '12
Python solution. Not very elegant but it works.
1
u/swarage 0 0 Aug 19 '12 edited Aug 19 '12
Excuse my obsene amount of code:
http://pastebin.com/RzsaTRgi
I pretty much bruteforced this problem (I'm not the best programmer)
1
u/SpearShot Aug 20 '12
Java:
I'm still pretty new to programming and this is the first challenge I've done, so any comments or tips are appreciated. :)
Everything in the main method is just prompting to get the key, the message, and whether to decrypt or encrypt from the user. I combined the decrypt and encrypt into a single method and used a boolean to control the flow and give the right output.
How I did the bonus:
I just scrolled through the output until I saw a line that wasn't scrambled.
1
u/pkraack Aug 20 '12
took advantage of unicode and mod maybe drawn out abit much
1
u/stgcoder Aug 21 '12 edited Aug 21 '12
Python:
Bonus:
Code used to find the bonus key:
1
u/swarage 0 0 Aug 26 '12
solution using ruby w/o bonus : http://pastebin.com/nUVRjH4s
1
u/anhyzer_way Sep 07 '12
Javascript no bonus:
1
u/spacedhat Sep 28 '12 edited Oct 01 '12
c, no bonus. The text to crypt is taken from stdin. Arg 1 should be C for Crypt, or D for decrypt. Arg 2 is the key. Example: echo "CRYPTEME" | ./program C KEY
1
u/kcoPkcoP Jan 09 '13
Java solution, no bonus
1
u/marekkpie Jan 15 '13 edited Jan 15 '13
Lua. Pretty simple, and Lua's first class functions allows for some nice code reuse.
C. There's a lame "feature" in C that makes the modulus return negative numbers, so you need to roll your own:
1
u/InvisibleUp Aug 14 '12 edited Aug 14 '12
Here's my take, which for only learning C about a week ago, isn't that bad.
EDIT: Also forgot to mention it only encrypts, and require you to repeat the key manually. (EX: instead of "GLADOS", you need "GLADOSGLADOSG")