亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

如何使用java將你的名字用一個加密方式后輸出

如何使用java將你的名字用一個加密方式后輸出

正在回答

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改成你的名字即可


0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

如何使用java將你的名字用一個加密方式后輸出

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號