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. springboot启动嵌入式tomcat报错找不到jar包,关键字:FileNotFoundException,derbyLocale_cs.jar,StandardJarScanner.scan

    异常: java.io.FileNotFoundException: /Users/lanhuajian/.m2/repository/org/apache/derby/derby/10.13.1.1 ...

  2. HDU 4320 Contest 3

    只需A的全部质因数包含在B中即可. #include <iostream> #include <cstdio> #define LL __int64 #include < ...

  3. 译:MySQL性能优化的21条最佳经验

    今天,数据库的操作越来越成为整个应用的性能瓶颈了,这点对于Web应用尤其明显.关于数据库的性能,这并不只是DBA才需要担心的事,而这更是我们程序员需要去关注的事情.当我们去设计数据库表结构,对操作数据 ...

  4. Coding上部署Ghost博客

    Ghost构建于Node.js平台之上.支持0.10.*版本号的Node.js. 在你的本地计算机上执行Ghost事实上非常easy,前提是你已经安装了Node.js. 什么是Node.js? 略过 ...

  5. 阅读《Android 从入门到精通》(10)——单项选择

    单项选择(RadioGroup) RadioGroup 是 LinearLayout 的子类,继承关系例如以下: android.view.ViewGroup android.widget.Linea ...

  6. Wireshark默认不抓取本地包的解决方式

    事实上这个工具我已经用过非常多年了,还叫Ethereal的时候就在用. 今天因为实验须要,要抓一下在localhost间的包,结果发现获取不到.解决方法也非常easy,在cmd下输入: route a ...

  7. apiCloud中Frame框的操作,显示与隐藏Frame

    Frame是一层一层的概念, 有的位于上层,有的位于下层. 1.加载菜单 2.加载页面层 3.首页拆分出内容层,这个时候内容层位于页面层的上方,当点击其他页面的时候,内容层遮挡住了他们 解决方案一 判 ...

  8. Git 跟 GitHub 是什么关系?

    Git 跟 GitHub 是什么关系? 大概就是「魔兽争霸」与「对战平台」的关系吧. git是一个版本控制工具github是一个用git做版本控制的项目托管平台. git是一个版本管理工具,githu ...

  9. Codeforces 703D Mishka and Interesting sum 离线+树状数组

    链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...

  10. Gulp 相关

    获取执行在文件列表: http://www.thinksaas.cn/ask/question/21950/ 用through2这个插件. var through = require('through ...