課程
/后端開發
/Java
/JAVA實現對稱加密
如何使用java將你的名字用一個加密方式后輸出
2017-03-14
源自:JAVA實現對稱加密 1-1
正在回答
public class KkltHmac { ? ?public static final String src = "hmac test"; ? ?public static void main(String[] args) { ? ? ? ?jdkHmacMD5(); ? ? ? ?bcHmacMD5(); ? ?} ? ?public static void jdkHmacMD5(){ ? ? ? ?KeyGenerator hmacMD5 = null; ? ? ? ?try ? ? ? ?{ ? ? ? ? ? ?// 初始化KeyGenerator ? ? ? ? ? ?KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5"); ? ? ? ? ? ?// 產生密鑰 ? ? ? ? ? ?SecretKey secretKey = keyGenerator.generateKey(); ? ? ? ? ? ?// 獲取密鑰// ? ? ? byte[] key = secretKey.getEncoded(); ? ? ? ? ? ?byte[] key = Hex.decodeHex(new char[]{'1','2','3','4','5','6','7','8','9','a','b','c','d','e' }); ? ? ? ? ? ?// 還原密鑰 ? ? ? ? ? ?SecretKey restoreSecretKey = new SecretKeySpec(key, "HmacMD5"); ? ? ? ? ? ?// 實例化MAC ? ? ? ? ? ?Mac mac = Mac.getInstance(restoreSecretKey.getAlgorithm()); ? ? ? ? ? ?// 初始化MAC ? ? ? ? ? ?mac.init(restoreSecretKey); ? ? ? ? ? ?// 執行摘要 ? ? ? ? ? ?byte[] hmacMD5Bytes = mac.doFinal(src.getBytes()); ? ? ? ? ? ?System.out.println("jdk hmacMD5:" + Hex.encodeHexString(hmacMD5Bytes)); ? ? ? ?} catch (Exception e) { ? ? ? ? ? ?e.printStackTrace(); ? ? ? ?} ? ?} ? ?// 用bouncy castle實現: ? ?public static void bcHmacMD5() ? ?{ ? ? ? ?HMac hmac = new HMac(new MD5Digest()); ? ? ? ?// 必須是16進制的字符,長度必須是2的倍數 ? ? ? ?hmac.init(new KeyParameter(org.bouncycastle.util.encoders.Hex.decode("123456789abcde"))); ? ? ? ?hmac.update(src.getBytes(), 0, src.getBytes().length); ? ? ? ?// 執行摘要 ? ? ? ?byte[] hmacMD5Bytes = new byte[hmac.getMacSize()]; ? ? ? ?hmac.doFinal(hmacMD5Bytes, 0); ? ? ? ?System.out.println("bc hmacMD5:" + org.bouncycastle.util.encoders.Hex.toHexString(hmacMD5Bytes)); ? ?}}
把src改成你的名字即可
舉報
為你帶來軟硬件通用的對稱加密算法,以及他們的應用范圍
1 回答加解密后的數據應該如何傳輸?
1 回答如何通過JAVA來調用硬件加密設備(例如密碼卡這樣的設備)
3 回答可以使用aes給zip文件加密嗎?
1 回答請問老師aes加密時候用的password是密鑰嗎?為什么用KeyGenerator又生成了一個key
1 回答加密后的byte[]編碼
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2017-03-18
public class KkltHmac {
? ?public static final String src = "hmac test";
? ?public static void main(String[] args) {
? ? ? ?jdkHmacMD5();
? ? ? ?bcHmacMD5();
? ?}
? ?public static void jdkHmacMD5(){
? ? ? ?KeyGenerator hmacMD5 = null;
? ? ? ?try
? ? ? ?{
? ? ? ? ? ?// 初始化KeyGenerator
? ? ? ? ? ?KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5");
? ? ? ? ? ?// 產生密鑰
? ? ? ? ? ?SecretKey secretKey = keyGenerator.generateKey();
? ? ? ? ? ?// 獲取密鑰
// ? ? ? byte[] key = secretKey.getEncoded();
? ? ? ? ? ?byte[] key = Hex.decodeHex(new char[]{'1','2','3','4','5','6','7','8','9','a','b','c','d','e' });
? ? ? ? ? ?// 還原密鑰
? ? ? ? ? ?SecretKey restoreSecretKey = new SecretKeySpec(key, "HmacMD5");
? ? ? ? ? ?// 實例化MAC
? ? ? ? ? ?Mac mac = Mac.getInstance(restoreSecretKey.getAlgorithm());
? ? ? ? ? ?// 初始化MAC
? ? ? ? ? ?mac.init(restoreSecretKey);
? ? ? ? ? ?// 執行摘要
? ? ? ? ? ?byte[] hmacMD5Bytes = mac.doFinal(src.getBytes());
? ? ? ? ? ?System.out.println("jdk hmacMD5:" + Hex.encodeHexString(hmacMD5Bytes));
? ? ? ?} catch (Exception e) {
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ?}
? ?// 用bouncy castle實現:
? ?public static void bcHmacMD5()
? ?{
? ? ? ?HMac hmac = new HMac(new MD5Digest());
? ? ? ?// 必須是16進制的字符,長度必須是2的倍數
? ? ? ?hmac.init(new KeyParameter(org.bouncycastle.util.encoders.Hex.decode("123456789abcde")));
? ? ? ?hmac.update(src.getBytes(), 0, src.getBytes().length);
? ? ? ?// 執行摘要
? ? ? ?byte[] hmacMD5Bytes = new byte[hmac.getMacSize()];
? ? ? ?hmac.doFinal(hmacMD5Bytes, 0);
? ? ? ?System.out.println("bc hmacMD5:" + org.bouncycastle.util.encoders.Hex.toHexString(hmacMD5Bytes));
? ?}
}
把src改成你的名字即可