博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DES、3DES、AES加密方式
阅读量:6853 次
发布时间:2019-06-26

本文共 3196 字,大约阅读时间需要 10 分钟。

详见:

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] 

  1. public class MainActivity extends Activity implements OnClickListener {  

  2.   

  3.     private EditText des_key, des_src, des_dst;  

  4.     private Button btn_encode, btn_decode;  

  5.   

  6.     @Override  

  7.     protected void onCreate(Bundle savedInstanceState) {  

  8.         super.onCreate(savedInstanceState);  

  9.         setContentView(R.layout.activity_main);  

  10.         des_key = (EditText) findViewById(R.id.des_key);  

  11.         des_src = (EditText) findViewById(R.id.des_src);  

  12.         des_dst = (EditText) findViewById(R.id.des_dst);  

  13.         btn_encode = (Button) findViewById(R.id.btn_encode);  

  14.         btn_decode = (Button) findViewById(R.id.btn_decode);  

  15.         btn_encode.setOnClickListener(this);  

  16.         btn_decode.setOnClickListener(this);  

  17.     }  

  18.   

  19.     @Override  

  20.     public void onClick(View v) {  

  21.         String key_str = des_key.getText().toString();  

  22.         if (!TextUtils.isEmpty(key_str)) {  

  23.             try {  

  24.                 // DES不管长了短了,都变成八位  

  25.                 // AES 长度变为128 new SecretKeySpec(temp, "Aes");  

  26.                 // 3Des 长度变为24 new SecretKeySpec(temp, "Desced");  

  27.                 byte temp[] = new byte[8];  

  28.                 byte b[] = key_str.getBytes("UTF-8");  

  29.                 System.arraycopy(b, 0, temp, 0, Math.min(b.length, temp.length));  

  30.                 // Des只支持八位  

  31.                 SecretKey des_key = new SecretKeySpec(temp, "Des");  

  32.                 Cipher cipher = Cipher.getInstance("Des");  

  33.                 switch (v.getId()) {  

  34.                 case R.id.btn_encode:  

  35.                     String src_str = des_src.getText().toString();  

  36.                     if (!TextUtils.isEmpty(src_str)) {  

  37.                         cipher.init(Cipher.ENCRYPT_MODE, des_key);  

  38.                         byte[] bytes = cipher  

  39.                                 .doFinal(src_str.getBytes("UTF-8"));  

  40.                         // 是用Base64编码的二进制字节数组  

  41.                         des_dst.setText(Base64.encodeToString(bytes,  

  42.                                 Base64.DEFAULT));  

  43.                     }  

  44.                     break;  

  45.   

  46.                 case R.id.btn_decode:  

  47.                     String dst_str = des_dst.getText().toString();  

  48.                     if (!TextUtils.isEmpty(dst_str)) {  

  49.                         cipher.init(Cipher.DECRYPT_MODE, des_key);  

  50.                         // 是用Base64编码的二进制字节数组  

  51.                         byte[] bytes = cipher.doFinal(Base64.decode(dst_str,  

  52.                                 Base64.DEFAULT));  

  53.                         des_src.setText(new String(bytes, "UTF-8"));  

  54.                     }  

  55.                     break;  

  56.                 }  

  57.             } catch (Exception e) {  

  58.                 e.printStackTrace();  

  59.             }  

  60.         }  

  61.     }  

  62. }  

 

 

 

 

布局:

 

 

[java] 

  1. <span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  

  2.     xmlns:tools="http://schemas.android.com/tools"  

  3.     android:layout_width="match_parent"  

  4.     android:layout_height="match_parent"  

  5.     android:orientation="vertical" >  

  6.   

  7.     <EditText  

  8.         android:id="@+id/des_key"  

  9.         android:layout_width="wrap_content"  

  10.         android:layout_height="wrap_content"  

  11.         android:hint="秘钥" />  

  12.   

  13.     <EditText  

  14.         android:id="@+id/des_src"  

  15.         android:layout_width="wrap_content"  

  16.         android:layout_height="wrap_content"  

  17.         android:hint="原文" />  

  18.   

  19.     <EditText  

  20.         android:id="@+id/des_dst"  

  21.         android:layout_width="wrap_content"  

  22.         android:layout_height="wrap_content"  

  23.         android:hint="密文" />  

  24.   

  25.     <Button  

  26.         android:id="@+id/btn_encode"  

  27.         android:layout_width="wrap_content"  

  28.         android:layout_height="wrap_content"  

  29.         android:text="加密" />  

  30.   

  31.     <Button  

  32.         android:id="@+id/btn_decode"  

  33.         android:layout_width="wrap_content"  

  34.         android:layout_height="wrap_content"  

  35.         android:text="解密" />  

  36.   

  37. </LinearLayout></span>  

 

 

 

转载地址:http://yuyyl.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
关于Mac系统中SequelPro工具对于Mysql数值类型nt(M)存值的bug
查看>>
Linux下重置MySQL的Root帐号密码
查看>>
下一个目标-百度
查看>>
百度地图API学习之路(2)
查看>>
dell服务器硬盘的状态变成外来(foreign)
查看>>
redhat6.4更换centos 6 的 yum源
查看>>
jsquery问题
查看>>
深入了解android平台的jni---编译ffmpeg源码
查看>>
共享JSP部署后测试代码
查看>>
日常订阅的开发工具和服务——2018年
查看>>
linux下乱码问题及解决方式
查看>>
回车和换行有什么区别?很尴尬》》》
查看>>
Hibernate(十六)数据库事务与隔离级别
查看>>
laravel、lumen遇到的问题解决
查看>>
MYSQL-mysqlslap
查看>>
Cisco ASA5500解决内网用公网IP不能访问DMZ区服务器的
查看>>
Windows7常用命令
查看>>
crack-jar游戏之拉阔
查看>>
Java中的深拷贝和浅拷贝
查看>>