-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathany_base.py
More file actions
executable file
·30 lines (18 loc) · 841 Bytes
/
any_base.py
File metadata and controls
executable file
·30 lines (18 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/python
# I started solving the cryptopals.com challenges, and it slipped my mind in the first challenge that
# "base64 encoding" is not the same as converting to base 64. So, I ended up with this correct but so
# far useless converter between arbitrary bases, that I shall save for future use
domain = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/' # up to base-64
# 0xab = 10101011 = 1+2+8+32+128 = 171
def any_base( input, input_base, output_base):
base10 = 0
for (position, character) in enumerate(reversed(input)):
base10 += (input_base ** position) * domain.index(character)
output = ""
while base10 > 0:
multiplier = base10 / output_base
remainder = base10 % output_base
output = domain[remainder] + output
base10 = multiplier
return output
print any_base("1111", 2, 10)