本文實例講述了java實現的DES加密算法。分享給大家供大家參考,具體如下:
一、DES加密算法介紹
1、要求密鑰必須是8個字節,即64bit長度
2、因為密鑰是byte[8] , 代表字符串也可以是非可見的字節,可以與Base64編碼算法一起使用
3、加密、解密都需要通過字節數組作為數據和密鑰進行處理
二、對稱加密
DES加密算法屬于對稱加密。
即利用指定的密鑰,按照密碼的長度截取數據,分成數據塊,和密鑰進行復雜的移位、算數運算或者數據處理等操作,形成只有特定的密碼才能夠解開的數據。 加密與解密用的是同一個密鑰
三、相關類
1、Cipher:
Java/Android要使用任何加密,都需要使用Cipher這個類
使用Cipher進行加密,解密處理,需要創建實例對象并初始化。采用工廠模式創建對象
Cipher cipher = Cipher.getInstance("算法名稱");
cipher.init(加密/解密模式,Key秒);
2、Key:
Key類是Java加密系統所有密碼的父類
3、SecretKeyFactory:
對于DES加密解密,使用SecretKeyFactory生成,生成時需指定DESKeySpec
四、加密代碼步驟
1. 獲取Cipher對象,設置加密算法
1
|
Cipher cipher = Cipher.getInstance( "DES" ); |
2、準備Key對象
2.1 DES加密算法使用DESKeySpec類,構造方法參數需要為8個字節的密碼
創建DESKeySpec類對象
參數為密鑰,8個字節
1
|
DESKeySpec keySpec = new DESKeySpec( new byte [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ]); |
2.2 轉換成Key對象
1
2
|
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( "EDS" ); SecretKey key = keyFactory.generateSecret(keySpec); |
3.設置Cipher模式,加密/解密 ,參數一 :模式 ,參數二:Key對象,返回字節數組
Cipher.DECRYPT_MODE 解密
Cipher.ENCRYPT_MODE 加密
1
|
cipher.init(Cipher.ENCRYPT_MODE,key); |
4.返回加密結果,參數為加密內容
1
|
bytep[] ret = cipher.doFinal(data); |
因為對稱加密加密與解密是相逆的。所以解密步驟和加密步驟一樣,只是cipher.init()的模式不同,所以我們可以寫一個工具類來進行DES加密算法的加密解密
DES加密算法工具類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
/** * DES加密算法 * @param mode 模式: 加密,解密 * @param data 需要加密的內容 * @param keyData 密鑰 8個字節數組 * @return 將內容加密后的結果也是byte[]格式的 */ public static byte [] des( int mode, byte [] data, byte [] keyData) { byte [] ret = null ; //加密的內容存在并且密鑰存在且長度為8個字節 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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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. */ /** * 對稱加密 */ 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個字節,64bit長度 byte[8] * @param view */ public void btnDESEncrypt(View view) { //獲取需要加密的內容 String content = txtContent.getText().toString(); //獲取密鑰 String password = txtPassword.getText().toString(); //注意,加密,解密,秘鑰都需要是字節數組 byte [] keyData = password.getBytes(); //需要加密的內容 byte [] contentData = content.getBytes(); if (keyData.length == 8 ) { byte [] encryptedData = EncryptUtil.des(Cipher.ENCRYPT_MODE, contentData, keyData); //獲取加密后的數據(記住是byte[]類型的),用Base64編碼 成可見的字符串形式 String s = Base64.encodeToString(encryptedData, Base64.NO_WRAP); //顯示加密后的內容 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(); //因為在加密方法中,使用Base64對加密的內容進行編碼,要解密的時候需要Base64的解碼 byte [] encryptedData = Base64.decode(encryptedStr, Base64.NO_WRAP); byte [] keyData = password.getBytes(); //DES 要求 8個字節 if (keyData.length == 8 ){ //形成原始數據 byte [] decryptedData = EncryptUtil.des(Cipher.DECRYPT_MODE, encryptedData, keyData); txtResult.setText( new String(decryptedData)); } } } } |
layout:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<? 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 = "請輸入內容" /> < 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 > |
工具類參考 四:加密代碼步驟
效果圖:
希望本文所述對大家java程序設計有所幫助。