Skip to content

Commit 0bf21d6

Browse files
committed
bytesToHex function is changed beecause of required library is depracated on OpenJdk 11
1 parent fcac632 commit 0bf21d6

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

ciphers/AESEncryption.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import javax.crypto.KeyGenerator;
99
import javax.crypto.NoSuchPaddingException;
1010
import javax.crypto.SecretKey;
11-
import javax.xml.bind.DatatypeConverter;
1211

1312
/**
1413
* This example program shows how AES encryption and decryption can be done in
@@ -19,6 +18,8 @@
1918
*/
2019
public class AESEncryption {
2120

21+
22+
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
2223
/**
2324
* 1. Generate a plain text for encryption 2. Get a secret key (printed in
2425
* hexadecimal form). In actual use this must by encrypted and kept safe. The
@@ -103,12 +104,22 @@ public static String decryptText(byte[] byteCipherText, SecretKey secKey) throws
103104

104105
/**
105106
* Convert a binary byte array into readable hex form
106-
*
107+
* Old library is deprecated on OpenJdk 11 and
108+
* this is faster regarding other solution is using StringBuilder
109+
* Credit
110+
* {@link
111+
* https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java/9855338#9855338}
107112
* @param hash
108113
* (in binary)
109114
* @return hexHash
110115
*/
111-
private static String bytesToHex(byte[] hash) {
112-
return DatatypeConverter.printHexBinary(hash);
116+
public static String bytesToHex(byte[] bytes) {
117+
char[] hexChars = new char[bytes.length * 2];
118+
for (int j = 0; j < bytes.length; j++) {
119+
int v = bytes[j] & 0xFF;
120+
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
121+
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
122+
}
123+
return new String(hexChars);
113124
}
114125
}

0 commit comments

Comments
 (0)