-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathComparison.java
105 lines (98 loc) · 3.51 KB
/
Comparison.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
class Comparison
{
public static void main(String[] args)
{
BufferedImage imgA = null;
BufferedImage imgB = null;
File fileA=null, fileB=null;
boolean flag=true;
File folder = new File("Files\\Malware");
File[] listOfFiles = folder.listFiles();
double[] diff = new double[listOfFiles.length];
for (int i = 0; i < listOfFiles.length; i++)
{
fileB = new File("Files\\Malware\\"+listOfFiles[i].getName());
try
{
fileA = new File("Files\\Output\\Output.png");
imgA = ImageIO.read(fileA);
imgB = ImageIO.read(fileB);
}
catch (IOException e)
{
System.out.println(e);
}
int width1 = imgA.getWidth();
int width2 = imgB.getWidth();
int height1 = imgA.getHeight();
int height2 = imgB.getHeight();
if ((width1 != width2) || (height1 != height2))
System.out.println("Error: Images dimensions"+
" mismatch");
else
{
long difference = 0;
for (int y = 0; y < height1; y++)
{
for (int x = 0; x < width1; x++)
{
int rgbA = imgA.getRGB(x, y);
int rgbB = imgB.getRGB(x, y);
int redA = (rgbA >> 16) & 0xff;
int greenA = (rgbA >> 8) & 0xff;
int blueA = (rgbA) & 0xff;
int redB = (rgbB >> 16) & 0xff;
int greenB = (rgbB >> 8) & 0xff;
int blueB = (rgbB) & 0xff;
difference += Math.abs(redA - redB);
difference += Math.abs(greenA - greenB);
difference += Math.abs(blueA - blueB);
}
}
// Total number of red pixels = width * height
// Total number of blue pixels = width * height
// Total number of green pixels = width * height
// So total number of pixels = width * height * 3
double total_pixels = width1 * height1 * 3;
// Normalizing the value of different pixels
// for accuracy(average pixels per color
// component)
double avg_different_pixels = difference / total_pixels;
// There are 255 values of pixels in total
double percentage = (avg_different_pixels / 255) * 100;
diff[i]=percentage;
//System.out.println("Difference Percentage-->" + percentage);
/*if(percentage>0.0653)
{
System.out.println("The file is "+(fileB.getName().replaceFirst("[.][^.]+$", "")));
flag = false;
}*/
} }
double min = diff[0];
int index=0;
for(int i=0;i<listOfFiles.length;i++)
{
if(min>diff[i])
{
min=diff[i];
index=i;
}
}
for(int i=0;i<listOfFiles.length;i++)
{
if(index==i)
{
fileB = new File("Files\\Malware\\"+listOfFiles[i].getName());
System.out.println("The file may be "+(fileB.getName().replaceFirst("[.][^.]+$", "")));
}
}
/*if(flag)
{
System.out.println("Not a malware");
}*/
}
}