-
Notifications
You must be signed in to change notification settings - Fork 23
/
bfcrypt.py
78 lines (63 loc) · 2.04 KB
/
bfcrypt.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from PIL import Image
import PIL
import getpass
from Crypto.Cipher import Blowfish
import numpy as np
def pad_string(str):
new_str = str
pad_chars = 8 - (len(str) % 8)
if pad_chars != 0:
for x in range(pad_chars):
new_str += " "
return new_str
def encrypt(imagename,password):
img = Image.open( imagename )
img.load()
data = np.asarray( img, dtype="int32" )
H, W = len(data), len(data[0])
print(H)
print(W)
pixelstring= ""
for t in data:
for p in t:
for i in p:
pixelstring= pixelstring + str(100+i)
pixelstring= pixelstring+','+str(H)+','+str(W)
crypt_obj = Blowfish.new(password, Blowfish.MODE_ECB)
ciphertext = crypt_obj.encrypt(pad_string(pixelstring))
cipher= open(imagename+'.crypt', 'w')
cipher.write(ciphertext)
def decrypt(ciphername,password):
cipher= open(ciphername, 'r')
ciphertext= cipher.read()
crypt_obj = Blowfish.new(password, Blowfish.MODE_ECB)
pixelstring= crypt_obj.decrypt(ciphertext)
x= pixelstring.split(',')
H,W= int(x[-2]),int(x[-1])
pixelstring= x[0]
pixelstring= [int(pixelstring[i:i+3])-100 for i in range(0, len(pixelstring), 3)]
data = np.zeros((H, W, 3), dtype=np.uint8)
c= 0
for i in range(H):
for j in range(W):
data[i,j]= pixelstring[c], pixelstring[c+1], pixelstring[c+2]
c=c+3
img = Image.fromarray(data, 'RGB')
x= ciphername.split('.')
img.save(x[0]+'_dec.'+x[-2])
img.show()
def encode(filename):
pwd = getpass.getpass("Enter password: ")
try:
encrypt(filename, pwd)
print('Encrypted successfully')
except:
print("Could not encrypt.")
def decode(filename):
pwd = getpass.getpass("Enter password: ")
decrypt(filename+".crypt", pwd)
# try:
# decrypt(filename+".crypt", pwd)
# print('Decrypted successfully')
# except:
# print("Incorrect password.")