Advertisement
Guest User

Normal

a guest
Mar 1st, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package steganography;
  7.  
  8. import java.awt.Color;
  9. import java.awt.image.BufferedImage;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.FileOutputStream;
  14. import java.io.IOException;
  15. import java.util.ArrayList;
  16. import java.util.Arrays;
  17. import java.util.List;
  18. import java.util.Random;
  19. import javax.imageio.ImageIO;
  20.  
  21. /**
  22. *
  23. * @author DiePiePaw
  24. */
  25. public class Normal {
  26.  
  27. public void runSteganography(File image, File data, String key) throws IOException {
  28. int[][] matrix = this.getPixelMatrix(image);
  29. int[] keyUse = this.generateShuffleIndex(key, matrix.length, matrix.length);
  30. char[] dataArray = this.getBitData(data);
  31. int[][] matrixStego = this.insertData(dataArray, matrix, keyUse);
  32. this.makeImage(this.getImageWidth(image), this.getImageHeight(image), matrixStego);
  33.  
  34. int[] extract = this.extractData(keyUse, matrixStego);
  35.  
  36. //pre process data
  37. int getLengthName = extract[0];
  38. int[] realData = new int[extract.length-2-getLengthName];
  39. int indexReal=0;
  40. for(int n=1+getLengthName;n<extract.length-1;n++){
  41. realData[indexReal] = extract[n];
  42. indexReal++;
  43. }
  44. String name = this.getName(getLengthName, extract);
  45. this.makeDataFile(realData, "extract.txt");
  46. Function func = new Function();
  47. func.calculatePSNR(matrix, matrixStego);
  48. }
  49.  
  50. public boolean isContain(int[] array, int value) {
  51. if(Arrays.asList(array).contains(value)) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. }
  57.  
  58. public int[] generateShuffleIndex(String key, int length, int max) {
  59. int seed=0;
  60. char[] keyChar = key.toCharArray();
  61.  
  62. for(int iterate=0; iterate<keyChar.length; iterate++) {
  63. seed+=keyChar[iterate];
  64. }
  65.  
  66. int[] result = new int[length];
  67. int iterator = 0;
  68. Random random = new Random(seed);
  69.  
  70. while(iterator<result.length) {
  71. int tempRandom = random.nextInt((max-0)+0);
  72. if(!isContain(result,tempRandom)) {
  73. result[iterator] = tempRandom;
  74. iterator++;
  75. }
  76. }
  77.  
  78. return result;
  79. }
  80.  
  81. public int[] getPixel(BufferedImage buffer, int x, int y) {
  82. int argb = buffer.getRGB(x, y);
  83. int rgb[] = new int[] {
  84. (argb >> 16) & 0xff,
  85. (argb >> 8) & 0xff,
  86. (argb ) & 0xff
  87. };
  88. return rgb;
  89. }
  90.  
  91. public int[][] getPixelMatrix(File image) throws IOException {
  92. BufferedImage buffer = ImageIO.read(image);
  93. int[][] pixelMatrix = new int[buffer.getWidth()*buffer.getHeight()][3];
  94. int index=0;
  95. for(int n=0;n<buffer.getHeight();n++) {
  96. for(int m=0;m<buffer.getWidth();m++) {
  97. pixelMatrix[index] = this.getPixel(buffer, m, n);
  98. index++;
  99. }
  100. }
  101. return pixelMatrix;
  102. }
  103.  
  104. public char[] getBitData(File data) throws FileNotFoundException, IOException {
  105. char[] resultBit;
  106. String tempStringData = String.format("%8s", Integer.toBinaryString((byte)data.getName().length() & 0xFF)).replace(' ', '0');
  107.  
  108. for(int n=0;n<data.getName().length();n++) {
  109. tempStringData+=String.format("%8s", Integer.toBinaryString((byte)data.getName().charAt(n) & 0xFF)).replace(' ', '0');
  110. }
  111.  
  112. byte[] tempData = new byte[(int)data.length()];
  113. FileInputStream dataStream = new FileInputStream(data);
  114. dataStream.read(tempData, 0, tempData.length);
  115.  
  116. int index=0;
  117. while(index<data.length()) {
  118. tempStringData+=String.format("%8s", Integer.toBinaryString(tempData[index] & 0xFF)).replace(' ', '0');
  119. index++;
  120. }
  121. tempStringData+="00000000";
  122. resultBit = tempStringData.toCharArray();
  123. return resultBit;
  124. }
  125.  
  126. public int[][] insertData(char[] resultBit, int[][] matrixImage, int[] indexPixel) {
  127. int indexData=0;
  128. int[][] matrix = matrixImage;
  129.  
  130.  
  131. for(int n=0;n<resultBit.length;n++){
  132. for(int m=0;m<matrix[n].length;m++){
  133. matrix[indexPixel[n]][m] = changeBit(matrix[indexPixel[n]][m], resultBit[indexData]);
  134. indexData++;
  135. if(indexData>=resultBit.length){
  136. break;
  137. }
  138. }
  139.  
  140. if(indexData>=resultBit.length){
  141. break;
  142. }
  143. }
  144.  
  145. return matrix;
  146. }
  147.  
  148. public int changeBit(int image, char data) {
  149.  
  150. int result=image;
  151. if(result%2==0){
  152. if(data=='1'){
  153. result++;
  154. } else {
  155.  
  156. }
  157. } else {
  158. if(data=='0'){
  159. result--;
  160. } else {
  161.  
  162. }
  163. }
  164. return result;
  165. }
  166.  
  167. public int[] extractData(int[] key, int[][] imageMatrix) {
  168. List<Character> tempChar = new ArrayList<Character>();
  169. List<Integer> tempIntData = new ArrayList<Integer>();
  170. boolean repeat=true;
  171. int idxParse=0;
  172. int idxKey=0;
  173.  
  174. System.out.println("");
  175. while(repeat){
  176. for(int n=0;n<imageMatrix[key[idxKey]].length;n++){
  177. tempChar.add(this.getData(imageMatrix[key[idxKey]][n]));
  178. idxParse++;
  179.  
  180. if(idxParse%8==0) {
  181. String tempString = tempChar.get(0).toString();
  182. for(int x=1;x<tempChar.size();x++){
  183. tempString+=tempChar.get(x).toString();
  184. }
  185. tempIntData.add(Integer.parseInt(tempString, 2));
  186. //System.out.print(Integer.parseInt(tempString, 2)+" ");
  187. tempChar.clear();
  188. if(tempIntData.contains(0)){
  189. repeat=false;
  190. }
  191. idxParse=0;
  192. }
  193. }
  194. idxKey++;
  195. }
  196. //System.out.println("");
  197. int[] result = new int[tempIntData.size()];
  198. for(int n=0;n<result.length;n++){
  199. result[n]=tempIntData.get(n);
  200. //System.out.print((char)result[n]);
  201. }
  202.  
  203.  
  204. return result;
  205. }
  206.  
  207. public char getData(int image) {
  208. if(image%2==0) {
  209. return '0';
  210. } else {
  211. return '1';
  212. }
  213. }
  214.  
  215. public String getName(int length, int[] data) {
  216. char[] tempChar = new char[length];
  217. int idxTemp=0;
  218. for(int n=1;n<tempChar.length+1;n++){
  219. tempChar[idxTemp] = (char)data[n];
  220. idxTemp++;
  221. }
  222.  
  223. return new String(tempChar);
  224. }
  225.  
  226. public void makeImage(int width, int height, int[][] imageByte) throws IOException {
  227.  
  228. BufferedImage buffer = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
  229. int idxImage=0;
  230. for(int n=0;n<height;n++){
  231. for(int m=0;m<width;m++){
  232. Color tempCol = new Color(imageByte[idxImage][0],imageByte[idxImage][1],imageByte[idxImage][2]);
  233. buffer.setRGB(m, n, tempCol.getRGB());
  234. idxImage++;
  235. }
  236. }
  237.  
  238. ImageIO.write(buffer, "bmp", new File("saved.bmp"));
  239.  
  240. }
  241.  
  242. public int getImageWidth(File image) throws IOException {
  243. BufferedImage width = ImageIO.read(image);
  244. return width.getWidth();
  245. }
  246.  
  247. public int getImageHeight(File image) throws IOException {
  248. BufferedImage height = ImageIO.read(image);
  249. return height.getHeight();
  250. }
  251.  
  252. public void makeDataFile(int[] streamData, String name) throws FileNotFoundException, IOException {
  253. byte[] byteData = new byte[streamData.length];
  254. for(int n=0;n<streamData.length;n++){
  255. byteData[n] = (byte)streamData[n];
  256. }
  257. FileOutputStream stream = new FileOutputStream(name);
  258. try {
  259. stream.write(byteData);
  260. } finally {
  261. stream.close();
  262. }
  263. }
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement