Compare commits

..

3 commits

7 changed files with 113 additions and 106 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 MiB

View file

@ -7,11 +7,11 @@ class EncodeImage
@@utils = nil @@utils = nil
def initialize(p, m, i) def initialize(p, m, uri)
@@password = p @@password = p
@@message = m @@message = m
@@utils = ImageUtils.new(@@password, @@message, i) @@utils = ImageUtils.new(@@password, @@message, uri)
@@table = @@utils.generateLookupTable @@table = @@utils.generateLookupTable
end end
@ -30,59 +30,6 @@ class EncodeImage
msg_bits = pixel.encodeBits(msg_bits) msg_bits = pixel.encodeBits(msg_bits)
end end
# for x in 0..s[:width]-1
# for y in 0..s[:height]-1
# loc = {"x": x, "y": y}
# if @@table.include? loc then
# pixel = @@utils.getImage.getPixel(x, y)
# msg_bits = pixel.encodeBits(msg_bits)
# end
# end
# end
@@utils.getImage.saveImage @@utils.getImage.saveImage
end end
# Need to properly sever this at some point...
def decode
puts "Hacked in decode..."
s = @@utils.getImage.getSize
msg_bits = 0
@@table.reverse.each do |loc|
pixel = @@utils.getImage.getPixel(loc[:x], loc[:y])
msg_bits = (msg_bits << 6) | pixel.decodeBits
end
#this trash needs to move somewhere...
puts "Try to convert the message..."
len = msg_bits & 0xffff
msg_bits = msg_bits >> 16
# check the padding bits....
if (msg_bits & 255) != 0 then
puts "Padding failed scrutiny! "
puts msg_bits.to_s(2)
return false
end
msg_bits = msg_bits >> 8
puts "Padding passed scrutiny..."
msg_chars = []
while msg_bits > 0 do
byte = msg_bits & 0xff
msg_chars.push(byte.chr)
msg_bits = msg_bits >> 8
end
msg = msg_chars.reverse.join
return msg
end
end end

View file

@ -1,76 +1,116 @@
require "./pixel_image.rb" require "rmagick"
require "digest" require "digest"
class ImageUtils class ImageUtils
@@magickImg = nil @@magickImg = nil
@@pixelImg = nil
@@password = "" @@password = ""
@@message = "" @@message = ""
@@salt = 0
@@hash = nil @@imagickImg = nil
def initialize(pass, msg, i) @@width = 0
@@height = 0
@@pixel_arr = nil
@@hash = nil
@@rng = nil
def initialize(pass, msg, uri)
@@password = pass @@password = pass
@@message = msg @@message = msg
@@salt = 0
if msg.length > 400 then @@magickImg = Magick::Image.read(uri).first
puts "Maximum length exceeded. Exiting."
exit
end
@@magickImg = i @@width = @@magickImg.columns
@@height = @@magickImg.rows
@@pixelImg = PixelImage.new(@@magickImg) @@pixel_arr = @@magickImg.get_pixels(0, 0, @@width, @@height)
self.generateHash @@hash = self.generateHash
# convert hex string into an int to store for making the lookup table
@@rng = Random.new(@@hash.to_i(16))
puts self.getLookupTable.inspect
end end
def getImage def getPixel(x, y)
return @@pixelImg loc = (y*@@width) + x
return @@pixel_arr[loc]
end
def getSalt
for x in 0..@@width-1
for y in 0..@@height-1
pix = self.getPixel(x, y)
@@salt = @@salt + (pix.red/512)
@@salt = @@salt + (pix.green/512)
@@salt = @@salt + (pix.blue/512)
end
end
h = Digest::SHA512.hexdigest(@@salt.to_s(16))
return h
end end
def generateHash def generateHash
h = Digest::SHA512.hexdigest(@@password) pass_h = Digest::SHA512.hexdigest(@@password)
s = @@pixelImg.getSalt
merge_to_i = h + s return Digest::SHA512.hexdigest(pass_h + @@salt.to_s)
@@hash = Digest::SHA512.hexdigest(h+s)
end end
def generateLookupTable def getBits(o)
msg_bytes = o.unpack("c*")
msg_bits = 0
for b in msg_bytes
msg_bits = (msg_bits << 8) | b
end
return msg_bits
end
def getLookupTable
tbl = [] tbl = []
hash_bits = @@pixelImg.getBits(@@hash) # track hash classes. if too many occur during the interval, abaondon
msg_bits = @@pixelImg.getBits(@@message) # encoding.
clashes = 0
clash_limit = 100
# gross, but pad in the 24 bits that will become the msg length msg_bits = self.getBits(@@message)
# and padding bits...
msg_bits = msg_bits << 24
while msg_bits > 0 do while msg_bits > 0 do
loc = nil loc = nil
loop do loop do
x = (hash_bits.to_i % @@pixelImg.getSize[:width]) x = @@rng.rand(0..@@width)
y = (hash_bits.to_i % @@pixelImg.getSize[:height]) y = @@rng.rand(0..@@height)
loc = {"x": x, "y": y} loc = {"x": x, "y": y}
if !tbl.include?(loc) then
clashes = 0
else
clashes = clashes + 1
end
break if !tbl.include?(loc) if clashes < clash_limit then
hash_bits = hash_bits >> 1 break
else
if hash_bits <= 0 then puts "wow clashed so much sorry for breaking"
puts
puts "wow too much data"
puts "leftover bits: " + msg_bits.to_s(36)
exit exit
end end
end end
hash_bits = hash_bits >> 1
tbl.push(loc) tbl.push(loc)
msg_bits = msg_bits >> 6 msg_bits = msg_bits >> 6
@ -78,4 +118,16 @@ class ImageUtils
return tbl return tbl
end end
def getBits(o)
msg_bytes = o.unpack("c*")
msg_bits = 0
for b in msg_bytes
msg_bits = (msg_bits << 8) | b
end
return msg_bits
end
end end

