一、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>
工具类参考 四:加密代码步骤
效果图:
相关知识:
浅谈RSA加密算法
浅谈Base64编码算法