MD5和SHA1
public static String SHA1(String decript) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(decript.getBytes());
byte messageDigest[] = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
/**
* MD5加密
*
* @param
* @return 加密后的字符串
*/
public static String MD5(String s) {
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] btInput = s.getBytes();
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.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];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
3DESECB 加密&解密
package com.unicom.util.encrypt;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class ThreeDESECB {
public static final String KEY_CHARSET_UTF8 = "UTF-8";
public static final String ALGORITHM_MODE = "DESede";
public static final String CIPHER_MODE = "DESede/ECB/PKCS5Padding";
public static String encrypt(final String src, final String key) throws Exception {
final DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(KEY_CHARSET_UTF8));
final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_MODE);
final SecretKey securekey = keyFactory.generateSecret(dks);
final Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.ENCRYPT_MODE, securekey);
final byte[] b = cipher.doFinal(src.getBytes());
final BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(b).replaceAll("\r", "").replaceAll("\n", "");
}
public static String decrypt(final String src, final String key) throws Exception {
final BASE64Decoder decoder = new BASE64Decoder();
final byte[] bytesrc = decoder.decodeBuffer(src);
final DESedeKeySpec dks = new DESedeKeySpec(key.getBytes(KEY_CHARSET_UTF8));
final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_MODE);
final SecretKey securekey = keyFactory.generateSecret(dks);
final Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.DECRYPT_MODE, securekey);
final byte[] retByte = cipher.doFinal(bytesrc);
return new String(retByte);
}
public static void main(String[] args) throws Exception {
String key = "sdsdfdsynddgfgwabccvzaad";
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String sysTime = df.format(new Date());
// 加密流程
String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpYXQiOjE1NDc1NDUwNzgsImVudElkIjoiTFRDUzIwMTYwOTE4MDcifQ.eg_Agqw7nCa3A5LwEh6B1Yfp6U6TRrPqBCqh3MYLgjJasx1vu_tFPL9oq0BhqHR7N9GbbzDjyuX4XfdwaxvhG";
String parmToken = token + sysTime;
String sgin = encrypt(URLEncoder.encode(parmToken, "UTF-8"), key);
System.out.println(sgin);
// 解密流程
String tele_decrypt = decrypt(sgin, key);
System.out.println("模拟代码解密:" + tele_decrypt);
}
}