天天看點

淺談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對象,設定加密算法

Cipher cipher = Cipher.getInstance("DES");      

2、準備Key對象

  2.1 DES加密算法使用DESKeySpec類,構造方法參數需要為8個位元組的密碼

  建立DESKeySpec類對象

  參數為密鑰,8個位元組

DESKeySpec keySpec = new DESKeySpec(new byte[1,2,3,4,5,6,7,8]);      

  2.2 轉換成Key對象

SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("EDS");

SecretKey key = keyFactory.generateSecret(keySpec);      

 3.設定Cipher模式,加密/解密 ,參數一 :模式 ,參數二:Key對象,傳回位元組數組

Cipher.DECRYPT_MODE 解密      
Cipher.ENCRYPT_MODE 加密

cipher.init(Cipher.ENCRYPT_MODE,key);      

4.傳回加密結果,參數為加密内容

bytep[] ret = cipher.doFinal(data);      

因為對稱加密加密與解密是相逆的。是以解密步驟和加密步驟一樣,隻是cipher.init()的模式不同,是以我們可以寫一個工具類來進行DES加密算法的加密解密

淺談DES加密算法
淺談DES加密算法
1 /**
 2      * DES加密算法
 3      * @param mode     模式: 加密,解密
 4      * @param data     需要加密的内容
 5      * @param keyData  密鑰 8個位元組數組
 6      * @return         将内容加密後的結果也是byte[]格式的
 7      */
 8     public static byte[] des(int mode,byte[] data,byte[] keyData)
 9     {
10         byte[] ret = null;
11         //加密的内容存在并且密鑰存在且長度為8個位元組
12         if (data != null
13                 && data.length>0
14                 &&keyData!=null
15                 && keyData.length==8) {
16 
17             try {
18                 Cipher cipher = Cipher.getInstance("DES");
19 
20                 DESKeySpec keySpec = new DESKeySpec(keyData);
21 
22                 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
23 
24                 SecretKey key = keyFactory.generateSecret(keySpec);
25 
26                 cipher.init(mode, key);
27 
28                 ret = cipher.doFinal(data);
29 
30             } catch (NoSuchAlgorithmException e) {
31                 e.printStackTrace();
32             } catch (NoSuchPaddingException e) {
33                 e.printStackTrace();
34             } catch (IllegalBlockSizeException e) {
35                 e.printStackTrace();
36             } catch (BadPaddingException e) {
37                 e.printStackTrace();
38             } catch (InvalidKeySpecException e) {
39                 e.printStackTrace();
40             } catch (InvalidKeyException e) {
41                 e.printStackTrace();
42             }
43         }
44 
45         return ret;
46     }
47 
48     //DES 加密
49     public static byte[] desEncrypt(byte[] data,byte[] keyData){
50         return des(Cipher.ENCRYPT_MODE,data,keyData);
51     }
52     //DES 解密
53     public static byte[] desDecrypt(byte[] data,byte[] keyData){
54         return des(Cipher.DECRYPT_MODE,data,keyData);
55     }      

DES加密算法工具類

五、示例

淺談DES加密算法
淺談DES加密算法
1 package com.xqx.encrypsthow;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.util.Base64;
 6 import android.util.Log;
 7 import android.view.View;
 8 import android.widget.EditText;
 9 import android.widget.Toast;
10 import utils.EncryptUtil;
11 
12 import javax.crypto.*;
13 import javax.crypto.spec.DESKeySpec;
14 import java.security.InvalidKeyException;
15 import java.security.NoSuchAlgorithmException;
16 import java.security.spec.InvalidKeySpecException;
17 import java.util.Arrays;
18 
19 /**
20  * Created by Administrator on 2015/10/16.
21  */
22 
23 /**
24  * 對稱加密
25  */
26 public class SythEncryptActivity extends Activity {
27 
28     private EditText txtContent;
29     private EditText txtPassword;
30     private EditText txtResult;
31 
32 
33     @Override
34     public void onCreate(Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36         setContentView(R.layout.sythencrypylayout);
37 
38         txtContent = (EditText) findViewById(R.id.txt_content);
39         txtPassword = (EditText) findViewById(R.id.txt_password);
40         txtResult = (EditText) findViewById(R.id.txt_result);
41 
42 
43     }
44 
45     /**
46      * DES加密,要求密碼必須8個位元組,64bit長度 byte[8]
47      * @param view
48      */
49     public void btnDESEncrypt(View view) {
50 
51         //擷取需要加密的内容
52         String content = txtContent.getText().toString();
53         //擷取密鑰
54         String password = txtPassword.getText().toString();
55         //注意,加密,解密,秘鑰都需要是位元組數組
56         byte[] keyData = password.getBytes();
57         //需要加密的内容
58         byte[] contentData = content.getBytes();
59         if(keyData.length == 8) {
60             byte[] encryptedData = EncryptUtil.des(Cipher.ENCRYPT_MODE, contentData, keyData);
61             //擷取加密後的資料(記住是byte[]類型的),用Base64編碼 成可見的字元串形式
62             String s = Base64.encodeToString(encryptedData, Base64.NO_WRAP);
63             //顯示加密後的内容
64             txtResult.setText(s);
65         }
66 
67     }
68 
69     /**
70      * DES的解密
71      * @param view
72      */
73     public void btnDESDecrypt(View view) {
74         String encryptedStr = txtResult.getText().toString();
75         if(encryptedStr.length()>0){
76             String password = txtPassword.getText().toString();
77             //因為在加密方法中,使用Base64對加密的内容進行編碼,要解密的時候需要Base64的解碼
78             byte[] encryptedData = Base64.decode(encryptedStr, Base64.NO_WRAP);
79             byte[] keyData = password.getBytes();
80             //DES 要求 8個位元組
81             if(keyData.length == 8){
82                 //形成原始資料
83                 byte[] decryptedData = EncryptUtil.des(Cipher.DECRYPT_MODE, encryptedData, keyData);
84                 txtResult.setText(new String(decryptedData));
85             }
86 
87         }
88     }
89 }      

SythEncryptActivity.class

淺談DES加密算法
淺談DES加密算法
1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3               android:orientation="vertical"
 4               android:layout_width="match_parent"
 5               android:layout_height="match_parent">
 6     <EditText
 7             android:id="@+id/txt_content"
 8             android:layout_width="match_parent"
 9             android:layout_height="wrap_content"
10             android:hint="請輸入内容"
11             />
12 
13     <EditText
14             android:id="@+id/txt_password"
15             android:layout_width="match_parent"
16             android:layout_height="wrap_content"
17             android:hint="DES密鑰"
18             android:text="12345678"
19             android:inputType="textVisiblePassword"
20             />
21     <EditText
22             android:id="@+id/txt_result"
23             android:layout_width="match_parent"
24             android:layout_height="wrap_content"
25             />
26 
27     <Button
28             android:layout_width="wrap_content"
29             android:layout_height="wrap_content"
30             android:text="DES加密"
31             android:onClick="btnDESEncrypt"
32             />
33 
34     <Button
35             android:layout_width="wrap_content"
36             android:layout_height="wrap_content"
37             android:text="DES解密"
38             android:onClick="btnDESDecrypt"
39             />
40 
41 </LinearLayout>      

layout

工具類參考 四:加密代碼步驟

效果圖:

淺談DES加密算法

相關知識:

淺談RSA加密算法 淺談Base64編碼算法
上一篇: python調試