BIN
images/encoded/arty_new.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 MiB

View file

Before

Width:  |  Height:  |  Size: 2.7 MiB

After

Width:  |  Height:  |  Size: 2.7 MiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 237 KiB

View file

@ -1,14 +1,19 @@
require "rmagick"
require "./image_utils.rb" require "./image_utils.rb"
require "./encode_image.rb" require "./encode_image.rb"
img = Magick::ImageList.new "./images/original/arty_2.jpg"
#srand(12345) #srand(12345)
pass = "my_password" pass = "my_password"
message = "This is the message that I would like to save please " + message = "This is the message that I would like to save please " +
"and thank you also I love you? AND apostrophe's, and " +
"also some commas somewhere hiding around here, It's " +
"gonna be pushing it a bit here... 1234567 let's huck " +
"some more stuff in here to bump it to 255. Wow. We " +
"are going even further. further than anyone believed " +
"`~!@\#\{$%^&*()-_=+\][}{';:'}]} Wooow. 123456789!1234" +
"56789! so put 15 more??????400! TIMES TWO!! " +
"This is the message that I would like to save please " +
"and thank you also I love you? AND apostrophe's, and " + "and thank you also I love you? AND apostrophe's, and " +
"also some commas somewhere hiding around here, It's " + "also some commas somewhere hiding around here, It's " +
"gonna be pushing it a bit here... 1234567 let's huck " + "gonna be pushing it a bit here... 1234567 let's huck " +
@ -16,17 +21,21 @@ message = "This is the message that I would like to save please " +
"are going even further. further than anyone believed " + "are going even further. further than anyone believed " +
"`~!@\#\{$%^&*()-_=+\][}{';:'}]} Wooow. 123456789!1234" + "`~!@\#\{$%^&*()-_=+\][}{';:'}]} Wooow. 123456789!1234" +
"56789! so put 15 more??????400!" "56789! so put 15 more??????400!"
#message = "tests"
puts message.length puts message.length
puts message puts message
puts puts
puts "Initializing EncodeImage..."
start = Time.new.to_f start = Time.new.to_f
em = EncodeImage.new(pass, message, img) puts "Initializing EncodeImage..."
iu = ImageUtils.new(pass, message, "./images/original/arty.png")
puts "Took " + (Time.new.to_f - start).to_s + " seconds to encode... 🐱"
=begin
em = EncodeImage.new(pass, message, "./images/original/arty.png")
puts em.encode puts em.encode
puts "Took " + (Time.new.to_f - start).to_s + " seconds to encode... 🐱" puts "Took " + (Time.new.to_f - start).to_s + " seconds to encode... 🐱"
@ -38,13 +47,12 @@ result = em.decode
puts result puts result
=end
puts "Took " + (Time.new.to_f - start).to_s + " seconds to decode... 🐱" puts "Took " + (Time.new.to_f - start).to_s + " seconds to decode... 🐱"
if message == result then if message == result then
puts "WOW THEY MATCH! 🐱🐱🐱🐱🐱🐱🐱🐱" puts "WOW THEY MATCH! 🐱🐱🐱🐱🐱🐱🐱🐱"
end end
exit
puts
puts "Done!" puts "Done!"