DES 支持8位加密解密,3Des支持24位,Aes支持32位。3Des是Des算法做三次。位数的单位是字节byte。不是bits。

3Des是把24位分成3组。第一组八位用来加密,第二组8位用于解密,第三组8位用于加密,所以。假设秘钥为123456781234567812345678(3组1-8),则相当于做了一次12345678的Des加密。比如:第一次用12345678秘钥对123进行加密得到 "LDiFUdf0iew=",然后用第二组的12345678对其进行解密(逆向加密过程)。得到了123,第三次重新加密得到 "LDiFUdf0iew="。

三种加密方式代码里不同的地方:

byte temp[] = new byte[位数];

SecretKey des_key = new SecretKeySpec(temp, "加密算法");

加密算法名相应的是:Aes Des Desede

改了这两个地方就能够实现不同加密方式。

加密方式底层不一样。DES是把原文编程2进制的一串01。然后和密文做运算,交换什么什么位置。

public class MainActivity extends Activity implements OnClickListener {

	private EditText des_key, des_src, des_dst;
private Button btn_encode, btn_decode; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
des_key = (EditText) findViewById(R.id.des_key);
des_src = (EditText) findViewById(R.id.des_src);
des_dst = (EditText) findViewById(R.id.des_dst);
btn_encode = (Button) findViewById(R.id.btn_encode);
btn_decode = (Button) findViewById(R.id.btn_decode);
btn_encode.setOnClickListener(this);
btn_decode.setOnClickListener(this);
} @Override
public void onClick(View v) {
String key_str = des_key.getText().toString();
if (!TextUtils.isEmpty(key_str)) {
try {
// DES无论长了短了。都变成八位
// AES 长度变为128 new SecretKeySpec(temp, "Aes");
// 3Des 长度变为24 new SecretKeySpec(temp, "Desced");
byte temp[] = new byte[8];
byte b[] = key_str.getBytes("UTF-8");
System.arraycopy(b, 0, temp, 0, Math.min(b.length, temp.length));
// Des仅仅支持八位
SecretKey des_key = new SecretKeySpec(temp, "Des");
Cipher cipher = Cipher.getInstance("Des");
switch (v.getId()) {
case R.id.btn_encode:
String src_str = des_src.getText().toString();
if (!TextUtils.isEmpty(src_str)) {
cipher.init(Cipher.ENCRYPT_MODE, des_key);
byte[] bytes = cipher
.doFinal(src_str.getBytes("UTF-8"));
// 是用Base64编码的二进制字节数组
des_dst.setText(Base64.encodeToString(bytes,
Base64.DEFAULT));
}
break; case R.id.btn_decode:
String dst_str = des_dst.getText().toString();
if (!TextUtils.isEmpty(dst_str)) {
cipher.init(Cipher.DECRYPT_MODE, des_key);
// 是用Base64编码的二进制字节数组
byte[] bytes = cipher.doFinal(Base64.decode(dst_str,
Base64.DEFAULT));
des_src.setText(new String(bytes, "UTF-8"));
}
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}


布局:

<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/des_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="秘钥" /> <EditText
android:id="@+id/des_src"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="原文" /> <EditText
android:id="@+id/des_dst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="密文" /> <Button
android:id="@+id/btn_encode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加密" /> <Button
android:id="@+id/btn_decode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="解密" /> </LinearLayout></span>

[android]DES/3DES/AES加密方式的更多相关文章

  1. Java利用DES/3DES/AES这三种算法分别实现对称加密

    转载地址:http://blog.csdn.net/smartbetter/article/details/54017759 有两句话是这么说的: 1)算法和数据结构就是编程的一个重要部分,你若失掉了 ...

  2. Android数据加密之Aes加密

    前言: 项目中除了登陆,支付等接口采用rsa非对称加密,之外的采用aes对称加密,今天我们来认识一下aes加密. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes ...

  3. DES/3DES/AES 三种对称加密算法实现

    1. 简单介绍 3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES ...

  4. C#中使用DES和AES加密解密

    C#中使用DES和AES加密解密 2008-01-12 09:37 using System;using System.Text;using System.Security.Cryptography; ...

  5. DES、3DES、AES加密方式

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt165 DES 支持8位加密解密,3Des支持24位,Aes支持32位.3De ...

  6. 对加密的了解(DES/3DES/AES区别 )

    DES 1977年1月,美国政府颁布:采纳IBM公司设计的方案作为非机密数据的正式. 目前在国内,随着三金工程尤其是金卡工程的启动,DES算法在POS.ATM.磁卡及智能卡(IC卡).加油站.高速公路 ...

  7. java-信息安全(二)-对称加密算法DES,3DES,AES,Blowfish,RC2,RC4

    概述 信息安全基本概念: DES(Data Encryption Standard,数据加密标准) 3DES(Triple DES,三重数据加密算法(TDEA,Triple Data Encrypti ...

  8. DES/3DES/AES区别

    公元前400年,古希腊人发明了置换密码.1881年世界上的第一个电话保密专利出现.在第二次世界大战期间,德国军方启用“恩尼格玛”密码机,密码学在战争中起着非常重要的作用. DES 1977年1月,美国 ...

  9. android base64 和 aes 加密 解密

    package pioneerbarcode.ccw.com.encryptanddecode;import android.os.Bundle;import android.support.v7.a ...

随机推荐

  1. python--(爬虫-re模块)

    python--(爬虫-re模块) re模块四大核心功能: 1.findall 查找所有,返回list import re lst = re.findall("m", " ...

  2. css所有属性(table,行列组)总结

    概述: CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明: CSS声明总是以分号(;)结束,声明组以大括号({})括起来: 一.注释: CSS注释以 "/*" 开始, ...

  3. 【codeforces 235B】Let's Play Osu!

    [题目链接]:http://codeforces.com/problemset/problem/235/B [题意] 让你玩一个游戏,游戏结果由一个长度为n的01字符组成; 这个结果的分数与连续的1的 ...

  4. PatentTips - Object-oriented processor architecture and operating method

    BACKGROUND OF THE INVENTION The present invention relates to processors and computer systems. More s ...

  5. WinServer-AD操作常用powershell命令

    powershell 操作AD常用命令 查询AD中默认的密码策略 Get-ADDefaultDomainPasswordPolicy 查询AD中密码永不过期的用户 Get-ADUser -Filter ...

  6. 任务调度器quartz的使用

    1.quartz的获取. 可參照:Quartz任务调度模型实例 2.开发思路: 要使用定时器quartz.先弄清楚三个概念:调度器.任务.触发器.开发也是依照这三个方面来开发, 1>写一个Job ...

  7. GitHub客户端和Shell的基本操作和理解

    GitHub客户端和Shell指令的简单实用 客户端操作, web端操作, shell指令操作. 掌握了这三种操作,基本上就可以很好的运用gitHub了. 创建项目, 可以通过web端进行创建. 可以 ...

  8. zzuoj--10424--无聊的课(简单几何)

    10424: 无聊的课 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 81  Solved: 16 [Submit][Status][Web Boar ...

  9. Scala语言

    一.Scala概述 Scala简介 Scala是一种针对JVM将函数和面向对象技术组合在一起的编程语言.所以Scala必须要有JVM才能运行,和Python一样,Scala也是可以面向对象和面向函数的 ...

  10. utf8 string

    https://github.com/BassLC/idUTF8lib Idiot's UTF-8 Library A very (too much really) simple Utf8 libra ...