// 生成加密key:64个字节
KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5");
SecretKey secretKey = keyGen.generateKey();
// 查看key
byte[] encoded = secretKey.getEncoded();
BigInteger bigInteger = new BigInteger(1, encoded);
String s = bigInteger.toString(16);
//加密:你好hmac
Mac hmacMD5 = Mac.getInstance("hmacMD5");
hmacMD5.init(secretKey);
hmacMD5.update("bob".getBytes(StandardCharsets.UTF_8));
byte[] bytes = hmacMD5.doFinal();
String s1 = new BigInteger(1, bytes).toString(16);
System.out.println(s1);
// 根据64个字节数组恢复key
SecretKeySpec mykey = new SecretKeySpec(encoded, "HmacMD5");
Mac hmacMD52 = Mac.getInstance("hmacMD5");
hmacMD52.init(mykey);
hmacMD52.update("bob".getBytes(StandardCharsets.UTF_8));
byte[] bytes2 = hmacMD5.doFinal();
String s12 = new BigInteger(1, bytes2).toString(16);
System.out.println(s12);
网友评论