some small fixes

This commit is contained in:
Mari Wahl 2014-10-08 01:38:11 -04:00
parent 0085af90dd
commit 5929b46c08
8 changed files with 118 additions and 40 deletions

View file

@ -3,6 +3,8 @@
- https://www.cryptool.org/en/cryptool1-en
- frequency analyses online:
http://www.simonsingh.net/The_Black_Chamber/hintsandtips.html
http://www.xarg.org/tools/caesar-cipher/
## ROT13
@ -21,7 +23,3 @@ In Python we can use: ```"YRIRY GJB CNFFJBEQ EBGGRA".decode(encoding="ROT13")```
https://docs.python.org/2/library/codecs.html#codec-base-classes
---
Online:
http://www.xarg.org/tools/caesar-cipher/

View file

@ -1,6 +0,0 @@
CryptoAnalysis
==============
* Several implementations of Caesar cipher with frequency analysis.
* Vinegere code.

View file

@ -7,9 +7,7 @@ __author__ = "bt3gl"
import string
FREQ_ENGLISH = [0.0749, 0.0129, 0.0354, 0.0362, 0.1400, 0.0218, 0.0174, 0.0422, 0.0665, 0.0027, 0.0047, 0.0357,
0.0339, 0.0674, 0.0737, 0.0243, 0.0026, 0.0614, 0.0695, 0.0985, 0.0300, 0.0116, 0.0169, 0.0028,
0.0164, 0.0004]
FREQ_ENGLISH = [0.0749, 0.0129, 0.0354, 0.0362, 0.1400, 0.0218, 0.0174, 0.0422, 0.0665, 0.0027, 0.0047, 0.0357,0.0339, 0.0674, 0.0737, 0.0243, 0.0026, 0.0614, 0.0695, 0.0985, 0.0300, 0.0116, 0.0169, 0.0028, 0.0164, 0.0004]
@ -38,7 +36,7 @@ def frequency(msg):
# Compute the word frequencies
dict_freq = dict([(c,0) for c in string.lowercase])
diff = 0.0
for c in msg:
for c in msg.lower():
if 'a'<= c <= 'z':
diff += 1
dict_freq[c] += 1
@ -50,14 +48,13 @@ def frequency(msg):
def decipher(msg):
# Decipher by frequency
min_delta = 1000
best_rotation = 0
min_delta, best_rotation = 20, 0.0
freq = frequency(msg)
for key in range(26):
d = delta(freq, FREQ_ENGLISH)
for k in range(26):
d = delta(freq[k:] + freq[:k], FREQ_ENGLISH)
if d < min_delta:
min_delta = d
best_rotation = key
best_rotation = k
return cipher(msg, -best_rotation)
@ -74,12 +71,17 @@ def decipher_simple(msg):
if __name__ == '__main__':
# creating a cipher
key = 13
text = 'hacker school is awesome!'
cip = cipher(text, key)
dec = decipher(cip)
cipe = cipher(text, key)
print "Cipher: " + cipe
# decyphering
with open('cipher.txt', 'r') as f:
cip = f.readlines()
cip = cip[0].strip()
cipe = decipher(cip)
print "Decipher: " + cipe
print "Cipher: " + cip
print "Decipher: " + dec
assert(text == dec)