java實(shí)現(xiàn)的DES加密算法詳解
本文實(shí)例講述了java實(shí)現(xiàn)的DES加密算法。分享給大家供大家參考,具體如下:
一、DES加密算法介紹
1、要求密鑰必須是8個(gè)字節(jié),即64bit長(zhǎng)度
2、因?yàn)槊荑€是byte[8] , 代表字符串也可以是非可見(jiàn)的字節(jié),可以與Base64編碼算法一起使用
3、加密、解密都需要通過(guò)字節(jié)數(shù)組作為數(shù)據(jù)和密鑰進(jìn)行處理
二、對(duì)稱加密
DES加密算法屬于對(duì)稱加密。
即利用指定的密鑰,按照密碼的長(zhǎng)度截取數(shù)據(jù),分成數(shù)據(jù)塊,和密鑰進(jìn)行復(fù)雜的移位、算數(shù)運(yùn)算或者數(shù)據(jù)處理等操作,形成只有特定的密碼才能夠解開(kāi)的數(shù)據(jù)。 加密與解密用的是同一個(gè)密鑰
三、相關(guān)類
1、Cipher:
Java/Android要使用任何加密,都需要使用Cipher這個(gè)類
使用Cipher進(jìn)行加密,解密處理,需要?jiǎng)?chuàng)建實(shí)例對(duì)象并初始化。采用工廠模式創(chuàng)建對(duì)象
Cipher cipher = Cipher.getInstance("算法名稱");
cipher.init(加密/解密模式,Key秒);
2、Key:
Key類是Java加密系統(tǒng)所有密碼的父類
3、SecretKeyFactory:
對(duì)于DES加密解密,使用SecretKeyFactory生成,生成時(shí)需指定DESKeySpec
四、加密代碼步驟
1. 獲取Cipher對(duì)象,設(shè)置加密算法
Cipher cipher = Cipher.getInstance("DES");
2、準(zhǔn)備Key對(duì)象
2.1 DES加密算法使用DESKeySpec類,構(gòu)造方法參數(shù)需要為8個(gè)字節(jié)的密碼
創(chuàng)建DESKeySpec類對(duì)象
參數(shù)為密鑰,8個(gè)字節(jié)
DESKeySpec keySpec = new DESKeySpec(new byte[1,2,3,4,5,6,7,8]);
2.2 轉(zhuǎn)換成Key對(duì)象
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("EDS"); SecretKey key = keyFactory.generateSecret(keySpec);
3.設(shè)置Cipher模式,加密/解密 ,參數(shù)一 :模式 ,參數(shù)二:Key對(duì)象,返回字節(jié)數(shù)組
Cipher.DECRYPT_MODE 解密
Cipher.ENCRYPT_MODE 加密
cipher.init(Cipher.ENCRYPT_MODE,key);
4.返回加密結(jié)果,參數(shù)為加密內(nèi)容
bytep[] ret = cipher.doFinal(data);
因?yàn)閷?duì)稱加密加密與解密是相逆的。所以解密步驟和加密步驟一樣,只是cipher.init()的模式不同,所以我們可以寫一個(gè)工具類來(lái)進(jìn)行DES加密算法的加密解密
DES加密算法工具類
/** * DES加密算法 * @param mode 模式: 加密,解密 * @param data 需要加密的內(nèi)容 * @param keyData 密鑰 8個(gè)字節(jié)數(shù)組 * @return 將內(nèi)容加密后的結(jié)果也是byte[]格式的 */ public static byte[] des(int mode,byte[] data,byte[] keyData) { byte[] ret = null; //加密的內(nèi)容存在并且密鑰存在且長(zhǎng)度為8個(gè)字節(jié) if (data != null && data.length>0 &&keyData!=null && keyData.length==8) { try { Cipher cipher = Cipher.getInstance("DES"); DESKeySpec keySpec = new DESKeySpec(keyData); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey key = keyFactory.generateSecret(keySpec); cipher.init(mode, key); ret = cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeySpecException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } } return ret; } //DES 加密 public static byte[] desEncrypt(byte[] data,byte[] keyData){ return des(Cipher.ENCRYPT_MODE,data,keyData); } //DES 解密 public static byte[] desDecrypt(byte[] data,byte[] keyData){ return des(Cipher.DECRYPT_MODE,data,keyData); }
五、示例
SythEncryptActivity.class:
package com.xqx.encrypsthow; import android.app.Activity; import android.os.Bundle; import android.util.Base64; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import utils.EncryptUtil; import javax.crypto.*; import javax.crypto.spec.DESKeySpec; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; import java.util.Arrays; /** * Created by Administrator on 2015/10/16. */ /** * 對(duì)稱加密 */ public class SythEncryptActivity extends Activity { private EditText txtContent; private EditText txtPassword; private EditText txtResult; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sythencrypylayout); txtContent = (EditText) findViewById(R.id.txt_content); txtPassword = (EditText) findViewById(R.id.txt_password); txtResult = (EditText) findViewById(R.id.txt_result); } /** * DES加密,要求密碼必須8個(gè)字節(jié),64bit長(zhǎng)度 byte[8] * @param view */ public void btnDESEncrypt(View view) { //獲取需要加密的內(nèi)容 String content = txtContent.getText().toString(); //獲取密鑰 String password = txtPassword.getText().toString(); //注意,加密,解密,秘鑰都需要是字節(jié)數(shù)組 byte[] keyData = password.getBytes(); //需要加密的內(nèi)容 byte[] contentData = content.getBytes(); if(keyData.length == 8) { byte[] encryptedData = EncryptUtil.des(Cipher.ENCRYPT_MODE, contentData, keyData); //獲取加密后的數(shù)據(jù)(記住是byte[]類型的),用Base64編碼 成可見(jiàn)的字符串形式 String s = Base64.encodeToString(encryptedData, Base64.NO_WRAP); //顯示加密后的內(nèi)容 txtResult.setText(s); } } /** * DES的解密 * @param view */ public void btnDESDecrypt(View view) { String encryptedStr = txtResult.getText().toString(); if(encryptedStr.length()>0){ String password = txtPassword.getText().toString(); //因?yàn)樵诩用芊椒ㄖ?,使用Base64對(duì)加密的內(nèi)容進(jìn)行編碼,要解密的時(shí)候需要Base64的解碼 byte[] encryptedData = Base64.decode(encryptedStr, Base64.NO_WRAP); byte[] keyData = password.getBytes(); //DES 要求 8個(gè)字節(jié) if(keyData.length == 8){ //形成原始數(shù)據(jù) byte[] decryptedData = EncryptUtil.des(Cipher.DECRYPT_MODE, encryptedData, keyData); txtResult.setText(new String(decryptedData)); } } } }
layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <EditText android:id="@+id/txt_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入內(nèi)容" /> <EditText android:id="@+id/txt_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="DES密鑰" android:text="12345678" android:inputType="textVisiblePassword" /> <EditText android:id="@+id/txt_result" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DES加密" android:onClick="btnDESEncrypt" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="DES解密" android:onClick="btnDESDecrypt" /> </LinearLayout>
工具類參考 四:加密代碼步驟
效果圖:
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
迅雷、快車、旋風(fēng)URL加密/解密工具:
http://tools.jb51.net/password/urlrethunder
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
java正則替換括號(hào)中的逗號(hào)實(shí)現(xiàn)示例
本文主要介紹了java正則替換括號(hào)中的逗號(hào)實(shí)現(xiàn)示例,主要介紹了兩種示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01SpringBoot超詳細(xì)講解@Enable*注解和@Import
這篇文章主要介紹了SpringBoot?@Enable*注解和@Import,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07JavaWeb簡(jiǎn)單用戶登錄注冊(cè)實(shí)例代碼(有驗(yàn)證碼)
這篇文章主要介紹了JavaWeb簡(jiǎn)單用戶登錄注冊(cè)實(shí)例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02SpringBoot讀取Resource目錄下文件的四種方式總結(jié)
在Spring?Boot項(xiàng)目中,經(jīng)常需要獲取resources目錄下的文件,這些文件可以包括配置文件、模板文件、靜態(tài)資源等,本文將介紹四種常用的方法來(lái)獲取resources目錄下的文件,需要的朋友可以參考下2023-08-08