導航:首頁 > 編程語言 > javamd5密碼加密

javamd5密碼加密

發布時間:2023-03-06 20:12:53

A. java MD5加密問題

jdk本身有md5的加密方法,也可以用appache下面的jar包。加密後保存在資料庫,平時在調用出對比,是否正確!

B. java 中如何進行md5加密

JDK裡面有一個java.security.MessageDigest類,這個類就是用來加密的。

加密代碼如下:

Stringtoken=System.currentTimeMillis()+newRandom().nextInt()+"";
try{
MessageDigestmd=MessageDigest.getInstance("MD5");
byte[]md5=md.digest(token.getBytes());
}catch(Exceptione){
thrownewRuntimeException(e);
}

這個byte類型的數組就是使用MD5加密後的結果

C. 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"));
}

D. java 中怎麼使用md5加密

樓主您好
JDK裡面有一個java.security.MessageDigest類,這個類就是用來加密的。
String token = System.currentTimeMillis()+new Random().nextInt()+"";<img id="selectsearch-icon" src="https://gss0.bdstatic.com/70cFsjip0QIZ8tyhnq/img/iknow/qb/select-search.png" alt="搜索" class="selectsearch-hide">
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] md5 = md.digest(token.getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}

E. JAVA中給賬號密碼用MD5加密

import java.security.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class md5{
public String md5(String str) {
String s=str;
if(s==null){
return "";
}else{
String value = null;
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
}catch (NoSuchAlgorithmException ex) {
Logger.getLogger(md5.class.getName()).log(Level.SEVERE, null, ex);
}
sun.misc.BASE64Encoder baseEncoder = new sun.misc.BASE64Encoder();
try {
value = baseEncoder.encode(md5.digest(s.getBytes("utf-8")));
} catch (Exception ex) {
}
return value;
}
}
}

F. java web md5加密的使用

1、Java中你可以用MD5 util工具類,網上有md5工具類的,你下載一個,在數據入庫時候,進行密文md5一下在存入資料庫就行

2、非重要性數據使用md5是沒有意義的

3、以上個人觀點,如果還有什麼不懂的可以在繼續追問

G. java中md5加密

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class md5 {
public String str;

public void md5s(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();

int i;

StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
str = buf.toString();
System.out.println("result: " + buf.toString());// 32位的加密
System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}

public static void main(String agrs[]) {
md5 md51 = new md5();
md51.md5s("4");//加密4
}

}

H. java用md5密碼加密有必要嗎

有必要的,md5就是為了防止人偷窺,而當密碼很短的時候,利用暴力搜索也比較容易搜索到,只有密碼強度足夠的情況下才有意義,你想轉換也就沒那麼容易了。md5類hash演算法的設計初衷就是單向,即不可逆。

I. 可變MD5加密(Java實現)

可變在這里含義很簡單 就是最終的加密結果是可變的 而非必需按標准MD 加密實現 Java類庫security中的MessageDigest類就提供了MD 加密的支持 實現起來非常方便 為了實現更多效果 我們可以如下設計MD 工具類

Java代碼

package ** ** util;

import java security MessageDigest;

/**

* 標准MD 加密方法 使用java類庫的security包的MessageDigest類處理

* @author Sarin

*/

