对称加密:
des 代码示例:
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
/**
* 对称加密算法 DES
*/
public class TestDES {
/**密钥算法**/
public static final String KEY_ALGORITHM = “DES”;
/**加密/解密算法 /工作模式/填充方式**/
public static final String CIPHER_ALGORITHM = “DES/ECB/PKCS5PADDING”;
private static final char[] CH_HEX = {‘0′,’1′,’2′,’3′,’4′,’5′,’6′,’7′,’8′,’9′,’A’,
‘B’,’C’,’D’,’E’,’F’};
/**
*生成密钥
* @return
* @throws Exception
*/
public static byte[] initKey() throws Exception{
// 实例化 密钥生成器
KeyGenerator kg = KeyGenerator. getInstance (KEY_ALGORITHM);
//初始化密钥生成器
kg.init(56, new SecureRandom());
//生成密钥
SecretKey secretKey = kg.generateKey();
//获得密钥的二进制编码形式
return secretKey.getEncoded();
}
/**
* 转换密钥
* @param key
* @return
* @throws Exception
*/
public static Key toKey(byte[] key) throws Exception{
//实例化des密钥材料
DESKeySpec keySpec = new DESKeySpec(key);
//实例化密钥工厂
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
//生成秘密密钥
SecretKey secretKey = keyFactory.generateSecret(keySpec);
return secretKey;
}
/**
* 加密
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encrypt(byte[] data,byte[] key) throws Exception{
//还原密钥
Key k = toKey(key);
//实例化
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,设置化加密模式
cipher.init(Cipher.ENCRYPT_MODE,k);
return cipher.doFinal(data);
}
public static byte[] decrypt(byte[] data,byte[] key) throws Exception{
//还原密钥
Key k = toKey(key);
//实例化
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//初始化,设置化加密模式
cipher.init(Cipher.DECRYPT_MODE,k);
return cipher.doFinal(data);
}
public static void main(String[] args) {
String str = “ceshides”;
try {
//获得密钥
byte[] keys = initKey();
//加密
byte[] encrypes = encrypt(str.get bytes (),keys);
String result = byteArrayToHex(encrypes);
System.out.println(“加密后结果为:”+result);
//解密
hexStringToBytes(result);
byte[] decrypts = decrypt(hexStringToBytes(result), keys);
System.out.println(“解密结果为:”+new String(decrypts));
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 将字节数组转化为16 进制 字符串
* @param bytes
* @return
*/
private static String byteArrayToHex(byte[] bytes){
// 一个字节占8位,一个十六进制字符占4位;十六进制字符数组的长度为字节数组长度的两倍
char [] chars = new char[bytes.length*2];
int index = 0;
for(byte b:bytes){
//取字节的高4位
chars[index++] = CH_HEX[b>>>4 & 0xf];
//取字节的低4位
chars[index++] = CH_HEX[b & 0xf];
}
return new String(chars).toUpperCase();
}
/**
* 16进制字符串转换为字节数组
* @param hexString
* @return
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals(“”)) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c) {
return (byte) “0123456789ABCDEF”.indexOf(c);
}
}
测试结果为:
加密后结果为:4B9ADEDC61FD508F082A1965C8AEB669
解密结果为:ceshides
微公号:roc_shangcp