fork(49) download
  1. // for encoding
  2.  
  3. import java.awt.Graphics2D;
  4. import java.awt.Image;
  5. import java.awt.image.BufferedImage;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.FileNotFoundException;
  10. import java.io.IOException;
  11.  
  12. import javax.imageio.ImageIO;
  13.  
  14.  
  15. public class Stegnography {
  16.  
  17. private BufferedImage image=null,new_image=null;
  18. private int pixels[][];
  19. private char Data[];
  20. private int Width,Height;
  21. private int Binary[];
  22. private int Length=0;
  23.  
  24. public Stegnography()
  25. {
  26. File fileName=new File("C:\\Users\\Umer\\Desktop\\project.png");
  27.  
  28. try
  29. {
  30. new_image= ImageIO.read(fileName);
  31.  
  32. image=new BufferedImage(new_image.getWidth(), new_image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
  33.  
  34. Graphics2D graphics = image.createGraphics();
  35. graphics.drawRenderedImage(new_image, null);
  36.  
  37. graphics.dispose();
  38.  
  39. }
  40. catch (IOException e)
  41. {
  42. e.printStackTrace();
  43. }
  44.  
  45. pixels= new int[image.getWidth()][image.getHeight()];
  46.  
  47. Binary= new int[8];
  48.  
  49. Width=image.getWidth();
  50. Height=image.getHeight();
  51.  
  52. for(int i=0;i<Width;i++)
  53. {
  54. for(int j=0;j<Height;j++)
  55. {
  56. pixels[i][j]=image.getRGB(i, j);
  57. }
  58. }
  59.  
  60. String name= "adsf asdfasd fasdf asdasfa a dfasdfas fas fasdf asdf asdf sadf adsf asdf asd fasdf sadfadsfadsf adsf adsf ads fasdf asdf adsf as dfasfasd fadsf adsf asdf as" +
  61. "df as fasdf asdf asdf asd fas fasf addsf asf adsf asfa dsf dasf as fad fsadf adsf adsf sadf adsf adsf af asddf as dfds" +
  62. "asdfas fasf as fasf asdf sa fasf adfafdsfasdfasf";
  63. Length=name.length();
  64.  
  65. Data=name.toCharArray();
  66.  
  67.  
  68.  
  69.  
  70. WriteLSB();
  71.  
  72. try
  73. {
  74. ImageIO.write(image, "png", fileName);
  75. }
  76. catch (IOException e)
  77. {
  78.  
  79. e.printStackTrace();
  80. }
  81.  
  82. System.out.println("Encoding Completed");
  83. }
  84.  
  85. private void WriteLSB()
  86. {
  87. int temp,tempText,x=0,p=0,i,j=0;
  88. Binary((int) Data[x]);
  89. x++;
  90.  
  91. for(i=0;(i<Width && x<Data.length);i++)
  92. {
  93. for(j=0;( j<Height && x<Data.length);j++)
  94. {
  95.  
  96. int change=0;
  97.  
  98. for(int k=0;k<4;k++)
  99. {
  100. if(k==0)
  101. {
  102. change=1;
  103. }
  104. else
  105. if(k==1)
  106. {
  107. change=256;
  108. }
  109. else
  110. if(k==2)
  111. {
  112. change=65536;
  113. }
  114. else
  115. if(k==3)
  116. {
  117. change = 16777216;
  118. }
  119.  
  120. if(Binary[p]==0)
  121. {
  122. pixels[i][j]= pixels[i][j] & ~change; // ~1 ki all bits 1 hoti ha except LSB
  123. }
  124. else
  125. if(Binary[p]==1)
  126. {
  127. pixels[i][j]= pixels[i][j] | change; // only LSB ko 1 krna ha
  128. }
  129. p++;
  130.  
  131. if(p==8)
  132. {
  133. p=0;
  134. Binary((int) Data[x]);
  135. x++;
  136. }
  137. }
  138.  
  139.  
  140.  
  141. }
  142. }
  143.  
  144.  
  145. for(i=0;i<Width;i++)
  146. {
  147. for(int h=0;h<Height;h++)
  148. {
  149. image.setRGB(i, h,pixels[i][h]);
  150. }
  151. }
  152.  
  153. }
  154.  
  155. private void Binary(int temp)
  156. {
  157. Binary=null;
  158. Binary= new int[8];
  159.  
  160. for(int i=7;i>=0;i--)// cuz character is of 1 byte
  161. {
  162. Binary[i]=temp%2;
  163.  
  164. System.out.print(Binary[i]);
  165. temp/=2;
  166. }
  167. System.out.println();
  168. }
  169. }
  170.  
  171.  
  172. // for decoding
  173.  
  174. import java.awt.Image;
  175. import java.awt.image.BufferedImage;
  176. import java.io.BufferedWriter;
  177. import java.io.ByteArrayOutputStream;
  178. import java.io.File;
  179. import java.io.FileInputStream;
  180. import java.io.FileNotFoundException;
  181. import java.io.FileOutputStream;
  182. import java.io.FileWriter;
  183. import java.io.IOException;
  184.  
  185. import javax.imageio.ImageIO;
  186.  
  187.  
  188. public class RetreiveData {
  189.  
  190. private BufferedImage image=null;
  191. private int pixels[][];
  192. private char Data[];
  193. private int Width,Height;
  194. private String myString="";
  195.  
  196. public RetreiveData()
  197. {
  198. try
  199. {
  200. image= ImageIO.read(new File("C:\\Users\\Umer\\Desktop\\project.png"));
  201. }
  202. catch (IOException e)
  203. {
  204. e.printStackTrace();
  205. }
  206.  
  207.  
  208.  
  209. pixels= new int[image.getWidth()][image.getHeight()];
  210.  
  211. Width=image.getWidth();
  212. Height=image.getHeight();
  213.  
  214. System.out.println(Width);
  215. System.out.println(Height);
  216.  
  217. for(int i=0;i<Width;i++)
  218. {
  219. for(int j=0;j<Height;j++)
  220. {
  221. pixels[i][j]=image.getRGB(i, j);
  222. }
  223. }
  224.  
  225. FileWriter fw=null;
  226. File file=null;
  227. BufferedWriter bw=null;
  228.  
  229. try
  230. {
  231. file = new File("C:\\Users\\Umer\\Desktop\\Retreive.txt");
  232. fw = new FileWriter(file);
  233.  
  234. bw= new BufferedWriter(fw);
  235.  
  236. }
  237. catch (Exception e)
  238. {
  239. e.printStackTrace();
  240. }
  241.  
  242.  
  243. ReadLSB();
  244.  
  245. try
  246. {
  247. bw.write(myString);
  248. bw.close();
  249. }
  250. catch (IOException e)
  251. {
  252. e.printStackTrace();
  253. }
  254.  
  255. System.out.println(myString);
  256. }
  257.  
  258. private void ReadLSB()
  259. {
  260. int temp,tempText=0,x=0,p=0;
  261.  
  262. for(int i=0;i<Width;i++)
  263. {
  264. for(int j=0;j<Height;j++)
  265. {
  266. temp=pixels[i][j];
  267.  
  268. int change=0;
  269.  
  270. for(int k=0;k<4;k++)
  271. {
  272. if(k==0)
  273. {
  274. change=1;
  275. }
  276. else
  277. if(k==1)
  278. {
  279. change=256;
  280. }
  281. else
  282. if(k==2)
  283. {
  284. change=65536;
  285. }
  286. else
  287. if(k==3)
  288. {
  289. change = 16777216;
  290. }
  291.  
  292.  
  293. if((pixels[i][j] & change)==1)
  294. {
  295. tempText|=1 ;
  296. }
  297.  
  298. tempText<<=1;
  299.  
  300. p++;
  301. if(p==8)
  302. {
  303. myString += (char) tempText;// convert int to String and storing in String object
  304. p=0;
  305. //x++;
  306. if(tempText!=272)
  307. System.out.println("Temp is "+tempText);
  308. tempText=0;
  309.  
  310. }
  311. }
  312.  
  313.  
  314.  
  315. }
  316. }
  317. }
  318. }
  319.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty