[android]DES/3DES/AES加密方式
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加密方式的更多相关文章
- Java利用DES/3DES/AES这三种算法分别实现对称加密
转载地址:http://blog.csdn.net/smartbetter/article/details/54017759 有两句话是这么说的: 1)算法和数据结构就是编程的一个重要部分,你若失掉了 ...
- Android数据加密之Aes加密
前言: 项目中除了登陆,支付等接口采用rsa非对称加密,之外的采用aes对称加密,今天我们来认识一下aes加密. 其他几种加密方式: Android数据加密之Rsa加密 Android数据加密之Aes ...
- 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加密方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt165 DES 支持8位加密解密,3Des支持24位,Aes支持32位.3De ...
- 对加密的了解(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月,美国 ...
- android base64 和 aes 加密 解密
package pioneerbarcode.ccw.com.encryptanddecode;import android.os.Bundle;import android.support.v7.a ...
随机推荐
- 读取bin文件,并且按结构体赋值打印
目标:读取一个bin文件,并且将bin文件中的数据,按字节对齐赋值给结构体,并且打印出结构体的内容 目前思路是简单的先将bin文件数据一次性读到一个数组中,再将数组强制转换为结构体 ] FILE *f ...
- 06001_NoSQL概述
1.什么是NoSQL? NoSQL(NoSQL=Not Only SQL),意即“不仅仅是SQL”,是一项全新的数据库理念,泛指非关系型的数据库. 2.关于关系型数据库和nosql数据库 (1)关系型 ...
- Java String.replaceAll()方法
声明 以下是java.lang.String.replaceAll()方法的声明 public String replaceAll(String regex, String replacement) ...
- javascript-js常用插件集合
area.js 中国地区分级的js代码 Scripts/crypto.js CryptoJS (crypto.js) 为 JavaScript 提供了各种各样的加密算法 ...
- WinServer-IIS-FTP服务器
在设置FTP服务器的时候,注意SSL的设置,简单尝试的话,不需要启用SSL 常用FTP命令 ftp 进入FTP命令模式 open 192.168.10.6 (连接到FTP主机,或者open ...
- spring boot约定优于配置的这种做法在如今越来越流行了
约定优于配置的这种做法在如今越来越流行了,它的特点是简单.快速.便捷.但是这是建立在程序员熟悉这些约定的前提上.而 Spring 拥有一个庞大的生态体系,刚开始转到 Spring Boot 完全舍弃 ...
- 低效能的”where1=1”
网上有不少人提出过类似的问题:"看到有人写了where 1=1这种sql,究竟是什么意思?".事实上使用这种使用方法的开发者一般都是在使用动态数组的sql. 让我们想象例如以下的场 ...
- Python——异常基础
异常基础 在Python中,异常会依据错误自己主动地被触发.也能由代码触发和截获.异常由五个语句处理: 1.[try/except]:捕捉由Python或你引起的异常并恢复. 2.[try/final ...
- nova shelve 的使用
对于云中的资源我们常有例如以下需求 1,用户对临时不使用的VM进行停止操作.以节省费用. 2.对于长时间未使用的VM.管理员想要从hypervisor层面上清除它们从而节省主机资源. 3.但之前的停止 ...
- 苹果要求全部新app以及版本号更新必须支持iOS 8 SDK和64-bit
2014年10月20日.苹果官方公布了一则新闻,新闻内容例如以下: Starting February 1, 2015, new iOS apps uploaded to the App Store ...