mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-12 22:56:11 +08:00
Merge pull request #1230 from hakan0arslan/master
bytesToHex function is changed
This commit is contained in:
@ -8,7 +8,6 @@ import javax.crypto.IllegalBlockSizeException;
|
|||||||
import javax.crypto.KeyGenerator;
|
import javax.crypto.KeyGenerator;
|
||||||
import javax.crypto.NoSuchPaddingException;
|
import javax.crypto.NoSuchPaddingException;
|
||||||
import javax.crypto.SecretKey;
|
import javax.crypto.SecretKey;
|
||||||
import javax.xml.bind.DatatypeConverter;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This example program shows how AES encryption and decryption can be done in
|
* This example program shows how AES encryption and decryption can be done in
|
||||||
@ -19,6 +18,8 @@ import javax.xml.bind.DatatypeConverter;
|
|||||||
*/
|
*/
|
||||||
public class AESEncryption {
|
public class AESEncryption {
|
||||||
|
|
||||||
|
|
||||||
|
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
|
||||||
/**
|
/**
|
||||||
* 1. Generate a plain text for encryption 2. Get a secret key (printed in
|
* 1. Generate a plain text for encryption 2. Get a secret key (printed in
|
||||||
* hexadecimal form). In actual use this must by encrypted and kept safe. The
|
* hexadecimal form). In actual use this must by encrypted and kept safe. The
|
||||||
@ -103,12 +104,22 @@ public class AESEncryption {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a binary byte array into readable hex form
|
* Convert a binary byte array into readable hex form
|
||||||
*
|
* Old library is deprecated on OpenJdk 11 and
|
||||||
|
* this is faster regarding other solution is using StringBuilder
|
||||||
|
* Credit
|
||||||
|
* {@link
|
||||||
|
* https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java/9855338#9855338}
|
||||||
* @param hash
|
* @param hash
|
||||||
* (in binary)
|
* (in binary)
|
||||||
* @return hexHash
|
* @return hexHash
|
||||||
*/
|
*/
|
||||||
private static String bytesToHex(byte[] hash) {
|
public static String bytesToHex(byte[] bytes) {
|
||||||
return DatatypeConverter.printHexBinary(hash);
|
char[] hexChars = new char[bytes.length * 2];
|
||||||
|
for (int j = 0; j < bytes.length; j++) {
|
||||||
|
int v = bytes[j] & 0xFF;
|
||||||
|
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
|
||||||
|
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
|
||||||
|
}
|
||||||
|
return new String(hexChars);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user