ThreeDESUtil.class 用于文件或字符串加密
package com.doris.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* 加密 解密
* @author Doris
*
* 2016年4月19日
*/
public class ThreeDESUtil {
public static String Algorithm = "DESede";
public static String PASSWORD_CRYPT_KEY = "密钥可自行设定";
/**
* 解密
* @param src
* @return
*/
public static byte[] decryptMode(byte[] src) {
try {
SecretKey desKey = new SecretKeySpec(
build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.DECRYPT_MODE, desKey);
return cipher.doFinal(src);
} catch (Exception e){
Log.getInstance().writeLog(e.getMessage());
}
return null;
}
/**
* 加密
* @param src
* @return
*/
public static byte[] encryptMode(byte[] src){
try {
SecretKey desKey = new SecretKeySpec(
build3DesKey(PASSWORD_CRYPT_KEY), Algorithm);
Cipher cipher = Cipher.getInstance(Algorithm);
cipher.init(Cipher.ENCRYPT_MODE, desKey);
return cipher.doFinal(src);
} catch (java.security.NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (javax.crypto.NoSuchPaddingException e2) {
e2.printStackTrace();
} catch (java.lang.Exception e3) {
e3.printStackTrace();
}
return null;
}
public static byte[] build3DesKey(String keyStr)
throws UnsupportedEncodingException{
byte[] key = new byte[24];
byte[] temp = keyStr.getBytes("utf-8");
/*
* 执行数组拷贝
* System.arraycopy(源数组,从源数组哪里开始拷贝,目标数组,拷贝多少位)
*/
if (key.length > temp.length) {
// 如果temp不够24位,则拷贝temp数组整个长度的内容到key数组
System.arraycopy(temp, 0, key, 0, temp.length);
} else {
// 如果temp大于24位,则拷贝temp数组24个长度的内容到key数组
System.arraycopy(temp, 0, key, 0, key.length);
}
return key;
}
/**
* 把文件转成byte数组
* @param file
* @return
*/
public static byte[] fileToByte(File file){
try {
FileInputStream fis = new FileInputStream(file);
if (file.length() > Integer.MAX_VALUE) {
return null;
}
int Length = (int) file.length();
byte[] bytes = new byte[Length];
fis.read(bytes);
fis.close();
return bytes;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
MD5Util.class 用于密钥加密
package com.doris.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 密钥用MD5加密
* @author Doris
*
* 2016年4月19日
*/
public class MD5Util {
public static String encodeAsHexString(byte[] data) {
StringBuffer buf = new StringBuffer();
for (byte ch : data) {
String s = Integer.toHexString(ch & 0xff);
buf.append(s.length() == 1 ? s = "0" + s : s);
}
return buf.toString();
}
public static String getMD5String(byte[] data) {
try {
return encodeAsHexString(encodeAsMD5(data));
} catch (Exception e) {
}
return "";
}
public static String getMD5String(String text) {
return getMD5String(text.getBytes());
}
public static byte[] getMd5(String text) throws Exception {
byte[] bt = encodeAsMD5(text.getBytes("UTF-8"));
return bt;
}
public static byte[] encodeAsMD5(byte[] data)
throws NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(data);
return md5.digest();
}
}
测试类:
package com.doris.util;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Main {
public static void main(String[] args){
String msg = "3DES加密解密案例";
System.out.println("【加密前】:" + msg);
try {
// 加密
byte[] secretArr = ThreeDESUtil.encryptMode(msg.getBytes());
System.out.println("【加密后】:" + new String(secretArr));
// 解密
byte[] myMsgArr = ThreeDESUtil.decryptMode(secretArr);
System.out.println("【解密后】:" + new String(myMsgArr));
} catch (Exception e) {
e.printStackTrace();
}
try {
File file = new File("文件路径带文件后缀");
byte[] bytes = ThreeDESUtil.fileToByte(file);
ThreeDESUtil.PASSWORD_CRYPT_KEY = MD5Util.getMD5String("密钥");
byte[] s = ThreeDESUtil.encryptMode(bytes); //加密
// byte[] s = ThreeDESUtil.decryptMode(bytes); //解密
OutputStream os = new FileOutputStream("加密或解密后文件路径带文件后缀");
for (int i = 0; i < s.length; i++) {
os.write((int) s[i]);
os.flush();
}
os.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("ok");
}
}
字符串加密结果:
不用多说,结果会根据密钥的不同而不同,所以解密的时候一定要用加密的密钥进行解密。
注意:
如果想要在手机上测试加密字符串,密钥和加密字符可用输入框,但如果显示在TextView是不可行的,TextView对特殊字符串是没有处理的,所有也不用去TextView的值进行解密。