-
Notifications
You must be signed in to change notification settings - Fork 0
/
XOR.java
80 lines (75 loc) · 1.99 KB
/
XOR.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ekraal;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
/**
*
* @author root
*/
public class XOR
{
public static byte [] PlainText;
public static byte [] Key;
public static byte [] CipherText;
public void Getfile(String FileName) throws FileNotFoundException, IOException
{
File myInFile = new File(FileName);
FileInputStream InFile = new FileInputStream(myInFile);
PlainText = new byte[(int) myInFile.length()];
InFile.read(PlainText);
InFile.close();
}
public void GenerateRandom(int Size)
{
Key = new byte[Size];
Random Rand = new Random(Size);
//Rand.nextBytes(Key);
Key="mypassword".getBytes();
}
public void OutputFile(byte [] InputBytes, String Name) throws FileNotFoundException, IOException
{
FileOutputStream OutFile = new FileOutputStream(Name);
OutFile.write(InputBytes);
}
public void XOR()
{
int KeyPosition =0;
CipherText = new byte [PlainText.length];
for(int i=0;i<PlainText.length;i++)
{
if(KeyPosition>Key.length-1)
{
KeyPosition= 0;
}
CipherText[i]= (byte) (PlainText[i]^Key[KeyPosition]);
KeyPosition++;
}
}
public void outPut(byte [] DataToShow)
{
for(int i=0;i<DataToShow.length;i++)
{
System.out.printf("%02X ", DataToShow[i]);
}
System.out.println("\n------------------------------------");
}
public static void main(String [] args) throws IOException
{
XOR xor = new XOR();
xor.Getfile("/home/mich01/Desktop/demoXOR.jpg");
xor.outPut(PlainText);
xor.GenerateRandom(4);
xor.outPut(Key);
xor.XOR();
xor.outPut(CipherText);
xor.OutputFile(CipherText, "/home/mich01/Desktop/demoDecrypt.jpg");
}
}