DES ,AES加密解密的实现
2017-05-06 15:33
495 查看
DES ,AES加密解密的实现:
MainActivity中:
布局: 俩个文本框分别输入内容和密码 俩个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; } }
相关文章推荐
- 【转】asp.net(c#)加密解密算法之sha1、md5、des、aes实现源码详解
- NET实现RSA AES DES 字符串 加密解密以及SHA1 MD5加密
- C#使用DES和AES实现加密解密功能示例
- java实现对称加密AES和DES的加密、解密
- C#实现DES加密解密,AES加密解密
- 用C#实现AES加密和解密
- as3.0利用DES和AES对数据进行加密和解密并且与php端加密匹配!
- python实现aes加密解密,RSA签名和验签,RSA加密解密,并调用接口
- C#中使用DES和AES加密解密
- javascript实现des解密加密全过程
- C#中使用DES和AES加密解密
- 使用C#实现AES加密解密
- C#实现AES加密和解密
- 用C#实现AES加密和解密
- C#实现AES加密解密
- .NET中的加密解密:私钥加密(对称加密):AES、DES、RC2、Rijindael、TripleDES
- python实现的AES双向对称加密解密与用法分析
- C#实现AES加密和解密函数原代码
- java实现AES加密解密
- C#实现AES加密和解密