import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* DES加密介绍:
* DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。DES加密算法出自IBM的研究,
* 后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少,因为DES使用56位密钥,以现代计算能力,
* 可以较容易破解
* 注意:DES加密和解密过程中,密钥长度都必须是8的倍数
*/
public class DES {
private static final byte[] IV_VALUE = {12, 24, 48, 36, 48, 60, 72, 84};
private static final byte[] KEY = {0X64, 0X6A, 0X71, 0X68, 0X2E, 0X63, 0X6F, 0X6D};
public static String encryptDES(String str) throws Exception {
IvParameterSpec zeroIv = new IvParameterSpec(IV_VALUE);
SecretKeySpec key = new SecretKeySpec(KEY, "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
byte[] encryptedData = cipher.doFinal(str.getBytes("UTF-8"));
return new BASE64Encoder().encode(encryptedData);
}
public static String decryptDES(String miwen) throws Exception {
byte[] byteMi = new BASE64Decoder().decodeBuffer(miwen);
IvParameterSpec zeroIv = new IvParameterSpec(IV_VALUE);
SecretKeySpec key = new SecretKeySpec(KEY, "DES");
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, key, zeroIv);
byte decryptedData[] = cipher.doFinal(byteMi);
return new String(decryptedData, "UTF-8");
}
}
DES对字符串的加密
最新推荐文章于 2020-06-17 16:01:00 发布