一、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加密算法的加密解密
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 }
五、示例
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 }
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>
工具類參考 四:加密代碼步驟
效果圖:

相關知識:
