Getting the original secret key from hexadecimal format

This is the hexadecimal format of the private key used to encrypt AES

00010203050607080A0B0C0D0F101112

Can I generate the original SecretKey format or an array of bytes from this?

If so, how?

+3
source share
3 answers

You can use Apache Commons Codec to perform hex decoding,

http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html#decodeHex (char [])

If you do not want to use any libraries, you can also do this,

byte[] bytes = new BigInteger("7F" + str, 16).toByteArray();
SecretKeySpec key = new SecretKeySpec(bytes, 1, bytes.length-1, "AES");

You need to add an extra byte of 0x7F to prevent BigInteger from deleting leading zeros or adding signed bytes.

+2
0

What is the original SecretKey format?
Do you know what byte[]contains signed bytes from -128 to 127? It would hurt if you tried this:

byte[] key = {
  0x00,0x01,0x02,0x03,0x05,0x06,0x07,0x08,
  0x0A,0x0B,0x0C,0x0D,0x0F,0x10,0x11,0x12
};

Note. If you have values ​​such as 0x80 - 0xFF, you need to specify them as (bytes) 0x80 - (bytes) 0xFF to avoid a range warning.

0
source

Source: https://habr.com/ru/post/1724027/


All Articles