數(shù)據(jù)加密標(biāo)準(zhǔn)(英語:Data Encryption Standard,縮寫為 DES)是一種對稱密鑰加密塊密碼算法祝迂,1976年被美國聯(lián)邦政府的國家標(biāo)準(zhǔn)局確定為聯(lián)邦資料處理標(biāo)準(zhǔn)(FIPS)阅嘶,隨后在國際上廣泛流傳開來。它基于使用56位密鑰的對稱算法。這個算法因為包含一些機密設(shè)計元素烘嘱,相對短的密鑰長度以及懷疑內(nèi)含美國國家安全局(NSA)的后門而在開始時有爭議鹦付,DES因此受到了強烈的學(xué)院派式的審查尚粘,并以此推動了現(xiàn)代的塊密碼及其密碼分析的發(fā)展。??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ---維基百科?
一敲长、DES簡介
1郎嫁、DES:數(shù)據(jù)加密標(biāo)準(zhǔn)互捌,是對稱加密算法領(lǐng)域中的典型算法
2、特點:秘鑰偏短(56位)行剂,生命周期短
3秕噪、JDK內(nèi)部提供了算法的實現(xiàn)
二、相關(guān)代碼
1厚宰、生成密鑰
public static byte[] getKey() throws Exception
{
KeyGenerator keyGen = KeyGenerator.getInstance("DES");
keyGen.init(56);
SecretKey secretKey = keyGen.generateKey();
return secretKey.getEncoded();
}
2腌巾、DES加密
/**
* DES加密
* @param data 要加密的原始數(shù)據(jù)
* @param key? 密鑰
* @return 加密后的數(shù)據(jù)
* @throws Exception
*/
public static byte[] encrypt(byte[] data, byte[] key) throws Exception
{
SecretKey secretKey = new SecretKeySpec(key, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
3、DES解密
/**
* DES解密
* @param data 要解密的數(shù)據(jù)
* @param key? 密鑰
* @return 解密后的數(shù)據(jù)
* @throws Exception
*/
public static byte[] decrypt(byte[] data, byte[] key) throws Exception
{
SecretKey secretKey = new SecretKeySpec(key, "DES");
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
return cipher.doFinal(data);
}
4铲觉、字節(jié)轉(zhuǎn)十六進(jìn)制
/**
* 字節(jié)轉(zhuǎn)十六進(jìn)制
*
* @param resultBytes
*? ? ? ? ? ? 輸入源
* @return 十六進(jìn)制字符串
*/
public static String fromBytesToHex(byte[] resultBytes) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < resultBytes.length; i++) {
if (Integer.toHexString(0xFF & resultBytes[i]).length() == 1) {
builder.append("0").append(Integer.toHexString(0xFF & resultBytes[i]));
} else {
builder.append(Integer.toHexString(0xFF & resultBytes[i]));
}
}
return builder.toString();
}