Md5\Base64\Des 常用加密算法
常用的几种加解密方式备份:
?
1、MD5方式:
import java.security.MessageDigest;
?
String s= "test";
?
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] byteArray = md5.digest(s.getBytes("UTF-8"));
?
?
?
?
2、Base64方式:
import org.apache.commons.codec.binary.Base64;
?
String s="test";
Base64.decodeBase64(s.getBytes("UTF-8"));
?
?
?
byte[] encodeBytes = Base64.encodeBase64(s.getBytes("UTF-8"), true);
String encodeString ?= ?new String(encodeBytes).trim();
?
?
?
3、Des方式:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
?
private static final String DEFAULT_ENCRYPT_MODE = "DES/ECB/PKCS5Padding";
private static final String DEFAULT_KEY_ALGORITHM = "DES";
private static final String DESEDE_KEY_ALGORITHM = "DESede";
?
?
public static byte[] DES(byte[] bytes, String algorithm, int iMode, byte[] key, byte[] iv) {
if (StringUtils.isEmpty(algorithm)) {
algorithm = DEFAULT_ENCRYPT_MODE;
}
if (iMode != Cipher.ENCRYPT_MODE && iMode != Cipher.DECRYPT_MODE) {
throw new IllegalArgumentException("iMode只能为1或2");
}
?
try {
DESKeySpec desKeySpec = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DEFAULT_KEY_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
?
Cipher cipher = Cipher.getInstance(algorithm);
if (iv != null) {
IvParameterSpec ivps = new IvParameterSpec(iv);
cipher.init(iMode, secretKey, ivps);
} else {
cipher.init(iMode, secretKey);
}
return cipher.doFinal(bytes);
} catch (Exception ex) {
logger.error("", ex);
}
return null;
}
?
//加解密转换用DESede
?
public static byte[] DESede(byte[] bytes, String algorithm, int iMode, byte[] key, byte[] iv) {
if (StringUtils.isEmpty(algorithm)) {
algorithm = DEFAULT_ENCRYPT_MODE;
}
if (iMode != Cipher.ENCRYPT_MODE && iMode != Cipher.DECRYPT_MODE) {
throw new IllegalArgumentException("iMode只能为1或2");
}
?
try {
DESedeKeySpec desKeySpec = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DESEDE_KEY_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
?
Cipher cipher = Cipher.getInstance(algorithm);
if (iv != null) {
IvParameterSpec ivps = new IvParameterSpec(iv);
cipher.init(iMode, secretKey, ivps);
} else {
cipher.init(iMode, secretKey);
}
return cipher.doFinal(bytes);
} catch (Exception ex) {
logger.error("", ex);
}
return null;
}
?
public static byte[] DESedeWithPKCS7Padding(byte[] bytes, String algorithm, int iMode,
byte[] key, byte[] iv) {
// DESede/ECB/PKCS5Padding
if (StringUtils.isEmpty(algorithm)) {
algorithm = DEFAULT_ENCRYPT_MODE;
}
if (iMode != Cipher.ENCRYPT_MODE && iMode != Cipher.DECRYPT_MODE) {
throw new IllegalArgumentException("iMode只能为1或2");
}
?
try {
DESedeKeySpec desKeySpec = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DESEDE_KEY_ALGORITHM);
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
?
Cipher cipher = Cipher.getInstance(algorithm, "BC");
if (iv != null) {
IvParameterSpec ivps = new IvParameterSpec(iv);
cipher.init(iMode, secretKey, ivps);
} else {
cipher.init(iMode, secretKey);
}
return cipher.doFinal(bytes);
} catch (Exception ex) {
ex.printStackTrace();
logger.error("", ex);
}
return null;
}
?
?
?
4、Hex与byte[]转换:
?
?
/**
* 将字节数组转换为16进制字符串表示
*?
* @param bytes
* ? ? ? ? ? ?待转换的字节数组
* @return 16进制表示的字符串
*/
public static String bytes2Hex(byte[] bytes) {
StringBuilder sb = new StringBuilder();
String hex = null;
for (int i = 0; i < bytes.length; i++) {
hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sb.append("0");
}
sb.append(hex);
}
return sb.toString();
}
?
/**
* 将16进制字符串转换为字节数组
*?
* @param hexstr
* @return
*/
public static byte[] hex2Bytes(String hexstr) {
byte[] b = hexstr.getBytes();
int length = b.length;
if ((length % 2) != 0) {
throw new IllegalArgumentException("字符串对应字节数必须是偶数");
}
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
?