一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java實現的DES加密算法詳解

java實現的DES加密算法詳解

2020-11-24 15:05聽著music睡 Java教程

這篇文章主要介紹了java實現的DES加密算法,結合實例形式詳細分析了java實現DES加密操作的原理、實現技巧與相關注意事項,需要的朋友可以參考下

本文實例講述了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實現的DES加密算法詳解

希望本文所述對大家java程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产亚洲精品视频中文字幕 | 亚洲美洲国产日产 | 欧美日韩一二三区免费视频观看 | 九九久久精品 | 精品卡1卡2卡三卡免费网站 | 草莓视频在线观看免费 | 香蕉精品 | 国产精品调教 | 成人免费在线视频 | 色婷婷网| 欧美最猛性xxxxx短视频 | 激情男人天堂 | 久久精品热在线观看85 | 天天做天天爽 | 双性人bbww欧美双性 | 女主被当众调教虐np | 国产精品亚洲一区二区久久 | 短篇小说肉| 日本中文字幕永久在线 | 555www成人网 | 天天操天天射天天爽 | 吃胸膜奶视频456 | 天天天综合网 | 国产精品一区三区 | 亚洲天堂2013| 色播影音先锋 | 波多野结衣中文字幕乱七八糟 | 国产欧美一区二区成人影院 | 精品香蕉99久久久久网站 | 免费370理论片中文字幕 | 娇妻与公陈峰姚瑶最新版 | 91yellow吧字幕网zmff7 | 精品无人区乱码1区2区3区免费 | 7个黑人玩北条麻妃 | 四虎成人国产精品视频 | 好大好硬好深好爽想要之黄蓉 | 国产亚洲精品自在线亚洲情侣 | 全彩调教侵犯h本子全彩妖气he | 亚洲人成毛片线播放 | 三级黄色片在线免费观看 | 精品一成人岛国片在线观看 |