DES算法

數(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位密鑰的對稱算法。這個算法因為包含一些機(jī)密設(shè)計元素,相對短的密鑰長度以及懷疑內(nèi)含美國國家安全局(NSA)的后門而在開始時有爭議,DES因此受到了強(qiáng)烈的學(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();

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 概述 之前一直對加密相關(guān)的算法知之甚少,只知道類似DES、RSA等加密算法能對數(shù)據(jù)傳輸進(jìn)行加密,且各種加密算法各有...
    Henryzhu閱讀 3,232評論 0 14
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,697評論 19 139
  • 在開發(fā)應(yīng)用過程中,客戶端與服務(wù)端經(jīng)常需要進(jìn)行數(shù)據(jù)傳輸,涉及到重要隱私安全信息時,開發(fā)者自然會想到對其進(jìn)行加密,即使...
    閑庭閱讀 3,440評論 0 11
  • 姓名:于川皓 學(xué)號:16140210089 轉(zhuǎn)載自:https://baike.baidu.com/item/RS...
    道無涯_cc76閱讀 2,818評論 0 1
  • 早餐:一碗皮蛋瘦肉粥 午餐:半塊豆腐,半碗豆角,一碗飯 晚餐: 運(yùn)動:早起跳操半小時,騎行1小時到北大橋 感悟:有...
    影子3623253閱讀 199評論 0 2

友情鏈接更多精彩內(nèi)容