Skip to content

Commit 0203d77

Browse files
committed
First commit
0 parents  commit 0203d77

10 files changed

+599
-0
lines changed

Cryptopals/HextoB64.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import base64
2+
instr = input()
3+
bytestr = bytes.fromhex(instr)
4+
b64output = base64.b64encode(bytestr)
5+
print(b64output)

Cryptopals/RepeatXor.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from binascii import hexlify
2+
3+
plaintext = b"Burning 'em, if you ain't quick and nimble I go crazy when I hear a cymbal"
4+
key = b"ICE"
5+
def repeat_encrypt(plaintext, key):
6+
cyphtxt = b''
7+
i = 0
8+
while i < len(plaintext):
9+
k = i%len(key)
10+
value = plaintext[i] ^ key[k]
11+
cyphtxt += bytes([value])
12+
i += 1
13+
crypto = str(hexlify(cyphtxt))
14+
print(crypto)
15+
16+
repeat_encrypt(plaintext, key)
17+
18+

Cryptopals/RepeatXorCrack.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
str1= b'this is a test'
2+
str2 = b'wokka wokka!!!'
3+
def hamming_distance(binary_seq_1, binary_seq_2):
4+
5+
dist = 0
6+
print(zip(binary_seq_1, binary_seq_2))
7+
for bit1, bit2 in zip(binary_seq_1, binary_seq_2):
8+
diff = bit1 ^ bit2
9+
print(bin(diff))
10+
dist += sum([1 for bit in bin(diff) if bit == '1'])
11+
12+
return dist
13+
14+
def counter(string):
15+
bytecount = 0
16+
for byte in string:
17+
bytecount += bin(byte).count('1')
18+
return bytecount
19+
20+
def edit_distance (str1, str2):
21+
count1 = counter(str1)
22+
count2 = counter(str2)
23+
print(count1, count2)
24+
dist = abs(count1-count2)
25+
return dist
26+
27+
distance = hamming_distance(str1, str2)
28+
print(distance)
29+

Cryptopals/singlexor.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
CHARACTER_FREQ = {
2+
'a': 0.0651738, 'b': 0.0124248, 'c': 0.0217339, 'd': 0.0349835,
3+
'e': 0.1041442, 'f': 0.0197881, 'g': 0.0158610, 'h': 0.0492888,
4+
'i': 0.0558094, 'j': 0.0009033, 'k': 0.0050529, 'l': 0.0331490,
5+
'm': 0.0202124, 'n': 0.0564513, 'o': 0.0596302, 'p': 0.0137645,
6+
'q': 0.0008606, 'r': 0.0497563, 's': 0.0515760, 't': 0.0729357,
7+
'u': 0.0225134, 'v': 0.0082903, 'w': 0.0171272, 'x': 0.0013692,
8+
'y': 0.0145984, 'z': 0.0007836, ' ': 0.1918182
9+
}
10+
def main():
11+
cyph_array = [bytes.fromhex(line.strip()) for line in open("strings.txt")]
12+
13+
plain_array = []
14+
for cyph in cyph_array:
15+
result = singlechar_xor(cyph)
16+
plain_array.append(result)
17+
sorted_results = sorted(plain_array, key=lambda c: c['score'], reverse=True)[0]
18+
pretty_print_result(sorted_results)
19+
# for x in array:
20+
# singlexor()
21+
#
22+
def acquire_array_from_file(filename):
23+
ciphtxts_array = open(filename, 'r').readlines()
24+
for ciphtxt in ciphtxts_array:
25+
ciphtxt = bytes.fromhex(ciphtxt)
26+
return ciphtxts_array
27+
def pretty_print_result(result):
28+
"""Prints the given resulting candidate in a pretty format."""
29+
print(result['plaintext'].decode().rstrip(), "\tScore:", "{0:.2f}".format(result['score']),
30+
"\tKey:", chr(result['key']))
31+
32+
33+
34+
35+
#must take a single string and try against all possible values.
36+
#returns best.
37+
def singlechar_xor(CYPH):
38+
best_score = 0
39+
for possible_key in range(256):
40+
output = b''
41+
42+
for char in CYPH:
43+
output += bytes([char ^ possible_key])
44+
45+
score = scoring_function(output)
46+
if score > best_score:
47+
best_score = score
48+
49+
result = {
50+
'key': possible_key,
51+
'score': score,
52+
'plaintext': output
53+
}
54+
return result
55+
56+
57+
#takes a single plaintext and scores it.
58+
def scoring_function(plaintext):
59+
score = 0
60+
for single_byte in plaintext:
61+
score += CHARACTER_FREQ.get(chr(single_byte).lower(), 0)
62+
return score
63+
64+
65+
main()

Cryptopals/strings.txt

+327
Large diffs are not rendered by default.

