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