从十六进制格式获取原始密钥 [英] Getting the original secretkey from hex format
本文介绍了从十六进制格式获取原始密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是用于AES加密的秘密密钥的十六进制格式
This is the hex format of a secret key used for AES encryption
00010203050607080A0B0C0D0F101112
我可以由此生成原始的SecretKey格式或字节数组吗?
Can i generate the original SecretKey format or byte array from this?
如果可以,怎么办?
推荐答案
您可以使用Apache Commons Codec进行十六进制解码,
You can use Apache Commons Codec to perform the hex decoding,
http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html#decodeHex(char [])
如果您不想使用任何库,也可以这样做,
If you don't want 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");
您必须添加一个额外的字节0x7F,以防止BigInteger剥离前导零或添加带符号的字节.
You have to add an extra byte 0x7F to prevent BigInteger from stripping leading zeros or adding signed bytes.
这篇关于从十六进制格式获取原始密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文