DES、3DES、AES加密方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt165
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,然后和密文做运算,交换什么什么位置。
[java] view plaincopyprint?
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();
}
}
}
}
布局:
[java] view plaincopyprint?
<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>

DES、3DES、AES加密方式的更多相关文章
- [android]DES/3DES/AES加密方式
DES 支持8位加密解密,3Des支持24位,Aes支持32位.3Des是Des算法做三次.位数的单位是字节byte.不是bits. 3Des是把24位分成3组.第一组八位用来加密,第二组8位用于解密 ...
- Java利用DES/3DES/AES这三种算法分别实现对称加密
转载地址:http://blog.csdn.net/smartbetter/article/details/54017759 有两句话是这么说的: 1)算法和数据结构就是编程的一个重要部分,你若失掉了 ...
- DES/3DES/AES 三种对称加密算法实现
1. 简单介绍 3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称.它相当于是对每个数据块应用三次DES ...
- C#中使用DES和AES加密解密
C#中使用DES和AES加密解密 2008-01-12 09:37 using System;using System.Text;using System.Security.Cryptography; ...
- 对加密的了解(DES/3DES/AES区别 )
DES 1977年1月,美国政府颁布:采纳IBM公司设计的方案作为非机密数据的正式. 目前在国内,随着三金工程尤其是金卡工程的启动,DES算法在POS.ATM.磁卡及智能卡(IC卡).加油站.高速公路 ...
- java-信息安全(二)-对称加密算法DES,3DES,AES,Blowfish,RC2,RC4
概述 信息安全基本概念: DES(Data Encryption Standard,数据加密标准) 3DES(Triple DES,三重数据加密算法(TDEA,Triple Data Encrypti ...
- DES/3DES/AES区别
公元前400年,古希腊人发明了置换密码.1881年世界上的第一个电话保密专利出现.在第二次世界大战期间,德国军方启用“恩尼格玛”密码机,密码学在战争中起着非常重要的作用. DES 1977年1月,美国 ...
- 两种JavaScript的AES加密方式(可与Java相互加解密)
由于JavaScript属于弱类型脚本语言,因此当其与强类型的后台语言进行数据交互时会产生各种问题,特别是加解密的操作.本人由于工作中遇到用js与Java进行相互加解密的问题,在网上查了很多资料及代码 ...
- go加密算法:CBC对称加密(一)--3DES/AES
其实对称加密中的:DES\3DES\AES 采取的加解密步骤一致,只是小的细节不太一样.大家多看看就能写出来了 // rsao1.go package main import ( "byte ...
随机推荐
- mysql索引 索引优缺点
mysql索引索引优化 1.什么是索引?索引是对数据库表中一列或多列的值进行排序的一种结构,使用索引可快速访问数据库表中的特定信息. 2.作用是什么?索引的作用相当于图书的目录,可以根据目录中的页码快 ...
- C语言中全局变量存放在哪个位置?
今年软考的时候,遇到了这个题目,表示不解,然后考完之后去查了一下百度,才发现自己选错.全局变量存放在静态存储区,位置是固定的. 局部变量在栈空间,栈地址是不固定的.栈:就是那些由编译器在需要的时候分配 ...
- react入门之使用webpack搭配环境(一)
react入门之搭配环境(一) 如果你想直接上手开发,而跳过这些搭配环境的繁琐过程,推荐你使用官方的create-react-app命令 npm install -g create-react-app ...
- 【性能测试工具】- Http_Load
优点:参数少:缺点:url.txt文件要求较高,不如siege方便 http_load是ACME实验室开发的,这个工具以并发方式运行,用以测试web服务器的吞吐量与负载.它不同于其它压力测试工具的是, ...
- mysql对binlog的处理
--mysql对binlog的处理 ------------------------2014/05/28 Binlog是mysql以二进制形式打印的日志,它默认不加密,不压缩.每个正常的binlog文 ...
- 表空间移动(transporting tablespaces)
--表空间移动(transporting tablespaces) --------------------------------------2014/01/15 1. 表空间传输步骤简介. ...
- Java字符串操作
最近翻看之前的东西,发现有些看似简单的东西竟然忘的差不多了,记录一下对字符串大小写转换的操作. 打印结果
- MFC基础窗口创建,CWinApp、CFrameWnd
1.CWinApp(包括了这个类的导出类):代表了我们的程序.封装了消息循环等. 2.CFrameWnd:代表了程序的框架窗口.封装了窗口的注册.创建.显示.刷新.等等窗口操作. 3.Win32中.一 ...
- shell学习笔记(一)
1.第一行必须以#!/bin/bash #!表示脚本使用后面的解释器解释执行 2.echo 打印输出 例如 echo "hello world" > aa.txt 3.接 ...
- 神经网络与深度学习笔记 Chapter 2.
转载请注明出处http://www.cnblogs.com/zhangcaiwang/p/6886037.html 以前都没有正儿八经地看过英文类文档,神经网络方面又没啥基础,结果第一章就花费了我将近 ...