Showing posts with label Encryption. Show all posts
Showing posts with label Encryption. Show all posts

Thursday, April 2, 2015

Prolog Enigma Machine Week 3: Finishing Touches

Prolog says Yes.

As you may have noticed on my twitter, my M3 Enigma Machine is really coming together! This is the product of a lot of refining of the primary enigma predicate, which consists of all of transitions from rotor to rotor. While this model is static, I feel that this code could easily be adapted to fit the different rotors. While you're at it, you should also make me a GUI like the amazing one I used for reference. (:

In terms of completeness though, I'm currently ready for the presentation, but I still need to add the plugboard. The good news is, that compared to the rotors, this is an easy task... especially considering I already wrote the predicate! It takes in the original alphabet, a list of pairs, and returns an exchanged alphabet, like this:


alphabet(A),
plugGen(A,[[a,c],[b,f]]).

Would yield

A=[a,b,c,d,e,f]
O=[c,f,a,d,e,b]
 
As it turns out, the professor is giving us another week to work on this so I'm going to take it easy, and just get that plugboard implemented that day. For now enjoy pics of success IO that you can replicate yourself in any other emulator!
 

 

Tuesday, March 24, 2015

Week 2: Putting together a basic M3 Enigma Machine in Prolog


Using what little built in features GNU Prolog has, by the beginning of this week I was able to compile all that I need to begin work on the actual encryption process. Here's a small list of some of the predicates I implemented. I'm not sitting in front of my programming pc at the moment so these may not all be 100% accurate.

  • len(List,Length), returns the length of the list.
  • indexFromChar(List,Char,Index), returns the index of a char in a list.
  • CharFromIndex(List,Char,Index), returns the Char of a index in a list.
  • turn(List,NList), "turns" a list (Tail+Head).
  • turnN(List,Times,NList), turns the list as many times as you'd like.
  • sswap(List,Char1,Char2,NList), replaces the first instance of Char1 with Char2.
  • dswap(List,Char1,Char2,NList), replaces both first instances of Char1 and Char2.
  • plugboard(Alphabet, Lol, Plugboard), generates a plugboard from pairs in Lol.
  • transition(List1,List2,I,O), finds the index of I in List2, returns the Char as O. 
I'm sure most Prolog vets would scoff at my work, but I particularly proud of dswap and sswap, which both saw use in the plugboard function. I've seen in a Tower of Hanoi demonstration by my mate Kyle Godbey that Prolog has a (relatively) small stack (because it does like a million things!), so my concentration became to reduce as many calls to the accumulative dswap as possible. I accomplished this by considering the fact that I'll working with lists that contain one of each character-- rearrangements of the alphabet. I made the predicate follow suite by having it "abandon ship" so to speak once it's swapped two chars to reduce calls to the same function. Even more fun is that dswap knows to call an sswap with the proper parameters in order to complete the remaining swap.

With all of these helpful predicates I've been carving my way through actually constructing the Enigma machine. I'm trying to keep versatility in mind as I code, but at the same time I'm expected to have this done at the end of three weeks, so I'm trying to follow the model of the M3 Enigma Machine, which is common in many simulations. What I've been doing in order to build my first basic model is constantly referring to these two sites: A great visualization and an in-depth look into how the pieces and parts work. A lot of the diagrams were lifted Dartmouth's awesome and detailed simulator, but this page is helpful with taking you through baby steps. Funny enough, this guide to yet another emulator has been helpful in explaining the hefty German vocabulary for the parts as well as how actual messages should look.  

As far as my current implementation, I'm as far as returning from the reflector plate. The goal is to get all the way home this week, and then next week to incorporate lists, and then incorporate the multi-rotor stepping (the whole Royal Flags Wave Kings Above business). 

Saturday, February 28, 2015

Spring Break Project: Enigma machine

You may remember that a while back, I went on an indefinite hiatus due to the pressures of balancing classwork, projects and professional stuff. In light of that trying time, I've convinced myself that I should attempt to pair my projects with my classwork where it's applicable in order to stay afloat. That being said, my next project in Programming Languages will be in Prolog. I've only seen a few samples of Prolog code in action, it seems to be driven largely by logic. Of course everyone says this, but I always thought every programming language was driven by logic! Anyways, the kind of logic Prolog operates on seems to be more like Syllogism in Philosophy. For example:
  • All men are mortal
  • Socrates is a man
  • Therefore Socrates is mortal.
 After a quick google search, I can see that this is also represented in Prolog, which the following:

  • All dogs are canines
  • All canines are mammals
?- deduction(all(dogs, canines), all(canines, mammals), C).
 
{C = some(mammals, dogs)}
{C = all(dogs, mammals)}
{C = some(dogs, mammals)}
  • Therefore: Some mammals are dogs, all dogs are mammals, and some dogs are mammals.
Nice.  So the next is, what do I think I'm going to do in this language? I was thinking about making an Enigma Machine in Prolog. Honestly, I'd rather write a program that generates Engima Machines (for an alphabet of any size, with any number of customized rotors, etc...) BUT, I don't want to get too ambitious. At the end of the day I'd rather be a guy who does more than he says. However, in light of my recent passion for Scheme/Racket, I think I might save the generator for that language. We'll see how it goes. I think the outline will look much like this:
  • Create a simple Enigma machine for 3 letters, then 4 (three rotors, odd and even reflector plates) in Racket.
  • Create an Enigma machine for all 26 letters of the alphabet in Racket. Then in Prolog.
  • Then, if time permits, work on an Enigma generator in Racket.

This way my assignment is covered, and I can also hone my functional programming skills some more. I'll be sure to update my github as I make progress.