md5解密java代碼的簡(jiǎn)單介紹

Java MD5如何解密?

MD5是單向加密的,不管何種數(shù)據(jù)進(jìn)行MD5加密都會(huì)得到固定長(zhǎng)度的字符串,

成都創(chuàng)新互聯(lián)專(zhuān)業(yè)為企業(yè)提供察哈爾右翼后網(wǎng)站建設(shè)、察哈爾右翼后做網(wǎng)站、察哈爾右翼后網(wǎng)站設(shè)計(jì)、察哈爾右翼后網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、察哈爾右翼后企業(yè)網(wǎng)站模板建站服務(wù),十年察哈爾右翼后做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

MD5一般用戶文件完整性的校驗(yàn),也有用來(lái)做密碼加密的。

想要破解MD5,因其本身的算法不可逆,故只能使用窮舉法,也就是不斷拼字符串加密和已知的MD5字符串進(jìn)行比對(duì),這是一個(gè)相當(dāng)大的工程,需要龐大的數(shù)據(jù)基礎(chǔ)。

java加密解密代碼

package com.cube.limail.util;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;/**

* 加密解密類(lèi)

*/

public class Eryptogram

{

private static String Algorithm ="DES";

private String key="CB7A92E3D3491964";

//定義 加密算法,可用 DES,DESede,Blowfish

static boolean debug = false ;

/**

* 構(gòu)造子注解.

*/

public Eryptogram ()

{

} /**

* 生成密鑰

* @return byte[] 返回生成的密鑰

* @throws exception 扔出異常.

*/

public static byte [] getSecretKey () throws Exception

{

KeyGenerator keygen = KeyGenerator.getInstance (Algorithm );

SecretKey deskey = keygen.generateKey ();

System.out.println ("生成密鑰:"+bytesToHexString (deskey.getEncoded ()));

if (debug ) System.out.println ("生成密鑰:"+bytesToHexString (deskey.getEncoded ()));

return deskey.getEncoded ();

} /**

* 將指定的數(shù)據(jù)根據(jù)提供的密鑰進(jìn)行加密

* @param input 需要加密的數(shù)據(jù)

* @param key 密鑰

* @return byte[] 加密后的數(shù)據(jù)

* @throws Exception

*/

public static byte [] encryptData (byte [] input ,byte [] key ) throws Exception

{

SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );

if (debug )

{

System.out.println ("加密前的二進(jìn)串:"+byte2hex (input ));

System.out.println ("加密前的字符串:"+new String (input ));

} Cipher c1 = Cipher.getInstance (Algorithm );

c1.init (Cipher.ENCRYPT_MODE ,deskey );

byte [] cipherByte =c1.doFinal (input );

if (debug ) System.out.println ("加密后的二進(jìn)串:"+byte2hex (cipherByte ));

return cipherByte ;

} /**

* 將給定的已加密的數(shù)據(jù)通過(guò)指定的密鑰進(jìn)行解密

* @param input 待解密的數(shù)據(jù)

* @param key 密鑰

* @return byte[] 解密后的數(shù)據(jù)

* @throws Exception

*/

public static byte [] decryptData (byte [] input ,byte [] key ) throws Exception

{

SecretKey deskey = new javax.crypto.spec.SecretKeySpec (key ,Algorithm );

if (debug ) System.out.println ("解密前的信息:"+byte2hex (input ));

Cipher c1 = Cipher.getInstance (Algorithm );

c1.init (Cipher.DECRYPT_MODE ,deskey );

byte [] clearByte =c1.doFinal (input );

if (debug )

{

System.out.println ("解密后的二進(jìn)串:"+byte2hex (clearByte ));

System.out.println ("解密后的字符串:"+(new String (clearByte )));

} return clearByte ;

} /**

* 字節(jié)碼轉(zhuǎn)換成16進(jìn)制字符串

* @param byte[] b 輸入要轉(zhuǎn)換的字節(jié)碼

* @return String 返回轉(zhuǎn)換后的16進(jìn)制字符串

*/

public static String byte2hex (byte [] b )

{

String hs ="";

String stmp ="";

for (int n =0 ;n b.length ;n ++)

{

stmp =(java.lang.Integer.toHexString (b [n ] 0XFF ));

if (stmp.length ()==1 ) hs =hs +"0"+stmp ;

else hs =hs +stmp ;

if (n b.length -1 ) hs =hs +":";

} return hs.toUpperCase ();

}

/**

* 字符串轉(zhuǎn)成字節(jié)數(shù)組.

* @param hex 要轉(zhuǎn)化的字符串.

* @return byte[] 返回轉(zhuǎn)化后的字符串.

*/

public static byte[] hexStringToByte(String hex) {

int len = (hex.length() / 2);

byte[] result = new byte[len];

char[] achar = hex.toCharArray();

for (int i = 0; i len; i++) {

int pos = i * 2;

result[i] = (byte) (toByte(achar[pos]) 4 | toByte(achar[pos + 1]));

}

return result;

}

private static byte toByte(char c) {

byte b = (byte) "0123456789ABCDEF".indexOf(c);

return b;

}

/**

* 字節(jié)數(shù)組轉(zhuǎn)成字符串.

* @param String 要轉(zhuǎn)化的字符串.

* @return 返回轉(zhuǎn)化后的字節(jié)數(shù)組.

*/

public static final String bytesToHexString(byte[] bArray) {

StringBuffer sb = new StringBuffer(bArray.length);

String sTemp;

for (int i = 0; i bArray.length; i++) {

sTemp = Integer.toHexString(0xFF bArray[i]);

if (sTemp.length() 2)

sb.append(0);

sb.append(sTemp.toUpperCase());

}

return sb.toString();

}

