-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsteg.py
77 lines (61 loc) · 3.3 KB
/
steg.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
#Bitcoin steganography library, inserts a private key, wif by default into a named file by altering the LSB of the r
#in the rgb palette of the pixel.
#Hex bitcoin private key is 64 bytes (512 bits to hide)
#WIF bitcoin private key is 51 bytes (408 bits to hide) and easily importable into a wallet for sweeping..
__author__ = 'pete'
from bitcoin import * #probably just need encode_privkey() and random_key()
from PIL import Image
def create_wif_key( key):
return encode_privkey(key, 'wif')
class Steg(): #instantiate a class which allows steganography data in and out of im..
#def __init__(self,):
def encode(self, file, data, output='output.png'): #put data into image..default output image output.png
self.im = Image.open(file)
self.im_w, self.im_h = self.im.size
self.rgb_im = self.im.convert('RGB')
self.output = output
self.data = data + sha256(data)[-4:]
self.data = chr(len(self.data)) + self.data
#print self.data, len(self.data) #append checksum and prepend str length byte for easy extraction..
self.datalen = len(self.data) #number of bytes
self.bitcount = self.datalen * 8 #number of bits in bytestring to disguise
s = []
for x in range(0,self.datalen): #convert the data into an ascii bit list.
p = bin(ord(self.data[x]))
p = p.lstrip('-0b')
while len(p) < 8: #pad the zero's up to 8
p = '0'+ p
s.append(p)
s = "".join(s) #list to string
if self.bitcount < self.im_w: #can fit the entire message in first row of pixels..
for x in range(0,self.bitcount):
r, g, b = self.rgb_im.getpixel((x,0)) #get the pixel in r,g,b form..
r = r & 254 #and 11111110
if s[x] == '1':
r = r | 1 #or 00000001
self.rgb_im.putpixel((x,0),(r,g,b)) #put the replaced pixel with LSB altered..
self.rgb_im.save(self.output)
def decode(self,file = 'output.png'): #n=512 (64 hex * 8), n=408 (64 hex -> 51 wif * 8) extract private key from file..
self.im = Image.open(file)
self.im_w, self.im_h = self.im.size
self.rgb_im = self.im.convert('RGB')
j = 0
p = ""
for x in range(0,8): #get the string length byte out first..
r, g, b = self.rgb_im.getpixel((x,0))
p = p + str(r & 1)
j += 1
self.bytelength = int(p,2)
t = []
for x in range(0,(self.bytelength)): #convert the ascii bit string back to ascii integer string
p=""
for y in range(0,8):
r, g, b = self.rgb_im.getpixel((j,0))
p = p + str(r & 1) #and each r value with b 00000001 to extract the encode bit as a byte
j += 1
t.append(chr(int(p,2)))
t = "".join(t)
if sha256(t[:-4])[-4:] == t[-4:]: #last four bytes of t is checksum. do sha256 up to that to ensure integrity
return t[:-4]
else:
return t + ' failed checksum'