MD5、3DES加密

来源:互联网 发布:无间道网络剧粤语 编辑:程序博客网 时间:2024/06/10 19:10

 Md5加密是不可编译的一种加密文件;而3DES是可加密,可解密的另一种加密文件。今天就这两种加密方面贴出点代码供大家参考,说实话,3DES的加密不是我自己写的,是我从网上找的,然后修改过的。具体谁的博客,真的很不好意思,我给忘了,再找就找不到了。
首先看一下Main方法
Main方法
package com.test;
/*  
* 当前文件:Test .java  
* 作者:王延侠  
*/

public class Test { 
        
        public static void main(String[] args) {
                /*
                 * MD5加密
                 */
        String str = "my name is tomcat!";
       
       
                System.out.println("Md5加密以前"+str);
                System.out.println("Md5加密以后"+Md5.MD5(str));     
                /*
                 * 3DES加密
                 */
                TDES t = new TDES();
                t.getKey("Blowfish");//先得到密钥
                   System.out.println("3DES加密以前:"+str);
                System.out.println("3DES加密以后:"+t.getEncString(str));
                System.out.println("3DES解密以后:"+t.getDesString(t.getEncString(str)));
        }
}

3DES加密算法

package com.test;

/*  
* 当前文件:TDES.java  
* 作者:王延侠  
*/

import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class TDES{

Key key;

/**
* 根据参数生成KEY
*
* @param strKey
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 加密String明文输入,String密文输出
*
* @param strMing
* @return
*/
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
BASE64Encoder base64en = new BASE64Encoder();
try {
byteMing = strMing.getBytes("UTF8");
byteMi = this.getEncCode(byteMing);
strMi = base64en.encode(byteMi);
} catch (Exception e) {
e.printStackTrace();
} finally {
base64en = null;
byteMing = null;
byteMi = null;
}
return strMi;
}

/**
* 解密 以String密文输入,String明文输出
*
* @param strMi
* @return
*/
public String getDesString(String strMi) {
BASE64Decoder base64De = new BASE64Decoder();
byte[] byteMing = null;
byte[] byteMi = null;
String strMing = "";
try {
byteMi = base64De.decodeBuffer(strMi);
byteMing = this.getDesCode(byteMi);
strMing = new String(byteMing, "UTF8");
} catch (Exception e) {
e.printStackTrace();
} finally {
base64De = null;
byteMing = null;
byteMi = null;
}
return strMing;
}

/**
* 加密以byte[]明文输入,byte[]密文输出
*
* @param byteS
* @return
*/
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;
}

/**
* 解密以byte[]密文输入,以byte[]明文输出
*
* @param byteD
* @return
*/
private byte[] getDesCode(byte[] byteD) {
Cipher cipher;
byte[] byteFina = null;
try {
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
} catch (Exception e) {
e.printStackTrace();
} finally {
cipher = null;
}
return byteFina;

}
}

MD5加密算法
package com.test;
/*  
* 当前文件:Md5 .java  
* 作者:王延侠  
*/
import java.security.MessageDigest;

public class Md5 {
public final static String MD5(String md5) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
byte[] strTemp = md5.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte[] md = mdTemp.digest();
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
String dd = new String(str);
return dd;
} catch (Exception e) {
return null;
}
}
}

原创粉丝点击