/**

* 從數(shù)據(jù)庫(kù)中獲取密鑰.

* @param deptid 企業(yè)id.

* @return 要返回的字節(jié)數(shù)組.

* @throws Exception 可能拋出的異常.

*/

public static byte[] getSecretKey(long deptid) throws Exception {

byte[] key=null;

String value=null;

//CommDao dao=new CommDao();

// List list=dao.getRecordList("from Key k where k.deptid="+deptid);

//if(list.size()0){

//value=((com.csc.sale.bean.Key)list.get(0)).getKey();

value = "CB7A92E3D3491964";

key=hexStringToByte(value);

//}

if (debug)

System.out.println("密鑰:" + value);

return key;

}

public String encryptData2(String data) {

String en = null;

try {

byte[] key=hexStringToByte(this.key);

en = bytesToHexString(encryptData(data.getBytes(),key));

} catch (Exception e) {

e.printStackTrace();

}

return en;

}

public String decryptData2(String data) {

String de = null;

try {

byte[] key=hexStringToByte(this.key);

de = new String(decryptData(hexStringToByte(data),key));

} catch (Exception e) {

e.printStackTrace();

}

return de;

}

} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //獲得鑰匙(字節(jié)數(shù)組)

byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //傳入密碼和鑰匙,獲得加密后的字節(jié)數(shù)組的密碼

password=Eryptogram.bytesToHexString(tmp); //將字節(jié)數(shù)組轉(zhuǎn)化為字符串,獲得加密后的字符串密碼解密與之差不多

如何在java中實(shí)現(xiàn)md5加密和解密

package endecrypt;

02.

03.import java.io.UnsupportedEncodingException;

04.import java.security.MessageDigest;

05.import java.security.NoSuchAlgorithmException;

06.

07./**

08. * 采用MD5加密解密

09. * @author tfq

10. * @datetime 2011-10-13

11. */

12.public class MD5Util {

13.

14. /***

15. * MD5加碼 生成32位md5碼

16. */

17. public static String string2MD5(String inStr){

18. MessageDigest md5 = null;

19. try{

20. md5 = MessageDigest.getInstance("MD5");

21. }catch (Exception e){

22. System.out.println(e.toString());

23. e.printStackTrace();

24. return "";

25. }

26. char[] charArray = inStr.toCharArray();

27. byte[] byteArray = new byte[charArray.length];

28.

29. for (int i = 0; i charArray.length; i++)

30. byteArray[i] = (byte) charArray[i];

31. byte[] md5Bytes = md5.digest(byteArray);

32. StringBuffer hexValue = new StringBuffer();

33. for (int i = 0; i md5Bytes.length; i++){

34. int val = ((int) md5Bytes[i]) 0xff;

35. if (val 16)

36. hexValue.append("0");

37. hexValue.append(Integer.toHexString(val));

38. }

39. return hexValue.toString();

40.

41. }

42.

43. /**

44. * 加密解密算法 執(zhí)行一次加密,兩次解密

45. */

46. public static String convertMD5(String inStr){

47.

48. char[] a = inStr.toCharArray();

49. for (int i = 0; i a.length; i++){

50. a[i] = (char) (a[i] ^ 't');

51. }

52. String s = new String(a);

53. return s;

54.

55. }

java中,從數(shù)據(jù)庫(kù)取出來(lái)的密碼加密了,用代碼怎么實(shí)現(xiàn)md5解密

md5是不可逆的,只不過(guò)用的人多了,就有人創(chuàng)建了一個(gè)md5密碼表,應(yīng)該就是傳說(shuō)中的彩虹表,蜜要是有這個(gè)表就可以了

java 如何采用md5解密

package endecrypt;

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

/**

* 采用MD5加密解密

* @author tfq

* @datetime 2011-10-13

*/

public class MD5Util {

/***

* MD5加碼 生成32位md5碼

*/

public static String string2MD5(String inStr){

MessageDigest md5 = null;

try{

md5 = MessageDigest.getInstance("MD5");

}catch (Exception e){

System.out.println(e.toString());

e.printStackTrace();

return "";

}

char[] charArray = inStr.toCharArray();

byte[] byteArray = new byte[charArray.length];

for (int i = 0; i charArray.length; i++)

byteArray[i] = (byte) charArray[i];

byte[] md5Bytes = md5.digest(byteArray);

StringBuffer hexValue = new StringBuffer();

for (int i = 0; i md5Bytes.length; i++){

int val = ((int) md5Bytes[i]) 0xff;

if (val 16)

hexValue.append("0");

hexValue.append(Integer.toHexString(val));

}

return hexValue.toString();

}

/**

* 加密解密算法 執(zhí)行一次加密,兩次解密

*/

public static String convertMD5(String inStr){

char[] a = inStr.toCharArray();

for (int i = 0; i a.length; i++){

a[i] = (char) (a[i] ^ 't');

}

String s = new String(a);

return s;

}

// 測(cè)試主函數(shù)

public static void main(String args[]) {

String s = new String("tangfuqiang");

System.out.println("原始:" + s);

System.out.println("MD5后:" + string2MD5(s));

System.out.println("加密的:" + convertMD5(s));

System.out.println("解密的:" + convertMD5(convertMD5(s)));

}

}

名稱(chēng)欄目:md5解密java代碼的簡(jiǎn)單介紹
轉(zhuǎn)載來(lái)源:http://www.muchs.cn/article22/hssejc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、網(wǎng)站設(shè)計(jì)公司、建站公司、域名注冊(cè)、小程序開(kāi)發(fā)、品牌網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁(yè)設(shè)計(jì)