import org.apache.commons.codec.digest.DigestUtils;
public class MD5 {
/**
* MD5方法
*
* @param text 明文
* @param key 密钥
* @return 密文
* @throws Exception
*/
public static String md5(String text, String key) {
//加密后的字符串
String encodeStr=DigestUtils.md5Hex(text + key);
// System.out.println("MD5加密后的字符串为:encodeStr="+encodeStr);
return encodeStr;
}
/**
* MD5验证方法
*
* @param text 明文
* @param key 密钥
* @param md5 密文
* @return true/false
* @throws Exception
*/
public static boolean verify(String text, String key, String md5) {
//根据传入的密钥进行验证
String md5Text = md5(text, key);
if(md5Text.equalsIgnoreCase(md5))
{
// System.out.println("MD5验证通过");
return true;
}
return false;
}
}
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class DES3EncryptionUtil {
/**
* 转换成十六进制字符串
* @param key
* @return
*
* lee on 2017-08-09 10:54:19
*/
public static byte[] hex(String key){
String f = DigestUtils.md5Hex(key);
byte[] bkeys = new String(f).getBytes();
byte[] enk = new byte[24];
for (int i=0;i<24;i++){
enk[i] = bkeys[i];
}
return enk;
}
/**
* 3DES加密
* @param key 密钥,24位
* @param srcStr 将加密的字符串
* @return
*
* lee on 2017-08-09 10:51:44
*/
public static String encode3Des(String key,String srcStr){
byte[] keybyte = hex(key);
byte[] src = srcStr.getBytes();
try {
//生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
//加密
Cipher c1 = Cipher.getInstance("DESede");
c1.init(Cipher.ENCRYPT_MODE, deskey);
String pwd = Base64.encodeBase64String(c1.doFinal(src));
// return c1.doFinal(src);//在单一方面的加密或解密
return pwd;
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
/**
* 3DES解密
* @param key 加密密钥,长度为24字节
* @param desStr 解密后的字符串
* @return
*
* lee on 2017-08-09 10:52:54
*/
public static String decode3Des(String key, String desStr){
Base64 base64 = new Base64();
byte[] keybyte = hex(key);
byte[] src = base64.decode(desStr);
try {
//生成密钥
SecretKey deskey = new SecretKeySpec(keybyte, "DESede");
//解密
Cipher c1 = Cipher.getInstance("DESede");
c1.init(Cipher.DECRYPT_MODE, deskey);
String pwd = new String(c1.doFinal(src));
// return c1.doFinal(src);
return pwd;
} catch (java.security.NoSuchAlgorithmException e1) {
// TODO: handle exception
e1.printStackTrace();
}catch(javax.crypto.NoSuchPaddingException e2){
e2.printStackTrace();
}catch(java.lang.Exception e3){
e3.printStackTrace();
}
return null;
}
}
来源:CSDN
作者:樱木花道.光
链接:https://blog.csdn.net/qq_30909079/article/details/104740779