Advertisement
Guest User

danix.steg.Steg

a guest
Sep 28th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. package danix.steg;
  2.  
  3. //Author: danix
  4.  
  5. //Digital steganographer
  6. //Encrypts and decrypts png images
  7. //To encrypt run java -jar Steg.jar [base] [image to hide] [output]
  8. //To decrypt run java -jar Steg.jar [encrypted] [decrypted output]
  9. //Works with pngs
  10.  
  11.  
  12. //I give you the permission to run, edit and distribute this program
  13. //and source code if you maintain the author tag
  14.  
  15.  
  16. import java.io.File;
  17. import java.io.IOException;
  18. import javax.imageio.ImageIO;
  19. import java.awt.image.BufferedImage;
  20.  
  21. public class Steg {
  22.  
  23.  
  24.     //Arguments:
  25.     //3 arguments encryption: base image, secret image, encrypted filename
  26.     public static void main(String[] args) {
  27.        
  28.         try {
  29.             switch (args.length) {
  30.             case 3:
  31.                 encrypt(args);
  32.                 break;
  33.             case 2:
  34.                 decrypt(args);
  35.                 break;
  36.             default:
  37.                 System.out.println("Argument error");
  38.                 System.out.println("Encryption: java -jar Steg.jar [base] [image to hide] [output]");
  39.                 System.out.println("Decryption: java -jar Steg.jar [encrypted] [decrypted output]");
  40.             }
  41.         } catch (IOException iox) {
  42.             System.out.println(iox.getMessage());
  43.             iox.printStackTrace(System.out);           
  44.         }
  45.     }
  46.    
  47.     //Hides in a pixel of the original image a pixel from the hidden image
  48.     private static int doMask(int input, int cover) {
  49.         int mask = 0xc0c0c0c0;
  50.         cover &= mask;
  51.         cover >>>= 6;
  52.         input &= ~(mask >>> 6);
  53.         return input |= cover;     
  54.     }
  55.    
  56.     //Extracts the pixel corresponding to the hidden image in the encrypted image
  57.     private static int extractHiddenPixel(int encrypted) {
  58.         int mask = 0x03030303;
  59.         encrypted &= mask;     
  60.         encrypted <<= 6;
  61.         for (int i=0; i<3; i++)
  62.             encrypted |= (encrypted >>> 2);        
  63.         return encrypted;
  64.     }
  65.    
  66.     //Writes a hidden image over a base image using digital steganography
  67.     private static void encrypt(String[] args) throws IOException {
  68.         String base = args[0];
  69.         String hidden = args[1];
  70.         String output = args[2];
  71.        
  72.         BufferedImage inputImage = ImageIO.read(new File(base));
  73.         BufferedImage hiddenImage = ImageIO.read(new File(hidden));
  74.        
  75.         if (!( (inputImage.getWidth() == hiddenImage.getWidth()) &&
  76.              (inputImage.getHeight() == hiddenImage.getHeight()))) {
  77.                 System.out.println("Error: hidden image must have the same size as the input image!");
  78.                 System.exit(1);
  79.         }
  80.          
  81.         int width = inputImage.getWidth();
  82.         int height = inputImage.getHeight();
  83.        
  84.         for (int i=0; i<width; i++) {
  85.             for (int j=0; j<height; j++) {
  86.                 int inputPixel = inputImage.getRGB(i, j);
  87.                 int hiddenPixel = hiddenImage.getRGB(i, j);
  88.                 inputPixel = doMask(inputPixel, hiddenPixel);              
  89.                 inputImage.setRGB(i, j, inputPixel);
  90.             }
  91.         }
  92.        
  93.         if(!ImageIO.write(inputImage, "png", new File(output)))
  94.             System.out.println("Failed writing :(");
  95.     }
  96.    
  97.     //Reverts the process
  98.     private static void decrypt(String[] args) throws IOException {
  99.         String encrypted = args[0];
  100.         String decrypted = args[1];
  101.        
  102.         BufferedImage encryptedImage = ImageIO.read(new File(encrypted));
  103.        
  104.         int width = encryptedImage.getWidth();
  105.         int height = encryptedImage.getHeight();
  106.        
  107.         for (int i=0; i<width;i++) {
  108.             for (int j=0; j<height; j++) {
  109.                 int encryptedPixel = encryptedImage.getRGB(i,j);
  110.                 int decryptedPixel = extractHiddenPixel(encryptedPixel);
  111.                 encryptedImage.setRGB(i, j, decryptedPixel);
  112.             }
  113.         }
  114.        
  115.         ImageIO.write(encryptedImage, "png", new File(decrypted));         
  116.     }
  117.    
  118.    
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement