DES ,AES加密解密的实现

来源:互联网 发布:软件开发基础教程pdf 编辑:程序博客网 时间:2024/06/04 17:53

DES ,AES加密解密的实现:

布局: 俩个文本框分别输入内容和密码      俩个Button加密,解密      一个TextView显示加密,解密后的内容      <?xml version="1.0" encoding="utf-8"?><LinearLayout    android:orientation="vertical"    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.subang.advancework.MainActivity">   <EditText       android:id="@+id/editxt_content"       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:hint="请输入加密内容"/>    <EditText        android:id="@+id/editxt_pwd"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入加密密码"/>    <Button        android:id="@+id/btn1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="加密"        android:onClick="secret"/>    <Button        android:id="@+id/btn2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="解密"        android:onClick="secret"/>    <TextView        android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="数据"        /></LinearLayout>

MainActivity中:

   package com.example.subang.advancework;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.util.Base64;import android.view.View;import android.widget.EditText;import android.widget.TextView;import java.security.InvalidKeyException;import java.security.Key;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.SecretKeySpec;public class MainActivity extends AppCompatActivity {    private TextView text;    private EditText content,pwd;    private String trim_content;    private String trim_pwd;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        text= (TextView) findViewById(R.id.text);        content= (EditText) findViewById(R.id.editxt_content);        pwd= (EditText) findViewById(R.id.editxt_pwd);    }    public void secret(View view) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {        switch(view.getId()){           case R.id.btn1:               //trim 取消字符串空格               trim_content = content.getText().toString().trim();               trim_pwd = pwd.getText().toString().trim();               //加密               String data =  encryption(trim_content, trim_pwd);               text.setText(data);               break;            case R.id.btn2:                //解密 的内容为加密之后的内容 直接从text得到                String trim = text.getText().toString().trim();                String data2 = decryption(trim, trim_pwd);                text.setText(data2);                break;           default:              break;        }    }   //    private String decryption(String trim_content, String trim_pwd) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {          if(!TextUtils.isEmpty(trim_content)){              byte[] decode = Base64.decode(trim_content, Base64.DEFAULT);              try {                  //具体流程和加密差不多 只是个别颠倒了一下                  Cipher cipher=Cipher.getInstance("DES");                  byte[] bytes = trim_pwd.getBytes();                  if(bytes.length==8){                      Key key=new SecretKeySpec(bytes,"DES");                      cipher.init(Cipher.DECRYPT_MODE,key);                  }else {                      return  null;                  }                  byte[] bytes1 = cipher.doFinal(decode);                  String content_text= new String(bytes1);                  return content_text;              } catch (NoSuchAlgorithmException e) {                  e.printStackTrace();              } catch (NoSuchPaddingException e) {                  e.printStackTrace();              }          }        return  null;    }    private String  encryption(String trim_content, String trim_pwd) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException {        try {            //加密引擎            Cipher cipher=Cipher.getInstance("DES");            //将字符串转化为字节数组            byte[] bytes = trim_pwd.getBytes();            //DES加密密码长度为8  3DES(DESede)为24, AES为16            if(bytes.length==8){                // SecretKeySpec:可以使用此类来根据一个字节数组构造一个 SecretKey,                // 而无须通过一个(基于 provider 的)SecretKeyFactory。                //此类仅对能表示为一个字节数组并且没有任何与之相关联的钥参数的原始密钥有用,                // 如,DES 或者 Triple DES 密钥。                //加密                Key key=new SecretKeySpec(bytes,"DES");                cipher.init(Cipher.ENCRYPT_MODE,key);            }else {                return null;            }            //判断文本框输入内容是否为空            if(!TextUtils.isEmpty(trim_content)){                byte[] bytes1 = cipher.doFinal(trim_content.getBytes());                String s = Base64.encodeToString(bytes1, Base64.DEFAULT);                return s;            }        } catch (NoSuchAlgorithmException e) {            e.printStackTrace();        } catch (NoSuchPaddingException e) {            e.printStackTrace();        }        return null;    }}

0 0
原创粉丝点击