一、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[,,,,,,,]);

  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加密算法
* @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>
&&keyData!=null
&& keyData.length==) { 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);
}

DES加密算法工具类

五、示例

 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 == ) {
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()>){
String password = txtPassword.getText().toString();
//因为在加密方法中,使用Base64对加密的内容进行编码,要解密的时候需要Base64的解码
byte[] encryptedData = Base64.decode(encryptedStr, Base64.NO_WRAP);
byte[] keyData = password.getBytes();
//DES 要求 8个字节
if(keyData.length == ){
//形成原始数据
byte[] decryptedData = EncryptUtil.des(Cipher.DECRYPT_MODE, encryptedData, keyData);
txtResult.setText(new String(decryptedData));
} }
}
}

SythEncryptActivity.class

 <?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=""
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>

layout

工具类参考 四:加密代码步骤

效果图:

相关知识:

浅谈RSA加密算法

浅谈Base64编码算法

最新文章

  1. 一步步开发自己的博客 .NET版(10、前端对话框和消息框的实现)
  2. c++语言友元函数和成员函数对运算符重载
  3. case when 用法
  4. 9月12日JavaScript脚本语言
  5. Cannot modify header information - headers already sent by
  6. hdu3732(多重背包)
  7. 使用Docker运行Microsoft SQL Server 2017
  8. grep、awk、sed的巩固练习
  9. [Swift]LeetCode1024. 视频拼接 | Video Stitching
  10. redis 集群搭建
  11. codeforce 139E
  12. c 链表之 快慢指针 查找循环节点
  13. [转载] Web Service工作原理及实例
  14. SQL函数汇总(MySQL教材)
  15. static 和 final
  16. Python学习-31.Python中集合的一些操作
  17. Memcached (第一篇)
  18. UITableView 让 cell 被选中的颜色底色快速消失,而不是一直停留在cell上
  19. C/C++ 语言获取文件大小
  20. Git----时光穿梭机之工作区和暂存区03

热门文章

  1. 【cs229-Lecture17】离散与维数灾难
  2. SQL语句中将Datetime类型转换为字符串类型
  3. When to use dequeueReusableCellWithIdentifier vs dequeueReusableCellWithIdentifier: forIndexPath
  4. 【转载】Linux下动态共享库加载时的搜索路径详解
  5. 【转载】dirs、pushd、popd指令
  6. SQL Server 2008 各种DateTime的取值范围
  7. Linux - 常见Shell文本处理方法
  8. Mysql学习笔记(六)增删改查
  9. MVC,如何在视图中声明方法,调用方法?
  10. C#接口知识大全收藏