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. HDU3001 Traveling (状压dp+三进制+Tsp问题总结)

    (1)这道题最多可以走两次,所以有0, 1, 2三种状态,所以我们要用三进制 如果要用三进制,就要自己初始化两个数组, 一个是3的n次方,一个是三进制数的第几位的数字是什么 void init() { ...

  2. C# 上位机的USB设备拔插检测

    我们做USB通信时,通信成功后,往往要检测USB设备的拔插状态,这里就USB拔插进行一下说明. 参考:https://www.imooc.com/article/17438 先说明一下,我这里只是用C ...

  3. PHP读取XML数据中CDATA内数值

    // 在开发过程中遇到对XML获取时候加载 CDATA 无法读取内部的数值(例如微信平台的返回值) $content = simplexml_load_string('<content>& ...

  4. jquery让 readOnly失效的方法

    re.attr("readOnly","true"); re.attr("readOnly",false); 注意 :false不能带引号

  5. GNU Linux中的SO_RCVLOWAT和SO_SNDLOWAT说明

    /*********************************************************************  * Author  : Samson  * Date   ...

  6. android 推断手机是否支持前置摄像头

    话不多说 直接上代码, @SuppressLint("NewApi") public static boolean isSupportFrontCamera() { if (!ha ...

  7. PHP的curl库代码使用

    欢迎訪问个人原创地址: http://www.phpthinking.com/archives/468 使用PHP的cURL库能够简单和有效地去抓网页. 你仅仅须要执行一个脚本.然后分析一下你所抓取的 ...

  8. SEO分享:关于SEO的十个问题

    想写的幽默一点,幽默细胞太少,想写的专业一点,又不够专业,结果就出现了这篇不伦不类的怪文,望海涵! 导读:前面写过一篇类似的文章,受到的评价好坏都有吧.有人说讲的没有实质性的内容,有些人抱怨回答的太过 ...

  9. ioctl.h 分析

    ioctl.h 分析 我自己画了个解析图...不要嫌弃丑啊.. . 哈哈 type The magic number. Just choose one number (after consulting ...

  10. JAVA网络编程--UDP通信

    首先网络传输数据需了解例如以下三点 1.找到对方IP 2.数据要发送到对方指定的应用程序上,为了标识这些应用程序,所以给这些网络应用程序用数字进行了标识.为了方便称呼这个数字,叫做port,逻辑por ...