Skip to content

Instantly share code, notes, and snippets.

@b1tninja
Last active August 29, 2015 14:22
Show Gist options
  • Save b1tninja/54f9f323e719eed73c6c to your computer and use it in GitHub Desktop.
Save b1tninja/54f9f323e719eed73c6c to your computer and use it in GitHub Desktop.
Blowfish for IRC
import struct, string
from Crypto.Cipher import Blowfish
def blow(key, plaintext):
if not isinstance(plaintext, str):
raise TypeError('Plaintext must be str')
else:
if len(plaintext) % 8 != 0:
plaintext += "\0" * (8-(len(plaintext)%8))
cipher = Blowfish.new(key, Blowfish.MODE_ECB)
ciphertext = ''
charset = list('./' + string.digits + string.ascii_lowercase + string.ascii_uppercase)
for j in range(0,len(plaintext),8):
block = cipher.encrypt(plaintext[j:j+8])
(low, high) = struct.unpack('>LL', block)
while high:
ciphertext += charset[high%64]
high //= 64
if len(ciphertext) % 6 != 0:
ciphertext += charset[0] * (6-len(ciphertext)%6)
while low:
ciphertext += charset[low%64]
low //= 64
if len(ciphertext) % 6 != 0:
ciphertext += charset[0] * (6-len(ciphertext)%6)
assert len(ciphertext) % 12 == 0
return ciphertext
def unblow(key, ciphertext):
if not isinstance(ciphertext, str):
raise TypeError('Ciphertext must be str')
else:
assert len(ciphertext) % 12 == 0
cipher = Blowfish.new(key, Blowfish.MODE_ECB)
plaintext = bytearray()
charset = list('./' + string.digits + string.ascii_lowercase + string.ascii_uppercase)
for j in range(0,len(ciphertext),12):
high = 0
for k in range(6):
high |= charset.index(ciphertext[j+k]) << (k*6)
low = 0
for k in range(6):
low |= charset.index(ciphertext[j+k+6]) << (k*6)
plaintext += cipher.decrypt(struct.pack('>LL', low, high))
return plaintext.decode('utf8').rstrip("\0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment