Skip to content

Instantly share code, notes, and snippets.

@Ninjex
Created February 28, 2014 16:20
Show Gist options
  • Save Ninjex/9273954 to your computer and use it in GitHub Desktop.
Save Ninjex/9273954 to your computer and use it in GitHub Desktop.
This script will take text and embed it into a picture via tampering with pixel LSB values.
#!/usr/bin/ruby
require 'rubygems'
require 'RMagick'
prompt = '> '
puts "Image to embed (not overwritten):"
print prompt
image_file = gets.chomp
img = Magick::Image::read(image_file)[0]
width = img.columns
height = img.rows
list = img.dispatch(0, 0, width, height, "RGB")
def to_bin input
binary = []
array = input.split('')
array.each do |c|
binary << "%08b" % c.ord
end
binary
end
puts "Enter message to stegonographically hide inside #{image_file}:"
print prompt
text = gets.chomp
value = to_bin text
embed = value.join
puts embed
bin_array = []
list.map{|n| bin_array << n.to_s(2)}
count = 0
embed.split('').each do |b|
pixel = bin_array[count]
lsb = pixel[-1..-1]
lsb = b if b != lsb
new_pixel = pixel[0..-2]+lsb
bin_array[count] = new_pixel # embed the pixel
count += 1
end
new_arr = []
bin_array.map{|x| new_arr << x.to_i(2)}
new_img = Magick::Image.constitute(width, height, 'RGB', new_arr)
puts "Pixel Binary LSB conversions done!"
puts "Save Pixel Binary to Image:"
print prompt
out_file = gets.chomp
new_img.write out_file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment