直接引入「commons-codec-1.10.jar」這個java包,然後調用相應方法即可
比如我們可以寫一個方法類,把常用的方法都寫進去:
publicclassEncryptionUtil{
/**
*Base64encode
**/
(Stringdata){
returnBase64.encodeBase64String(data.getBytes());
}
/**
*Base64decode
*@
**/
(Stringdata){
returnnewString(Base64.decodeBase64(data.getBytes()),"utf-8");
}
/**
*md5
**/
publicstaticStringmd5Hex(Stringdata){
returnDigestUtils.md5Hex(data);
}
/**
*sha1
**/
publicstaticStringsha1Hex(Stringdata){
returnDigestUtils.sha1Hex(data);
}
/**
*sha256
**/
publicstaticStringsha256Hex(Stringdata){
returnDigestUtils.sha256Hex(data);
}
}
(PS:純手打,望採納)
② Java MD5和SHA256等常用加密演算法
在Java項目開發中,數據安全是至關重要的。特別是在前後端介面交互時,為了保護信息的完整性和安全性,我們需要對介面簽名、用戶登錄密碼等進行加密處理。加密演算法作為基礎技術,在身份驗證、單點登錄、信息通信和支付交易等多個場景中扮演著關鍵角色。
MD5,全稱信息摘要演算法,是一種常見的128位(16位元組)散列函數。它通過復雜的演算法操作,將明文轉化為無法還原的密文,確保信息傳輸的一致性。盡管MD5常用於密碼的存儲,但需注意,由於其本質上是摘要而非加密,生成的128位字元串是單向的,無法逆向獲取原始信息。在找回密碼時,我們只能通過對比用戶輸入的MD5值來驗證,而無法獲取原密碼。
SHA系列,如SHA-1,盡管有碰撞的潛在風險,但其安全性相對較高,適用於對信息安全要求較高的場景。HMAC(Hash-based Message Authentication Code)是基於哈希函數的認證碼,推薦使用SHA256、SHA384、SHA512以及它們的HMAC變種,如HMAC-SHA256等,以提供更高級別的加密和認證功能。
對於實際應用中的對稱加密演算法,如常見的加密鹽,它可以增強密碼的安全性,防止暴力破解。至於在線加密網站,選擇適合項目的加密演算法至關重要。在眾多演算法中,SHA256、SHA384和SHA512因其較高的安全性,以及HMAC-SHA變種的認證能力,被廣泛認為是更推薦的選擇。
③ java怎麼把字元串進行md5加密
給你看源代碼,我自己寫的
public static String md5(String src){
try{
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] output = md.digest(src.getBytes());//加密處理
//將加密結果output利用Base64轉換成字元串輸出
String ret = Base64.encodeBase64String(output);
return ret;
}catch(Exception e){
throw new NoteException("密碼加密失敗",e);
}
}
public static void main(String[] args) {
System.out.println(md5("123456"));
}
④ java的md5的加密演算法代碼
import java.lang.reflect.*;
/*******************************************************************************
* keyBean 類實現了RSA Data Security, Inc.在提交給IETF 的RFC1321中的keyBean message-digest
* 演算法。
******************************************************************************/
public class keyBean {
/*
* 下面這些S11-S44實際上是一個4*4的矩陣,在原始的C實現中是用#define 實現的, 這里把它們實現成為static
* final是表示了只讀,切能在同一個進程空間內的多個 Instance間共享
*/
static final int S11 = 7;
static final int S12 = 12;
static final int S13 = 17;
static final int S14 = 22;
static final int S21 = 5;
static final int S22 = 9;
static final int S23 = 14;
static final int S24 = 20;
static final int S31 = 4;
static final int S32 = 11;
static final int S33 = 16;
static final int S34 = 23;
static final int S41 = 6;
static final int S42 = 10;
static final int S43 = 15;
static final int S44 = 21;
static final byte[] PADDING = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 };
/*
* 下面的三個成員是keyBean計算過程中用到的3個核心數據,在原始的C實現中 被定義到keyBean_CTX結構中
*/
private long[] state = new long[4]; // state (ABCD)
private long[] count = new long[2]; // number of bits, molo 2^64 (lsb
// first)
private byte[] buffer = new byte[64]; // input buffer
/*
* digestHexStr是keyBean的唯一一個公共成員,是最新一次計算結果的 16進制ASCII表示.
*/
public String digestHexStr;
/*
* digest,是最新一次計算結果的2進制內部表示,表示128bit的keyBean值.
*/
private byte[] digest = new byte[16];
/*
* getkeyBeanofStr是類keyBean最主要的公共方法,入口參數是你想要進行keyBean變換的字元串
* 返回的是變換完的結果,這個結果是從公共成員digestHexStr取得的.
*/
public String getkeyBeanofStr(String inbuf) {
keyBeanInit();
keyBeanUpdate(inbuf.getBytes(), inbuf.length());
keyBeanFinal();
digestHexStr = "";
for (int i = 0; i < 16; i++) {
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 這是keyBean這個類的標准構造函數,JavaBean要求有一個public的並且沒有參數的構造函數
public keyBean() {
keyBeanInit();
return;
}
/* keyBeanInit是一個初始化函數,初始化核心變數,裝入標準的幻數 */
private void keyBeanInit() {
count[0] = 0L;
count[1] = 0L;
// /* Load magic initialization constants.
state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L;
return;
}
/*
* F, G, H ,I 是4個基本的keyBean函數,在原始的keyBean的C實現中,由於它們是
* 簡單的位運算,可能出於效率的考慮把它們實現成了宏,在java中,我們把它們 實現成了private方法,名字保持了原來C中的。
*/
private long F(long x, long y, long z) {
return (x & y) | ((~x) & z);
}
private long G(long x, long y, long z) {
return (x & z) | (y & (~z));
}
private long H(long x, long y, long z) {
return x ^ y ^ z;
}
private long I(long x, long y, long z) {
return y ^ (x | (~z));
}
/*
* FF,GG,HH和II將調用F,G,H,I進行近一步變換 FF, GG, HH, and II transformations for
* rounds 1, 2, 3, and 4. Rotation is separate from addition to prevent
* recomputation.
*/
private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x + ac;
a = ((int) a << s) | ((int) a >>> (32 - s));
a += b;
return a;
}
/*
* keyBeanUpdate是keyBean的主計算過程,inbuf是要變換的位元組串,inputlen是長度,這個
* 函數由getkeyBeanofStr調用,調用之前需要調用keyBeaninit,因此把它設計成private的
*/
private void keyBeanUpdate(byte[] inbuf, int inputLen) {
int i, index, partLen;
byte[] block = new byte[64];
index = (int) (count[0] >>> 3) & 0x3F;
// /* Update number of bits */
if ((count[0] += (inputLen << 3)) < (inputLen << 3))
count[1]++;
count[1] += (inputLen >>> 29);
partLen = 64 - index;
// Transform as many times as possible.
if (inputLen >= partLen) {
keyBeanMemcpy(buffer, inbuf, index, 0, partLen);
keyBeanTransform(buffer);
for (i = partLen; i + 63 < inputLen; i += 64) {
keyBeanMemcpy(block, inbuf, 0, i, 64);
keyBeanTransform(block);
}
index = 0;
} else
i = 0;
// /* Buffer remaining input */
keyBeanMemcpy(buffer, inbuf, index, i, inputLen - i);
}
/*
* keyBeanFinal整理和填寫輸出結果
*/
private void keyBeanFinal() {
byte[] bits = new byte[8];
int index, padLen;
// /* Save number of bits */
Encode(bits, count, 8);
// /* Pad out to 56 mod 64.
index = (int) (count[0] >>> 3) & 0x3f;
padLen = (index < 56) ? (56 - index) : (120 - index);
keyBeanUpdate(PADDING, padLen);
// /* Append length (before padding) */
keyBeanUpdate(bits, 8);
// /* Store state in digest */
Encode(digest, state, 16);
}
/*
* keyBeanMemcpy是一個內部使用的byte數組的塊拷貝函數,從input的inpos開始把len長度的
* 位元組拷貝到output的outpos位置開始
*/
private void keyBeanMemcpy(byte[] output, byte[] input, int outpos,
int inpos, int len) {
int i;
for (i = 0; i < len; i++)
output[outpos + i] = input[inpos + i];
}
/*
* keyBeanTransform是keyBean核心變換程序,有keyBeanUpdate調用,block是分塊的原始位元組
*/
private void keyBeanTransform(byte block[]) {
long a = state[0], b = state[1], c = state[2], d = state[3];
long[] x = new long[16];
Decode(x, block, 64);
/* Round 1 */
a = FF(a, b, c, d, x[0], S11, 0xd76aa478L); /* 1 */
d = FF(d, a, b, c, x[1], S12, 0xe8c7b756L); /* 2 */
c = FF(c, d, a, b, x[2], S13, 0x242070dbL); /* 3 */
b = FF(b, c, d, a, x[3], S14, 0xc1bdceeeL); /* 4 */
a = FF(a, b, c, d, x[4], S11, 0xf57c0fafL); /* 5 */
d = FF(d, a, b, c, x[5], S12, 0x4787c62aL); /* 6 */
c = FF(c, d, a, b, x[6], S13, 0xa8304613L); /* 7 */
b = FF(b, c, d, a, x[7], S14, 0xfd469501L); /* 8 */
a = FF(a, b, c, d, x[8], S11, 0x698098d8L); /* 9 */
d = FF(d, a, b, c, x[9], S12, 0x8b44f7afL); /* 10 */
c = FF(c, d, a, b, x[10], S13, 0xffff5bb1L); /* 11 */
b = FF(b, c, d, a, x[11], S14, 0x895cd7beL); /* 12 */
a = FF(a, b, c, d, x[12], S11, 0x6b901122L); /* 13 */
d = FF(d, a, b, c, x[13], S12, 0xfd987193L); /* 14 */
c = FF(c, d, a, b, x[14], S13, 0xa679438eL); /* 15 */
b = FF(b, c, d, a, x[15], S14, 0x49b40821L); /* 16 */
/* Round 2 */
a = GG(a, b, c, d, x[1], S21, 0xf61e2562L); /* 17 */
d = GG(d, a, b, c, x[6], S22, 0xc040b340L); /* 18 */
c = GG(c, d, a, b, x[11], S23, 0x265e5a51L); /* 19 */
b = GG(b, c, d, a, x[0], S24, 0xe9b6c7aaL); /* 20 */
a = GG(a, b, c, d, x[5], S21, 0xd62f105dL); /* 21 */
d = GG(d, a, b, c, x[10], S22, 0x2441453L); /* 22 */
c = GG(c, d, a, b, x[15], S23, 0xd8a1e681L); /* 23 */
b = GG(b, c, d, a, x[4], S24, 0xe7d3fbc8L); /* 24 */
a = GG(a, b, c, d, x[9], S21, 0x21e1cde6L); /* 25 */
d = GG(d, a, b, c, x[14], S22, 0xc33707d6L); /* 26 */
c = GG(c, d, a, b, x[3], S23, 0xf4d50d87L); /* 27 */
b = GG(b, c, d, a, x[8], S24, 0x455a14edL); /* 28 */
a = GG(a, b, c, d, x[13], S21, 0xa9e3e905L); /* 29 */
d = GG(d, a, b, c, x[2], S22, 0xfcefa3f8L); /* 30 */
c = GG(c, d, a, b, x[7], S23, 0x676f02d9L); /* 31 */
b = GG(b, c, d, a, x[12], S24, 0x8d2a4c8aL); /* 32 */
/* Round 3 */
a = HH(a, b, c, d, x[5], S31, 0xfffa3942L); /* 33 */
d = HH(d, a, b, c, x[8], S32, 0x8771f681L); /* 34 */
c = HH(c, d, a, b, x[11], S33, 0x6d9d6122L); /* 35 */
b = HH(b, c, d, a, x[14], S34, 0xfde5380cL); /* 36 */
a = HH(a, b, c, d, x[1], S31, 0xa4beea44L); /* 37 */
d = HH(d, a, b, c, x[4], S32, 0x4bdecfa9L); /* 38 */
c = HH(c, d, a, b, x[7], S33, 0xf6bb4b60L); /* 39 */
b = HH(b, c, d, a, x[10], S34, 0xbebfbc70L); /* 40 */
a = HH(a, b, c, d, x[13], S31, 0x289b7ec6L); /* 41 */
d = HH(d, a, b, c, x[0], S32, 0xeaa127faL); /* 42 */
c = HH(c, d, a, b, x[3], S33, 0xd4ef3085L); /* 43 */
b = HH(b, c, d, a, x[6], S34, 0x4881d05L); /* 44 */
a = HH(a, b, c, d, x[9], S31, 0xd9d4d039L); /* 45 */
d = HH(d, a, b, c, x[12], S32, 0xe6db99e5L); /* 46 */
c = HH(c, d, a, b, x[15], S33, 0x1fa27cf8L); /* 47 */
b = HH(b, c, d, a, x[2], S34, 0xc4ac5665L); /* 48 */
/* Round 4 */
a = II(a, b, c, d, x[0], S41, 0xf4292244L); /* 49 */
d = II(d, a, b, c, x[7], S42, 0x432aff97L); /* 50 */
c = II(c, d, a, b, x[14], S43, 0xab9423a7L); /* 51 */
b = II(b, c, d, a, x[5], S44, 0xfc93a039L); /* 52 */
a = II(a, b, c, d, x[12], S41, 0x655b59c3L); /* 53 */
d = II(d, a, b, c, x[3], S42, 0x8f0ccc92L); /* 54 */
c = II(c, d, a, b, x[10], S43, 0xffeff47dL); /* 55 */
b = II(b, c, d, a, x[1], S44, 0x85845dd1L); /* 56 */
a = II(a, b, c, d, x[8], S41, 0x6fa87e4fL); /* 57 */
d = II(d, a, b, c, x[15], S42, 0xfe2ce6e0L); /* 58 */
c = II(c, d, a, b, x[6], S43, 0xa3014314L); /* 59 */
b = II(b, c, d, a, x[13], S44, 0x4e0811a1L); /* 60 */
a = II(a, b, c, d, x[4], S41, 0xf7537e82L); /* 61 */
d = II(d, a, b, c, x[11], S42, 0xbd3af235L); /* 62 */
c = II(c, d, a, b, x[2], S43, 0x2ad7d2bbL); /* 63 */
b = II(b, c, d, a, x[9], S44, 0xeb86d391L); /* 64 */
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
}
/*
* Encode把long數組按順序拆成byte數組,因為java的long類型是64bit的, 只拆低32bit,以適應原始C實現的用途
*/
private void Encode(byte[] output, long[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
output[j] = (byte) (input[i] & 0xffL);
output[j + 1] = (byte) ((input[i] >>> 8) & 0xffL);
output[j + 2] = (byte) ((input[i] >>> 16) & 0xffL);
output[j + 3] = (byte) ((input[i] >>> 24) & 0xffL);
}
}
/*
* Decode把byte數組按順序合成成long數組,因為java的long類型是64bit的,
* 只合成低32bit,高32bit清零,以適應原始C實現的用途
*/
private void Decode(long[] output, byte[] input, int len) {
int i, j;
for (i = 0, j = 0; j < len; i++, j += 4)
output[i] = b2iu(input[j]) | (b2iu(input[j + 1]) << 8)
| (b2iu(input[j + 2]) << 16) | (b2iu(input[j + 3]) << 24);
return;
}
/*
* b2iu是我寫的一個把byte按照不考慮正負號的原則的」升位」程序,因為java沒有unsigned運算
*/
public static long b2iu(byte b) {
return b < 0 ? b & 0x7F + 128 : b;
}
/*
* byteHEX(),用來把一個byte類型的數轉換成十六進制的ASCII表示,
* 因為java中的byte的toString無法實現這一點,我們又沒有C語言中的 sprintf(outbuf,"%02X",ib)
*/
public static String byteHEX(byte ib) {
char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char[] ob = new char[2];
ob[0] = Digit[(ib >>> 4) & 0X0F];
ob[1] = Digit[ib & 0X0F];
String s = new String(ob);
return s;
}
public static void main(String args[]) {
keyBean m = new keyBean();
if (Array.getLength(args) == 0) { // 如果沒有參數,執行標準的Test Suite
System.out.println("keyBean Test suite:");
System.out.println("keyBean(\"):" + m.getkeyBeanofStr(""));
System.out.println("keyBean(\"a\"):" + m.getkeyBeanofStr("a"));
System.out.println("keyBean(\"abc\"):" + m.getkeyBeanofStr("abc"));
System.out.println("keyBean(\"message digest\"):"
+ m.getkeyBeanofStr("message digest"));
System.out.println("keyBean(\"abcdefghijklmnopqrstuvwxyz\"):"
+ m.getkeyBeanofStr("abcdefghijklmnopqrstuvwxyz"));
System.out
.println("keyBean(\"\"):"
+ m
.getkeyBeanofStr(""));
} else
System.out.println("keyBean(" + args[0] + ")="
+ m.getkeyBeanofStr(args[0]));
}
}
⑤ java怎麼把字元串進行md5加密
在Java中,要將字元串進行MD5加密,可以使用Java提供的MessageDigest類。以下是一個簡單的實現示例:
首先,定義一個方法md5,輸入參數為需要加密的字元串src,返回值為加密後的字元串:
public static String md5(String src) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] output = md.digest(src.getBytes()); // 加密處理
// 將加密結果output利用Base64轉換成字元串輸出
String ret = Base64.encodeBase64String(output);
return ret;
} catch (Exception e) {
throw new NoteException("密碼加密失敗", e);
}
}
這里使用了Base64庫將加密結果轉換為字元串,以便於查看和存儲。Base64是常用的編碼方式之一,可以將二進制數據轉換為文本格式。
接下來,我們可以通過主函數main來測試這個方法:
public static void main(String[] args) {
System.out.println(md5("123456"));
}
運行這段代碼,將會輸出123456的MD5加密結果。這里需要確保你的項目中已經引入了Base64庫,以便使用Base64.encodeBase64String方法。
此外,加密後的結果長度為32個字元,對應128位的MD5哈希值。如果直接使用十六進製表示,則長度為64個字元。
需要注意的是,MD5加密演算法雖然簡單,但存在被破解的風險,建議在實際項目中使用更安全的加密演算法,如SHA-256等。
在實際應用中,你還可以對加密後的字元串進行哈希比較,以驗證用戶輸入的密碼是否正確。
總之,通過上述代碼,我們可以輕松地將字元串轉換為MD5加密後的結果,從而保護敏感信息的安全。
⑥ java現在md5加密不安全了嗎
針對md5加密是否不安全的討論,首先需要明確md5並非加密演算法,而是一種摘要演算法。它用於將任意長度的數據轉換為固定長度的輸出,常用於數據完整性校驗。然而,md5的安全性在逐漸降低,原因在於其輸出的哈希值容易被碰撞,即兩個不同的輸入可能產生相同的輸出哈希值。
md5的不安全性體現在其哈希值的碰撞風險上。理論上,由於輸入空間遠遠大於輸出空間,理論上可以找到兩個不同的輸入產生相同的md5哈希值。雖然找到這種碰撞需要大量計算,但在互聯網環境下,已有工具和演算法能夠實現這一目標,降低了md5的安全性。
例如,在身份驗證和密碼存儲場景中,使用md5加密密碼不再安全,因為攻擊者可以通過哈希碰撞找到相同的哈希值,進而嘗試破解密碼。為了增強安全性,推薦使用更強大的哈希演算法,如SHA-256。SHA-256具有更大的輸出空間和更高的安全級別,使得哈希碰撞難度大幅增加。
此外,盡管存在其他摘要演算法如SHA-1和SHA-3等,它們在安全性上優於md5。SHA-3提供了更好的安全性,其設計旨在抵抗已知的哈希碰撞攻擊策略,因此在密碼學應用中更為推薦。
總之,md5加密的不安全性體現在其較低的抵抗碰撞能力,使得它在現代應用中逐漸被更安全的哈希演算法所替代。在需要數據安全性的地方,選擇SHA-256或SHA-3等更強大的哈希演算法更為合適。
⑦ Java中如何使用MD5演算法對數據就行加密
在Java中,使用MD5演算法對字元串進行加密的代碼如下:
首先定義一個公共靜態方法:public final static String MD5(String s) {
接著獲取字元串的位元組數組:byte[] btInput = s.getBytes();
然後創建MessageDigest實例:MessageDigest mdInst = MessageDigest.getInstance("MD5");
更新位元組數組:mdInst.update(btInput);
執行摘要演算法:byte[] md = mdInst.digest();
創建字元串緩沖區:StringBuffer sb = new StringBuffer();
遍歷摘要結果:for (int i = 0; i < md.length; i++) {
將每個位元組轉換為16進制字元串,並添加到緩沖區中:int val = (md[i]) & 0xff; if (val < 16) sb.append("0"); sb.append(Integer.toHexString(val));
最後返回生成的MD5值:return sb.toString();
處理可能出現的異常:} catch (Exception e) { return null; } }
以上代碼可以對任何給定的字元串進行MD5加密。值得注意的是,MD5演算法雖然簡單且效率高,但它存在安全性不足的問題,因此在實際應用中應謹慎使用。
在進行MD5加密時,還需注意以下幾點:
1. 輸入字元串的編碼方式,應確保與加密過程中的編碼一致。
2. MD5生成的哈希值長度為128位,通常表示為32位十六進制數。
3. MD5演算法不是加密演算法,而是一個哈希演算法,不能用於解密。
4. 為了提高安全性,建議使用更高級別的哈希演算法,如SHA-256或SHA-3。
5. 在實際項目中,可以將加密邏輯封裝為一個工具類,方便復用。
6. 對於需要長期存儲的加密數據,建議使用鹽值(Salt)機制,以增加破解難度。
總之,使用MD5演算法對數據進行加密時,需充分考慮其局限性和安全性,以確保數據的安全性。