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,然后和密文做运算,交换什么什么位置。

[java]  view plain copy print ?
  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 plain copy print ?
  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>  




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值