Created
April 7, 2011 22:21
-
-
Save jonelf/908911 to your computer and use it in GitHub Desktop.
Mihai asked an interesting question on my blog on how to analyze Base64 encoded and encrypted data and this code is the result of me starting to think about that. http://alicebobandmallory.com/articles/2009/09/16/breaking-simple-ciphers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 2011-04-08 [email protected] | |
require 'base64' | |
def caesar(text,n) | |
alphas=('A'..'Z').to_a*2 | |
text.tr('A-Z', alphas[n..n+26].join) | |
end | |
def histogram(message) | |
message.each_char. | |
inject(Hash.new(0)) { |h, k| h[k] += 1; h} | |
end | |
def graph_histogram(h) | |
h.to_a. | |
sort_by{|kv| kv[1]*-1}. | |
map{|a| "#{a[0]} |#{('+'*a[1])[0..120]}#{a[1].to_s}\n"}. | |
join | |
end | |
plaintext=<<EOF | |
There is one feature I notice that is generally missing in "cargo cult science." It's a kind of scientific integrity, a principle of scientific thought that corresponds to a kind of utter honesty — a kind of leaning over backwards. For example, if you're doing an experiment, you should report everything that you think might make it invalid — not only what you think is right about it; other causes that could possibly explain your results; and things you thought of that you've eliminated by some other experiment, and how they worked — to make sure the other fellow can tell they have been eliminated. | |
Details that could throw doubt on your interpretation must be given, if you know them. You must do the best you can — if you know anything at all wrong, or possibly wrong — to explain it. If you make a theory, for example, and advertise it, or put it out, then you must also put down all the facts that disagree with it, as well as those that agree with it. There is also a more subtle problem. When you have put a lot of ideas together to make an elaborate theory, you want to make sure, when explaining what it fits, that those things it fits are not just the things that gave you the idea for the theory; but that the finished theory makes something else come out right, in addition. | |
In summary, the idea is to try to give all of the information to help others to judge the value of your contribution; not just the information that leads to judgment in one particular direction or another. | |
EOF | |
puts graph_histogram( histogram(Base64.encode64(plaintext)) ) | |
puts graph_histogram( histogram(caesar(Base64.encode64(plaintext),7)) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment