网站首页 > 文章中心 > 其它

md5解密java代码的简单介绍

作者:小编 更新时间:2023-09-21 18:13:03 浏览量:72人看过

Java MD5如何解密?

java加密解密代码

package com.cube.limail.util;

import javax.crypto.Cipher;

import javax.crypto.KeyGenerator;

import javax.crypto.SecretKey;/**

* 加密解密类

md5解密java代码的简单介绍-图1

*/

public class Eryptogram

{

private static String Algorithm ="DES";

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

md5解密java代码的简单介绍-图2

static boolean debug = false ;

/**

* 构造子注解.

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

* 将指定的数据根据提供的密钥进行加密

* @param input 需要加密的数据

* @param key 密钥

* @return byte[] 加密后的数据

* @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 ("加密前的字符串:"+new String (input ));

} Cipher c1 = Cipher.getInstance (Algorithm );

c1.init (Cipher.ENCRYPT_MODE ,deskey );

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

return cipherByte ;

* 将给定的已加密的数据通过指定的密钥进行解密

* @param input 待解密的数据

* @return byte[] 解密后的数据

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

Cipher c1 = Cipher.getInstance (Algorithm );

c1.init (Cipher.DECRYPT_MODE ,deskey );

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

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

} return clearByte ;

* @param 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 ();

}

* 字符串转成字节数组.

* @param hex 要转化的字符串.

* @return byte[] 返回转化后的字符串.

public static byte[] hexStringToByte(String hex) {

byte[] result = new byte[len];

char[] achar = hex.toCharArray();

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

return result;

private static byte toByte(char c) {

return b;

* 字节数组转成字符串.

* @param String 要转化的字符串.

* @return 返回转化后的字节数组.

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]);

sb.append(0);

sb.append(sTemp.toUpperCase());

return sb.toString();

* 从数据库中获取密钥.

* @param deptid 企业id.

* @return 要返回的字节数组.

* @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();

key=hexStringToByte(value);

//}

if (debug)

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

return key;

String en = null;

try {

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

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

} catch (Exception e) {

e.printStackTrace();

return en;

String de = null;

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

return de;

} 加密使用: byte[] key=Eryptogram.getSecretKey(deptid); //获得钥匙(字节数组)

byte[] tmp=Eryptogram.encryptData(password.getBytes(), key); //传入密码和钥匙,获得加密后的字节数组的密码

md5解密java代码的简单介绍-图3

password=Eryptogram.bytesToHexString(tmp); //将字节数组转化为字符串,获得加密后的字符串密码解密与之差不多

如何在java中实现md5加密和解密

package endecrypt;

①.1. */

java中,从数据库取出来的密码加密了,用代码怎么实现md5解密

java 如何采用md5解密

import java.io.UnsupportedEncodingException;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

* @author tfq

/***

try{

}catch (Exception e){

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

return "";

char[] charArray = inStr.toCharArray();

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

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

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

StringBuffer hexValue = new StringBuffer();

hexValue.append("0");

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

return hexValue.toString();

* 加密解密算法 执行一次加密,两次解密

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;

// 测试主函数

public static void main(String args[]) {

String s = new String("tangfuqiang");

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

以上就是土嘎嘎小编大虾米为大家整理的相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章