Cool thanks for posting the code. There's a couple of interesting things about it that are worth calling out:
1) in generateKey, you're building a potentially huge number (what if your key that was used, instead of being "lucid" was actually "000000000000000000000000" or something). Then you're calling Convert.ToInt32 on it. I don't know how that behaves because I don't know C# and leave looking it up to you. It might throw an exception right then and there. Or it might return a signed integer, which is bad because then the values you compute will have minus signs in it, which wouldn't jive well with your encrypted format... or it would just return an unsigned, which I guess is fine, since you don't care about the actual number.
2) Also in generateKey, you divide by z.length twice. Is this for any particular reason? Are you just shortening the value? If so, you'll probably get better results by modding it with a prime number that's close to your maximum desired value.
3) Again for generateKey... why didn't you just go "return x.GetHashCode()" for it, or implement one of the known good hash codes for strings?
Finally, in general, it looks like you're doing a lot of work for something very simple. What I mean by that is, you're basically grabbing your numerical key and applying it to every character uniformly (multiply to encrypt, divide to decrypt). In practice, what this amounts to is a basic substitution cipher, where, given the key, every letter is mapped to another symbol, consistently.
If someone were trying to break your system by looking at the input you've provided so far, they'd guess at least this much, because "dreamviews" has two e's in it, and sure enough, the ciphertext shows the same value (4481132) in the exact place where the e's would show up. So, if I were given a much longer message, I could probably just run it through a frequency analyzer and fairly quickly decode the substitution (without knowing about how you generated the key).
To make substitution ciphers a little bit more robust, you need to do something like use a multi-tiered substitution (many layers), where after each letter, the layers shift in some way, so the overall substitution from start to finish looks pretty random if you don't understand the mechanism. This is how the German enigma machines worked in World War 2. Even those were eventually cracked by Alan Turing and friends. They've even built devices called Bombes to automate the cracking.
|
|
Bookmarks