public class MD {

/**

* 獲得MD 加密密碼的方法

*/

public static String getMD ofStr(String origString) {

String origMD = null;

try {

MessageDigest md = MessageDigest getInstance( MD );

byte[] result = md digest(origString getBytes());

origMD = byteArray HexStr(result);

} catch (Exception e) {

e printStackTrace();

}

return origMD ;

}

/**

* 處理位元組數組得到MD 密碼的方法

*/

private static String byteArray HexStr(byte[] bs) {

StringBuffer *** = new StringBuffer();

for (byte b : bs) {

*** append(byte HexStr(b));

}

return *** toString();

}

/**

* 位元組標准移位轉十六進制方法

*/

private static String byte HexStr(byte b) {

String hexStr = null;

int n = b;

if (n < ) {

//若需要自定義加密 請修改這個移位演算法即可

n = b & x F + ;

}

hexStr = Integer toHexString(n / ) + Integer toHexString(n % );

return hexStr toUpperCase();

}

/**

* 提供一個MD 多次加密方法

*/

public static String getMD ofStr(String origString int times) {

String md = getMD ofStr(origString);

for (int i = ; i < times ; i++) {

md = getMD ofStr(md );

}

return getMD ofStr(md );

}

/**

* 密碼驗證方法

*/

public static boolean verifyPassword(String inputStr String MD Code) {

return getMD ofStr(inputStr) equals(MD Code);

}

/**

* 重載一個多次加密時的密碼驗證方法

*/

public static boolean verifyPassword(String inputStr String MD Code int times) {

return getMD ofStr(inputStr times) equals(MD Code);

}

/**

* 提供一個測試的主函數

*/

public static void main(String[] args) {

System out println( : + getMD ofStr( ));

System out println( : + getMD ofStr( ));

System out println( sarin: + getMD ofStr( sarin ));

System out println( : + getMD ofStr( ));

}

}

可以看出實現的過程非常簡單 因為由java類庫提供了處理支持 但是要清楚的是這種方式產生的密碼不是標準的MD 碼 它需要進行移位處理才能得到標准MD 碼 這個程序的關鍵之處也在這了 怎麼可變?調整移位演算法不就可變了么!不進行移位 也能夠得到 位的密碼 這就不是標准加密了 只要加密和驗證過程使用相同的演算法就可以了

MD 加密還是很安全的 像CMD 那些窮舉破解的只是針對標准MD 加密的結果進行的 如果自定義移位演算法後 它還有效么?可以說是無解的了 所以MD 非常安全可靠

為了更可變 還提供了多次加密的方法 可以在MD 基礎之上繼續MD 就是對 位的第一次加密結果再MD 恩 這樣去破解?沒有任何意義

這樣在MIS系統中使用 安全可靠 歡迎交流 希望對使用者有用

我們最後看看由MD 加密演算法實現的類 那是非常龐大的

Java代碼

import java lang reflect *;

/**

* **********************************************

* md 類實現了RSA Data Security Inc 在提交給IETF

* 的RFC 中的MD message digest 演算法

* ***********************************************

*/

