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?

  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] view plaincopyprint?

  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>  

 

 

 

posted on 2016-11-23 19:39  grefr  阅读(712)  评论(0编辑  收藏  举报

导航