- 1、java項目如何加密?
- 2、如何生成HMAC在Java中相當於一個Python的例子嗎
- 3、怎麼用java實現HMAC-SHA1
- 4、java加密的幾種方式
- 5、php hash_hmac跟java算出來的結果不一樣
Java基本的單向加密演算法:
1.BASE64 嚴格地說,屬於編碼格式,而非加密演算法
2.MD5(Message Digest algorithm 5,信息摘要演算法)
3.SHA(Secure Hash Algorithm,安全散列演算法)
4.HMAC(Hash Message Authentication Code,散列消息鑒別碼)
按 照RFC2045的定義,Base64被定義為:Base64內容傳送編碼被設計用來把任意序列的8位位元組描述為一種不易被人直接識別的形式。(The Base64 Content-Transfer-Encoding is designed to represent arbitrary sequences of octets in a form that need not be humanly readable.)
常見於郵件、http加密,截取http信息,你就會發現登錄操作的用戶名、密碼欄位通過BASE64加密的。
主要就是BASE64Encoder、BASE64Decoder兩個類,我們只需要知道使用對應的方法即可。另,BASE加密後產生的位元組位數是8的倍數,如果不夠位數以=符號填充。
MD5
MD5 — message-digest algorithm 5 (信息-摘要演算法)縮寫,廣泛用於加密和解密技術,常用於文件校驗。校驗?不管文件多大,經過MD5後都能生成唯一的MD5值。好比現在的ISO校驗,都 是MD5校驗。怎麼用?當然是把ISO經過MD5後產生MD5的值。一般下載linux-ISO的朋友都見過下載鏈接旁邊放著MD5的串。就是用來驗證文 件是否一致的。
HMAC
HMAC(Hash Message Authentication Code,散列消息鑒別碼,基於密鑰的Hash演算法的認證協議。消息鑒別碼實現鑒別的原理是,用公開函數和密鑰產生一個固定長度的值作為認證標識,用這個 標識鑒別消息的完整性。使用一個密鑰生成一個固定大小的小數據塊,即MAC,並將其加入到消息中,然後傳輸。接收方利用與發送方共享的密鑰進行鑒別認證 等。
1. HMACSHA1似乎是你所需要的演算法:SecretKeySpec keySpec = new SecretKeySpec(
“qnscAdgRlkIhAUPY44oiexBKtQbGY0orf7OV1I50”.getBytes(),
“HmacSHA1”);
Mac mac = Mac.getInstance(“HmacSHA1”);
mac.init(keySpec);
byte[] result = mac.doFinal(“foo”.getBytes());
BASE64Encoder encoder = new BASE64Encoder();
System.out.println(encoder.encode(result));
生產:+3h2gpjf4xcynjCGU5lbdMBwGOc=
請注意,我sun.misc.BASE64Encoder為迅速在這裡,但你應該不依賴於太陽的JRE。以base64編碼器在下議院編解碼器將是一個更好的選擇,例如。
2. A小調的事情,但如果你正在尋找一個相當於HMAC(那麼默認的Python庫的MD5演算法,所以你需要的HMACMD5演算法在Java中。 這個我有這個確切的問題,並認為此答案這是有幫助的 CodeGo.net,但我錯過了一個地方傳遞到HMAC()的一部分,並就下一個兔子洞。希望這個答案可以防止其他人做的未來。 例如在Python REPL import hmac
hmac.new(“keyValueGoesHere”, “secretMessageToHash”).hexdigest()
‘1a7bb3687962c9e26b2d4c2b833b2bf2’
這等效於import org.apache.commons.codec.binary.Hex;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class HashingUtility {
public static String HMAC_MD5_encode(String key, String message) throws Exception {
SecretKeySpec keySpec = new SecretKeySpec(
key.getBytes(),
“HmacMD5”);
Mac mac = Mac.getInstance(“HmacMD5”);
mac.init(keySpec);
byte[] rawHmac = mac.doFinal(message.getBytes());
return Hex.encodeHexString(rawHmac);
}
}
請注意,在我的例子我在幹什麼。hexdigest相當於()
以下代碼為JAVABEAN,加密用
[PHP]
package test;
/*
* Copyright 1997-2001 by Sun Microsystems, Inc.,
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information
* of Sun Microsystems, Inc. (“Confidential Information”. You
* shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement
* you entered into with Sun.
*/
import java.security.*;
import javax.crypto.*;
/*
* This program demonstrates how to generate a secret-key object for
* HMAC-SHA1, and initialize an HMAC-SHA1 object with it.
*/
public class initMac {
public initMac() {
}
//定義加密演算法
String Algorithm = “HmacSHA1”;
public byte[] HmacSHA1(String post) throws Exception {
// Generate secret key for HMAC-SHA1
KeyGenerator kg = KeyGenerator.getInstance(Algorithm);
SecretKey sk = kg.generateKey();
// Get instance of Mac object implementing HMAC-SHA1, and
// initialize it with the above secret key
Mac mac = Mac.getInstance(Algorithm);
mac.init(sk);
byte[] result = mac.doFinal(post.getBytes());
return result;
}
}
[/PHP]
以下代碼為調用上面JAVABEAN的部分
[PHP]package test;
import test.initMac;
/**
* pTitle: /p
*
* pDescription: /p
基本的單向加密演算法:
BASE64 嚴格地說,屬於編碼格式,而非加密演算法
MD5(Message Digest algorithm 5,信息摘要演算法)
SHA(Secure Hash Algorithm,安全散列演算法)
HMAC(Hash Message Authentication Code,散列消息鑒別碼)
複雜的對稱加密(DES、PBE)、非對稱加密演算法:
DES(Data Encryption Standard,數據加密演算法)
PBE(Password-based encryption,基於密碼驗證)
RSA(演算法的名字以發明者的名字命名:Ron Rivest, AdiShamir 和Leonard Adleman)
DH(Diffie-Hellman演算法,密鑰一致協議)
DSA(Digital Signature Algorithm,數字簽名)
ECC(Elliptic Curves Cryptography,橢圓曲線密碼編碼學)
代碼參考:
/**
* BASE64加密
*
* @param key
* @return
* @throws Exception
*/
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* MD5加密
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptMD5(byte[] data) throws Exception {
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
return md5.digest();
}
/**
* SHA加密
*
* @param data
* @return
* @throws Exception
*/
public static byte[] encryptSHA(byte[] data) throws Exception {
MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
sha.update(data);
return sha.digest();
}
}
/**
* 初始化HMAC密鑰
*
* @return
* @throws Exception
*/
public static String initMacKey() throws Exception {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return encryptBASE64(secretKey.getEncoded());
}
/**
* HMAC加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
SecretKey secretKey = new SecretKeySpec(decryptBASE64(key), KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(data);
}
問題解決代碼如下:
public String md5(String txt) {
try{
MessageDigest md = MessageDigest.getInstance(“MD5”);
md.update(txt.getBytes(“GBK”)); //問題主要出在這裡,Java的字元串是unicode編碼,不受源碼文件的編碼影響;而PHP的編碼是和源碼文件的編碼一致,受源碼編碼影響。
StringBuffer buf=new StringBuffer();
for(byte b:md.digest()){
buf.append(String.format(“%02x”, b0xff));
}
return buf.toString();
}catch( Exception e ){
e.printStackTrace();
return null;
}
}
原創文章,作者:CITGM,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/126405.html