public class MD {

/* 下面這些S S 實際上是一個 * 的矩陣 在原始的C實現中是用#define 實現的

這里把它們實現成為static final是表示了只讀 切能在同一個進程空間內的多個

Instance間共享*/

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final int S = ;

static final byte[] PADDING = {

};

/* 下面的三個成員是MD 計算過程中用到的 個核心數據 在原始的C實現中

被定義到MD _CTX結構中

*/

private long[] state = new long[ ]; // state (ABCD)

private long[] count = new long[ ]; // number of bits molo ^ (l *** first)

private byte[] buffer = new byte[ ]; // input buffer

/* digestHexStr是MD 的唯一一個公共成員 是最新一次計算結果的

進制ASCII表示

*/

public String digestHexStr;

/* digest 是最新一次計算結果的 進制內部表示 表示 bit的MD 值

*/

private byte[] digest = new byte[ ];

/*

getMD ofStr是類MD 最主要的公共方法 入口參數是你想要進行MD 變換的字元串

返回的是變換完的結果 這個結果是從公共成員digestHexStr取得的.

*/

public String getMD ofStr(String inbuf) {

md Init();

md Update(inbuf getBytes() inbuf length());

md Final();

digestHexStr = ;

for (int i = ; i < ; i++) {

digestHexStr += byteHEX(digest[i]);

}

return digestHexStr;

}

// 這是MD 這個類的標准構造函數 JavaBean要求有一個public的並且沒有參數的構造函數

public MD () {

md Init();

return;

}

/* md Init是一個初始化函數 初始化核心變數 裝入標準的幻數 */

private void md Init() {

count[ ] = L;

count[ ] = L;

///* Load magic initialization constants

state[ ] = x L;

state[ ] = xefcdab L;

state[ ] = x badcfeL;

state[ ] = x L;

return;

}

/* F G H I 是 個基本的MD 函數 在原始的MD 的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 and

Rotation is separate from addition to prevent reputation

*/

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 >>> ( 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 >>> ( 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 >>> ( 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 >>> ( s));

a += b;

return a;

}

/*

md Update是MD 的主計算過程 inbuf是要變換的位元組串 inputlen是長度 這個

函數由getMD ofStr調用 調用之前需要調用md init 因此把它設計成private的

*/

private void md Update(byte[] inbuf int inputLen) {

int i index partLen;

byte[] block = new byte[ ];

index = (int) (count[ ] >>> ) & x F;

// /* Update number of bits */

if ((count[ ] += (inputLen << )) < (inputLen << ))

count[ ]++;

count[ ] += (inputLen >>> );

partLen = index;

// Transform as many times as possible

if (inputLen >= partLen) {

md Memcpy(buffer inbuf index partLen);

md Transform(buffer);

for (i = partLen; i + < inputLen; i += ) {

md Memcpy(block inbuf i );

md Transform(block);

}

index = ;

} else

i = ;

///* Buffer remaining input */

md Memcpy(buffer inbuf index i inputLen i);

}

/*

md Final整理和填寫輸出結果

*/

private void md Final() {

byte[] bits = new byte[ ];

int index padLen;

///* Save number of bits */

Encode(bits count );

///* Pad out to mod

index = (int) (count[ ] >>> ) & x f;

padLen = (index < ) ? ( index) : ( index);

md Update(PADDING padLen);

///* Append length (before padding) */

md Update(bits );

///* Store state in digest */

Encode(digest state );

}

/* md Memcpy是一個內部使用的byte數組的塊拷貝函數 從input的inpos開始把len長度的

位元組拷貝到output的outpos位置開始

*/

private void md Memcpy(byte[] output byte[] input int outpos int inpos int len) {

int i;

for (i = ; i < len; i++)

output[outpos + i] = input[inpos + i];

}

/*

md Transform是MD 核心變換程序 有md Update調用 block是分塊的原始位元組

*/

private void md Transform(byte block[]) {

long a = state[ ] b = state[ ] c = state[ ] d = state[ ];

long[] x = new long[ ];

Decode(x block );

/* Round */

a = FF(a b c d x[ ] S xd aa L); /* */

d = FF(d a b c x[ ] S xe c b L); /* */

c = FF(c d a b x[ ] S x dbL); /* */

b = FF(b c d a x[ ] S xc bdceeeL); /* */

a = FF(a b c d x[ ] S xf c fafL); /* */

d = FF(d a b c x[ ] S x c aL); /* */

c = FF(c d a b x[ ] S xa L); /* */

b = FF(b c d a x[ ] S xfd L); /* */

a = FF(a b c d x[ ] S x d L); /* */

d = FF(d a b c x[ ] S x b f afL); /* */

c = FF(c d a b x[ ] S xffff bb L); /* */

b = FF(b c d a x[ ] S x cd beL); /* */

a = FF(a b c d x[ ] S x b L); /* */

d = FF(d a b c x[ ] S xfd L); /* */

c = FF(c d a b x[ ] S xa eL); /* */

b = FF(b c d a x[ ] S x b L); /* */

/* Round */

a = GG(a b c d x[ ] S xf e L); /* */

d = GG(d a b c x[ ] S xc b L); /* */

c = GG(c d a b x[ ] S x e a L); /* */

b = GG(b c d a x[ ] S xe b c aaL); /* */

a = GG(a b c d x[ ] S xd f dL); /* */

d = GG(d a b c x[ ] S x L); /* */

c = GG(c d a b x[ ] S xd a e L); /* */

b = GG(b c d a x[ ] S xe d fbc L); /* */

a = GG(a b c d x[ ] S x e cde L); /* */

d = GG(d a b c x[ ] S xc d L); /* */

c = GG(c d a b x[ ] S xf d d L); /* */

b = GG(b c d a x[ ] S x a edL); /* */

a = GG(a b c d x[ ] S xa e e L); /* */

d = GG(d a b c x[ ] S xfcefa f L); /* */

c = GG(c d a b x[ ] S x f d L); /* */

b = GG(b c d a x[ ] S x d a c aL); /* */

/* Round */

a = HH(a b c d x[ ] S xfffa L); /* */

d = HH(d a b c x[ ] S x f L); /* */

c = HH(c d a b x[ ] S x d d L); /* */

b = HH(b c d a x[ ] S xfde cL); /* */

a = HH(a b c d x[ ] S xa beea L); /* */

d = HH(d a b c x[ ] S x bdecfa L); /* */

c = HH(c d a b x[ ] S xf bb b L); /* */

b = HH(b c d a x[ ] S xbebfbc L); /* */

a = HH(a b c d x[ ] S x b ec L); /* */

d = HH(d a b c x[ ] S xeaa faL); /* */

c = HH(c d a b x[ ] S xd ef L); /* */

b = HH(b c d a x[ ] S x d L); /* */

a = HH(a b c d x[ ] S xd d d L); /* */

d = HH(d a b c x[ ] S xe db e L); /* */

c = HH(c d a b x[ ] S x fa cf L); /* */

b = HH(b c d a x[ ] S xc ac L); /* */

/* Round */

a = II(a b c d x[ ] S xf L); /* */

d = II(d a b c x[ ] S x aff L); /* */

c = II(c d a b x[ ] S xab a L); /* */

b = II(b c d a x[ ] S xfc a L); /* */

a = II(a b c d x[ ] S x b c L); /* */

d = II(d a b c x[ ] S x f ccc L); /* */

c = II(c d a b x[ ] S xffeff dL); /* */

b = II(b c d a x[ ] S x dd L); /* */

a = II(a b c d x[ ] S x fa e fL); /* */

d = II(d a b c x[ ] S xfe ce e L); /* */

c = II(c d a b x[ ] S xa L); /* */

b = II(b c d a x[ ] S x e a L); /* */

a = II(a b c d x[ ] S xf e L); /* */

d = II(d a b c x[ ] S xbd af L); /* */

c = II(c d a b x[ ] S x ad d bbL); /* */

b = II(b c d a x[ ] S xeb d L); /* */

state[ ] += a;

state[ ] += b;

state[ ] += c;

state[ ] += d;

}

/*Encode把long數組按順序拆成byte數組 因為java的long類型是 bit的

只拆低 bit 以適應原始C實現的用途

*/

private void Encode(byte[] output long[] input int len) {

int i j;

for (i = j = ; j < len; i++ j += ) {

output[j] = (byte) (input[i] & xffL);

output[j + ] = (byte) ((input[i] >>> ) & xffL);

output[j + ] = (byte) ((input[i] >>> ) & xffL);

output[j + ] = (byte) ((input[i] >>> ) & xffL);

}

}

/*Decode把byte數組按順序合成成long數組 因為java的long類型是 bit的

只合成低 bit 高 bit清零 以適應原始C實現的用途

*/

private void Decode(long[] output byte[] input int len) {

int i j;

for (i = j = ; j < len; i++ j += )

output[i] = b iu(input[j]) | (b iu(input[j + ]) << ) | (b iu(input[j + ]) << )

| (b iu(input[j + ]) << );

return;

}

/*

b iu是我寫的一個把byte按照不考慮正負號的原則的"升位"程序 因為java沒有unsigned運算

*/

public static long b iu(byte b) {

return b < ? b & x F + : b;

}

/*byteHEX() 用來把一個byte類型的數轉換成十六進制的ASCII表示

因為java中的byte的toString無法實現這一點 我們又沒有C語言中的

sprintf(outbuf % X ib)

*/

public static String byteHEX(byte ib) {

char[] Digit = { A B C D E F };

char[] ob = new char[ ];

ob[ ] = Digit[(ib >>> ) & X F];

ob[ ] = Digit[ib & X F];

String s = new String(ob);

return s;

}

public static void main(String args[]) {

MD m = new MD ();

if (Array getLength(args) == ) { //如果沒有參數 執行標準的Test Suite

System out println( MD Test suite: );

System out println( MD ( ): + m getMD ofStr( ));

System out println( MD ( a ): + m getMD ofStr( a ));

System out println( MD ( abc ): + m getMD ofStr( abc ));

System out println( MD ( ): + m getMD ofStr( ));

System out println( MD ( ): + m getMD ofStr( ));

System out println( MD ( message digest ): + m getMD ofStr( message digest ));

System out println( MD ( abcdefghijklmnopqrstuvwxyz ): + m getMD ofStr( abcdefghijklmnopqrstuvwxyz ));

System out println( MD ( ):

+ m getMD ofStr( ));

} else

System out println( MD ( + args[ ] + )= + m getMD ofStr(args[ ]));

}

lishixin/Article/program/Java/hx/201311/26604

J. java演算法 md5加密方法

MD5以512位分組來處理輸入的信息,且每一分組又被劃分為16個32位子分組,經過了一系列的處理後,演算法的輸出由四個32位分組組成,將這四個32位分組級聯後將生成一個128位散列值。
在MD5演算法中,首先需要對信息進行填充,使其位元組長度對512求余數的結果等於448。因此,信息的位元組長度(Bits Length)將被擴展至N*512+448,即N*64+56個位元組(Bytes),N為一個正整數。填充的方法如下,在信息的後面填充一個1和無數個0,直到滿足上面的條件時才停止用0對信息的填充。然後再在這個結果後面附加一個以64位二進製表示的填充前的信息長度。經過這兩步的處理,現在的信息位元組長度=N*512+448+64=(N+1)*512,即長度恰好是512的整數倍數。這樣做的原因是為滿足後面處理中對信息長度的要求。MD5中有四個32位被稱作鏈接變數(Chaining Variable)的整數參數,他們分別為:A=0x01234567,B=0x89abcdef,C=0xfedcba98,D=0x76543210。 當設置好這四個鏈接變數後,就開始進入演算法的四輪循環運算,循環的次數是信息中512位信息分組的數目。
將上面四個鏈接變數復制到另外四個變數中:A到a,B到b,C到c,D到d。 主循環有四輪(MD4隻有三輪),每輪循環都很相似。第一輪進行16次操作。每次操作對a、b、c和d中的其中三個作一次非線性函數運算,然後將所得結果加上第四個變數(文本中的一個子分組和一個常數)。
再將所得結果向右環移一個不定的數,並加上a、b、c或d中之一。最後用該結果取代a、b、c或d中之一。 以一下是每次操作中用到的四個非線性函數(每輪一個)。
F(X,Y,Z)=(X∧Y)∨(( X)∧Z)
G(X,Y,Z)=(X∧Z)∨(Y∧( Z))
H(X,Y,Z)=X⊕Y⊕Z
I(X,Y,Z)=Y⊕(X∨( Z))

其中,⊕是異或,∧是與,∨是或, 是反符號。
如果X、Y和Z的對應位是獨立和均勻的,那麼結果的每一位也應是獨立和均勻的。F是一個逐位運算的函數。即,如果X,那麼Y,否則Z。函數H是逐位奇偶操作符。所有這些完成之後,將A,B,C,D分別加上a,b,c,d。然後用下一分組數據繼續運行演算法,最後的輸出是A,B,C和D的級聯。最後得到的A,B,C,D就是輸出結果,A是低位,D為高位,DCBA組成128位輸出結果。
這個東西很復雜的,樓主就不要追究了吧!

閱讀全文

與javamd5密碼加密相關的資料

熱點內容
python怎麼在代碼中換行 瀏覽:775
電影007系列免費完整版 瀏覽:463
費恩曼物理學講義pdf 瀏覽:335
用python畫abaqus數據 瀏覽:57
邵氏電影 經典 瀏覽:341
程序員去市場買菜 瀏覽:390
win7kms激活伺服器地址 瀏覽:643
全解周易pdf 瀏覽:150
星際爭霸的打開地圖的命令 瀏覽:619
不用拉貨的搬家用什麼app 瀏覽:454
藍光無水印影視網站 瀏覽:318
萬勝壓縮機好嗎 瀏覽:674
男主是強大狐狸的小說 瀏覽:729
並聯電阻間比值演算法 瀏覽:386
電影院最後一排被 瀏覽:931
重生北京收古董的小說 瀏覽:478
主角叫江晨的重生小說 瀏覽:730
那種電影網址 瀏覽:872
帶人名的電影名 瀏覽:402
能看台劇的網站有哪些? 瀏覽:511