-
Notifications
You must be signed in to change notification settings - Fork 30
/
KhazadMethod.java
124 lines (109 loc) · 2.91 KB
/
KhazadMethod.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package cryptography.ciphers.khazad;
import java.security.SecureRandom;
import org.apache.commons.codec.binary.Base64;
import cryptography.Mode;
public class KhazadMethod {
/**
* Khazad method for encryption and decryption
*
* @param inputText string
* @param keyBytes key from GetNewKey method
* @param mode encrypt/decrypt
* @return string
*/
public static String Khazad(final String inputText, final byte[] keyBytes, final Mode mode) {
try {
Base64 base64 = new Base64();
Khazad khazad = new Khazad();
khazad.keySetup(keyBytes);
if (mode == Mode.ENCRYPT) {
byte[] cipherBytes = applyPadding(inputText.getBytes());
khazad.encrypt(cipherBytes);
return base64.encodeToString(cipherBytes);
}
if (mode == Mode.DECRYPT) {
byte[] cipherBytes = base64.decode(inputText.getBytes());
khazad.decrypt(cipherBytes);
return new String(removePadding(cipherBytes));
}
return null;
} catch (Exception e) {
return e.toString();
}
}
/**
* Returns new key of 128 bits
*
* @return
*/
public static byte[] GetRandomKeyBytes() {
SecureRandom random = new SecureRandom();
byte[] bytes = new byte[16];
random.nextBytes(bytes);
return bytes;
}
/**
* Return key bytes as base64 string
*
* @param keyBytes key
* @return base64 string
*/
public static String KeyBytesToBase64String(byte[] keyBytes) {
Base64 base64 = new Base64();
return base64.encodeToString(keyBytes);
}
/**
* Return base64 key as byte array
* @param base64Key b64 key string
* @return byte array
*/
public static byte[] KeyBase64StringToBytes(final String base64Key) {
Base64 base64 = new Base64();
return base64.decode(base64Key.getBytes());
}
/**
* Applies padding on too short input
*
* @param input byte array input
* @return either input or padded input
*/
private static byte[] applyPadding(byte[] input) {
final int l = input.length;
if (l < 8) {
int n = 8 - l;
byte[] padding = new byte[n];
return concatenateByteArrays(input, padding);
}
return input;
}
/**
* Remove any applied padding
*
* @param decyphered byte array
* @return either input or stripped input
*/
private static byte[] removePadding(byte[] cipherText) {
int noPadCipherLength = cipherText.length;
for(int a=cipherText.length-1;cipherText[a]==0;a--) {
if(cipherText[a]==0) {
noPadCipherLength--;
}
}
byte[] noPadCipher = new byte[noPadCipherLength];
System.arraycopy(cipherText, 0, noPadCipher, 0, noPadCipherLength);
return noPadCipher;
}
/**
* Connects two byte arrays together
*
* @param a first byte array
* @param b second byte array
* @return concatenated byte array
*/
private static byte[] concatenateByteArrays(byte[] a, byte[] b) {
byte[] result = new byte[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
}