Cryptopals/xor.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
CHARACTER_FREQ = {
2+
'a': 0.0651738, 'b': 0.0124248, 'c': 0.0217339, 'd': 0.0349835,
3+
'e': 0.1041442, 'f': 0.0197881, 'g': 0.0158610, 'h': 0.0492888,
4+
'i': 0.0558094, 'j': 0.0009033, 'k': 0.0050529, 'l': 0.0331490,
5+
'm': 0.0202124, 'n': 0.0564513, 'o': 0.0596302, 'p': 0.0137645,
6+
'q': 0.0008606, 'r': 0.0497563, 's': 0.0515760, 't': 0.0729357,
7+
'u': 0.0225134, 'v': 0.0082903, 'w': 0.0171272, 'x': 0.0013692,
8+
'y': 0.0145984, 'z': 0.0007836, ' ': 0.1918182
9+
}
10+
def get_english_score(input_bytes):
11+
#Returns a score which is the sum of the probabilities in how each letter of the input data
12+
#appears in the English language. Uses the above probabilities.
13+
14+
score = 0
15+
16+
for byte in input_bytes:
17+
score += CHARACTER_FREQ.get(chr(byte).lower(), 0)
18+
19+
return score
20+
21+
22+
def singlechar_xor(input_bytes, key_value):
23+
#XORs every byte of the input with the given key_value and returns the result.
24+
output = b''
25+
26+
for char in input_bytes:
27+
output += bytes([char ^ key_value])
28+
29+
return output
30+
31+
32+
def singlechar_xor_brute_force(ciphertext):
33+
"""Tries every possible byte for the single-char key, decrypts the ciphertext with that byte
34+
and computes the english score for each plaintext. The plaintext with the highest score
35+
is likely to be the one decrypted with the correct value of key.
36+
"""
37+
candidates = []
38+
39+
for key_candidate in range(256):
40+
plaintext_candidate = singlechar_xor(ciphertext, key_candidate)
41+
candidate_score = get_english_score(plaintext_candidate)
42+
43+
result = {
44+
'key': key_candidate,
45+
'score': candidate_score,
46+
'plaintext': plaintext_candidate
47+
}
48+
49+
candidates.append(result)
50+
51+
# Return the candidate with the highest English score
52+
53+
return sorted(candidates, key=lambda c: c['score'], reverse=True)[0]
54+
55+
def pretty_print_result(result):
56+
"""Prints the given resulting candidate in a pretty format."""
57+
print(result['plaintext'].decode().rstrip(), "\tScore:", "{0:.2f}".format(result['score']),
58+
"\tKey:", chr(result['key']))
59+
60+
61+
62+
def main():
63+
ciphertext = bytes.fromhex("1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736")
64+
most_likely_plaintext = singlechar_xor_brute_force(ciphertext)
65+
pretty_print_result(most_likely_plaintext)
66+
67+
main()

PythonCyphers/CaesarCypher.py

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#TO DO: parameters for: used Symbolsets, keys starting from 1 or 0and run all keys. Optimize Warpararound .
2+
3+
#symbolsets; add array for used symbolsets.
4+
ALBET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
5+
albet = 'abcdefghijklmnopqrstuvwxyz'
6+
nums = '0123456789'
7+
8+
SYMBOLS = [ALBET, albet, nums]
9+
10+
cyphtxt = ''
11+
#INPUT
12+
clrtxt= input('Enter your message\n')
13+
key= input('Enter numeric key to decrypt. Skip to brute-force.\n')
14+
mode= input('Enter (E)ncrypt or (D)ecrypt\n')
15+
print(key)
16+
#Single symbol en/dec function.
17+
18+
19+
20+
#check if selected mode is correct
21+
if mode == 'E':
22+
#||'e'||'Encrypt'||'encrypt':
23+
mode = 1
24+
elif mode =='D':
25+
#||'d'||'Decrypt'||'decrypt':
26+
mode = -1
27+
else:
28+
mode = 0
29+
print('Not a valid mode. Enter encrypt/decrypt')
30+
31+
#Run caesar. To be updated with funcion and symbolsets for cicle and isset.
32+
if mode:
33+
if key == '':
34+
for key in range(len(ALBET)):
35+
for symbol in clrtxt:
36+
checkset= len(cyphtxt)
37+
for symset in SYMBOLS:
38+
if symbol in symset:
39+
symIndex = symset.find(symbol)
40+
cyphindex = int(symIndex) + int(key*mode)
41+
if cyphindex >= len(symset):
42+
cyphindex = cyphindex - len(symset)
43+
elif cyphindex < 0:
44+
cyphindex = cyphindex + len(symset)
45+
cyphtxt = cyphtxt + symset[cyphindex]
46+
if checkset == len(cyphtxt):
47+
cyphtxt = cyphtxt + symbol
48+
print('Key ' + str(key) +': '+ cyphtxt)
49+
cyphtxt= ''
50+
else:
51+
for symbol in clrtxt:
52+
checkset= len(cyphtxt)
53+
for symset in SYMBOLS:
54+
if symbol in symset:
55+
symIndex = symset.find(symbol)
56+
cyphindex = int(symIndex) + int(key*mode)
57+
if cyphindex >= len(symset):
58+
cyphindex = cyphindex - len(symset)
59+
elif cyphindex < 0:
60+
cyphindex = cyphindex + len(symset)
61+
cyphtxt = cyphtxt + symset[cyphindex]
62+
if checkset == len(cyphtxt):
63+
cyphtxt = cyphtxt + symbol
64+
print('Key ' + str(key) +': '+ cyphtxt)
65+

PythonCyphers/Hulloworld.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/python3.6
2+
print('Hullo, world!')
3+
print("What's yer name, fella?")
4+
yername= input()
5+
print("pleas'd ta meetcha, " + yername)

PythonCyphers/ReverseCipher.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
clrtxt = input('\nReverseCypher works both for encoding and decoding.\nEnter message:\n')
2+
cyphtxt = ''
3+
4+
i = len(clrtxt) - 1
5+
while i >= 0:
6+
cyphtxt = cyphtxt +clrtxt[i]
7+
i = i - 1
8+
9+
print('\nOutput:\n'+ cyphtxt)

PythonCyphers/TranspositionCypher.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def main():
2+
clrtxt= input()
3+
key = input()
4+
cyphtxt = transEncrypt(key, clrtxt)
5+
6+
print(cyphtxt + '|')
7+
8+
def transEncrypt(mkey, mclrtxt)
9+
cyphtxt = [''] * key

0 commit comments

Comments
 (0)