-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXorCrypto.java
More file actions
54 lines (46 loc) · 2.36 KB
/
XorCrypto.java
File metadata and controls
54 lines (46 loc) · 2.36 KB
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
public class XorCrypto
{
static final String KEY = "Password";
static final String SEPARATOR = "----------------------------------------";
static String[] tests = new String[] { "Hello" , "This is a test" , "I want to encode me then decode me :D" };
public static void main ( String[] args )
{
for ( String test : tests )
{
String encoded = encrypt ( test , KEY );
String decrypted = decrypt ( encoded , KEY );
System.out.printf ( "%s%n%s -> %s%n%s -> %s%n" , SEPARATOR , test , encoded , encoded , decrypted );
}
}
static String encrypt ( String plainText , String key )
{
String result = xor ( plainText , key );
return MyBase64.encrypt ( result );
}
static String decrypt ( String base64 , String key )
{
String result = xor ( MyBase64.decrypt ( base64 ) , key );
return result;
}
static String xor ( String input , String key )
{
String result = "";
for ( int i = 0 ; i < input.length () ; i++ )
{
result += ( char ) ( input.charAt ( i ) ^ key.charAt ( i % key.length () ) );
}
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Output //
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Hello -> GAQfHxg= //
// GAQfHxg= -> Hello //
// ---------------------------------------- //
// This is a test -> BAkaAFcGAUQxQQcWBBs= //
// BAkaAFcGAUQxQQcWBBs= -> This is a test //
// ---------------------------------------- //
// I want to encode me then decode me :D -> GUEEEhkbUhA/QRYdFAAWAXAMFlMDBxcKcAUWEBgLF0Q9BFNJMw== //
// GUEEEhkbUhA/QRYdFAAWAXAMFlMDBxcKcAUWEBgLF0Q9BFNJMw== -> I want to encode me then decode me :D //
//////////////////////////////////////////////////////////////////////////////////////////////////////////
}