views:

82

answers:

1

This a wider question than my previous one but related.

I want to implement oldish crypto systems in Ruby such as ADFGVX, VIC cipher and others, mostly for fun. These cryptosystems are built on top of more general systems like substitution (monoalphabetic such as Caesar or polyalphabetic like Vigenere) and transposition (simple, double). My question is really how would you create a hierarchy of classes to handle both cryptosystems and keys.

Something like Vigenere < Substitution < SimpleCipher or something else? How about keys? Some substitution keys are condensed before use ("ARABESQUE" becomes "ARBESQU") whereas most transposition keys are not and so on.

Right now, I have a very basic design doc (I said basic) and as I think about it, I can't seem find a satisfying way for this, so I come here for your wisdom.

Implementation will be in Ruby but unless the design must use multiple inheritance, it is not about Ruby itself.

Proof of concept (not yet runnable and possibly wrong) here in Mercurial.

Thanks.

+1  A: 

Think about the class signatures. Every crypto algorithm is going to have two methods, encrypt() and decrypt(), and you have to consider block and streaming ciphers (ie, many algorithms want data in fixed size blocks.)

Once you're below that level, though, the methods are going to look fairly radically different, and there's not a whole lot of shared behavior at that level.

So I suspect what you want is more of a module or mixin structure, not inheritance. Just because you have a taxonomy, it doesn't mean that taxonomy should be an inheritance hierarchy.

There's a lot of good discussion on this sort of thing; look for talk about whether Ostrich < Bird. See, Birds fly() ... except for Ostriches.

Charlie Martin
Thanks Charlie, I guess I always forget about mixins. Although it this case, I have the feeling that there is code to share because the building blocks are mostly the same for these old systems (subst, transp, checkerboards and so on).
Keltia
Yup, could well be. You might think then about if you need a module that provides crypto algorithms and contains or mixes in cryptographic primitives. Have a look at the Cryptol language: http://galois.com/technology/communications_security/cryptol
Charlie Martin