android中AES加解密的使用方法

今天在android项目中使用AES对数据进行加解密,遇到了很多问题,网上也找了很多资料,也不行。不过最后还是让我给搞出来了,这里把这个记录下来,不要让别人走我的弯路,因为网上绝大多数的例子都是行不通的。好了,接下来开始讲解

1、Aes工具类

package com.example.cheng.aesencrypt; import android.text.TextUtils; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; /** * class description here * * @author cheng * @version 1.0.0 * @since 2016-11-02 */ public class Aes { private static final String SHA1PRNG = "SHA1PRNG"; // SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法 private static final String IV = "qws871bz73msl9x8"; private static final String AES = "AES"; //AES 加密 private static final String CIPHERMODE = "AES/CBC/PKCS5Padding"; //algorithm/mode/padding /** * 加密 */ public static String encrypt(String key, String cleartext) { if (TextUtils.isEmpty(cleartext)) { return cleartext; } try { byte[] result = encrypt(key, cleartext.getBytes()); return parseByte2HexStr(result); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 加密 */ public static byte[] encrypt(String key, byte[] clear) throws Exception { byte[] raw = getRawKey(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(raw, AES); Cipher cipher = Cipher.getInstance(CIPHERMODE); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byte[] encrypted = cipher.doFinal(clear); return encrypted; } /** * 解密 */ public static String decrypt(String key, String encrypted) { if (TextUtils.isEmpty(encrypted)) { return encrypted; } try { byte[] enc = parseHexStr2Byte(encrypted); byte[] result = decrypt(key, enc); return new String(result); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 解密 */ public static byte[] decrypt(String key, byte[] encrypted) throws Exception { byte[] raw = getRawKey(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(raw, AES); Cipher cipher = Cipher.getInstance(CIPHERMODE); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; } /** * 生成随机数,可以当做动态的密钥 * 加密和解密的密钥必须一致,不然将不能解密 */ public static String generateKey() { try { SecureRandom secureRandom = SecureRandom.getInstance(SHA1PRNG); byte[] key = new byte[20]; secureRandom.nextBytes(key); return toHex(key); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } /** * 对密钥进行处理 */ public static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance(AES); //for android SecureRandom sr = null; // 在4.2以上版本中,SecureRandom获取方式发生了改变 if (android.os.Build.VERSION.SDK_INT >= 17) { sr = SecureRandom.getInstance(SHA1PRNG, "Crypto"); } else { sr = SecureRandom.getInstance(SHA1PRNG); } // for Java // secureRandom = SecureRandom.getInstance(SHA1PRNG); sr.setSeed(seed); kgen.init(128, sr); //256 bits or 128 bits,192bits //AES中128位密钥版本有10个加密循环,192比特密钥版本有12个加密循环,256比特密钥版本则有14个加密循环。 SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; } /** * 二进制转字符 */ public static String toHex(byte[] buf) { if (buf == null) return ""; StringBuffer result = new StringBuffer(2 * buf.length); for (int i = 0; i < buf.length; i++) { appendHex(result, buf[i]); } return result.toString(); } private static void appendHex(StringBuffer sb, byte b) { sb.append(IV.charAt((b >> 4) & 0x0f)).append(IV.charAt(b & 0x0f)); } /** * 将二进制转换成16进制 * * @param buf * @return */ public static String parseByte2HexStr(byte buf[]) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } /** * 将16进制转换为二进制 * * @param hexStr * @return */ public static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } }

2、mainActivity和layout文件如下:

package com.example.cheng.aesencrypt; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText mInputET; private TextView mShowEncryputTV; private TextView mShowInputTV; private static final String PASSWORD_STRING = "12345678"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mInputET = (EditText) findViewById(R.id.ase_input); mShowEncryputTV = (TextView) findViewById(R.id.show_oringe_encrypt); mShowInputTV = (TextView) findViewById(R.id.show_ase_encrypt); } /** * 加密 * * @param view */ public void encrypt(View view) { String inputString = mInputET.getText().toString().trim(); if (inputString.length() == 0) { Toast.makeText(this, "请输入要加密的内容", Toast.LENGTH_SHORT).show(); return; } String encryStr = Aes.encrypt(PASSWORD_STRING, inputString); mShowInputTV.setText(encryStr); } /** * 解密 * * @param view */ public void decrypt(View view) { String encryptString = mShowInputTV.getText().toString().trim(); if (encryptString.length() == 0) { Toast.makeText(this, "解密字符串不能为空", Toast.LENGTH_SHORT).show(); return; } String decryStr = Aes.decrypt(PASSWORD_STRING, encryptString); mShowEncryputTV.setText(decryStr); } }

layout文件

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.cheng.aesencrypt.MainActivity"> <EditText android:id="@+id/ase_input" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="输入要加密的内容" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="encrypt" android:text="点击进行ASE加密" /> <TextView android:id="@+id/show_ase_encrypt" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="显示加密后的内容" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="decrypt" android:text="点击进行ASE解密" /> <TextView android:id="@+id/show_oringe_encrypt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="显示加密后的内容" /> </LinearLayout>

3、最后的效果如下:

1)、是一个输入框,输入钥加密的字符串;

2)、点击“AES加密”按钮后生产的加密字符串;

3)、点击“AES解密”按钮后,对加密字符串进行解密,然后在3处看到解密后的字符串,可以看到加密字符串和解密字符串相同,所以AES加解密成功了

4、总结

要用真机测试,模拟器是不行的,具体原因没去研究;
点击获取本例的github地址:
也可以通过android studio直接git下来,git地址为https://github.com/chenguo4930/AndroidAES.git
其中也还有DES、RSA的加解密demo的github地址为https://github.com/chenguo4930/EncodeDemo
git地址为: https://github.com/chenguo4930/EncodeDemo.git

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

时间: 2024-08-31 20:26:43

android中AES加解密的使用方法的相关文章

android中AES加解密的使用方法_Android

今天在android项目中使用AES对数据进行加解密,遇到了很多问题,网上也找了很多资料,也不行.不过最后还是让我给搞出来了,这里把这个记录下来,不要让别人走我的弯路,因为网上绝大多数的例子都是行不通的.好了,接下来开始讲解 1.Aes工具类 package com.example.cheng.aesencrypt; import android.text.TextUtils; import java.security.NoSuchAlgorithmException; import java.

PHP和.net中des加解密的实现方法_php实例

php5.x版本,要添加php扩展php_mcrypt. PHP版: 复制代码 代码如下: class STD3Des {     private $key = "";     private $iv = "";      /**     * 构造,传递二个已经进行base64_encode的KEY与IV     *     * @param string $key     * @param string $iv     */     function __cons

myeclipse-Java——AES在Android studio下加解密问题

问题描述 Java--AES在Android studio下加解密问题 我在myeclipse下使用AES加解密算法,可以运行并没有正确加解密,可是同样的代码放到Android studio下却只能加密,解密出来都是空值,请问有人知道为什么吗? 解决方案 不会吧!同一段加密代码应该没问题,毕竟运行环境对这没那么大影响

AES加解密在php接口请求过程中的应用示例_php实例

在php请求接口的时候,我们经常需要考虑的一个问题就是数据的安全性,因为数据传输过程中很有可能会被用fillder这样的抓包工具进行截获.一种比较好的解决方案就是在客户端请求发起之前先对要请求的数据进行加密,服务端api接收到请求数据后再对数据进行解密处理,返回结果给客户端的时候也对要返回的数据进行加密,客户端接收到返回数据的时候再解密.因此整个api请求过程中数据的安全性有了一定程度的提高. 今天结合一个简单的demo给大家分享一下AES加解密技术在php接口请求中的应用. 首先,准备一个AE

AES加解密在php接口请求过程中的应用示例

在php请求接口的时候,我们经常需要考虑的一个问题就是数据的安全性,因为数据传输过程中很有可能会被用fillder这样的抓包工具进行截获.一种比较好的解决方案就是在客户端请求发起之前先对要请求的数据进行加密,服务端api接收到请求数据后再对数据进行解密处理,返回结果给客户端的时候也对要返回的数据进行加密,客户端接收到返回数据的时候再解密.因此整个api请求过程中数据的安全性有了一定程度的提高. 今天结合一个简单的demo给大家分享一下AES加解密技术在php接口请求中的应用. 首先,准备一个AE

Android Rsa数据加解密的介绍与使用示例_Android

Rsa加密 RSA是目前最有影响力的公钥加密算法,RSA也是第一个既能用于数据加密也能用于数字签名的算法.该算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困 难,因此可以将乘积公开作为加密密钥,即公钥,而两个大素数组合成私钥.公钥是可发布的供任何人使用,私钥则为自己所有,供解密之用. RSA算法原理      1.随机选择两个大质数p和q,p不等于q,计算N=pq:      2.选择一个大于1小于N的自然数e,e必须与(p-1)(q-1)互素.

Android中如何加载数据缓存_Android

最近app快完工了,但是很多列表加载,新闻咨询等数据一直从网络请求,速度很慢,影响用户体验,所以寻思用缓存来加载一些更新要求不太高的数据 首先做一个保存缓存的工具类 import java.io.File; import java.io.IOException; import android.content.Context; import android.os.Environment; import android.util.Log; /** * 缓存工具类 */ public class Co

Android开发之加载图片的方法_Android

本文实例讲述了Android开发之加载图片的方法.分享给大家供大家参考.具体分析如下: 加载网络上的图片需要在manifest中配置访问网络的权限,如下: <uses-permission android:name="android.permission.INTERNET" /> 如果不配置这个权限的话,会报错:unknown host exception. package com.example.loadimgfromweb; import java.io.InputSt

Android实现滑动加载数据的方法_Android

本文实例讲述了Android实现滑动加载数据的方法.分享给大家供大家参考.具体实现方法如下: EndLessActivity.java如下: package com.ScrollListView; import Android.app.ListActivity; import Android.os.Bundle; import Android.view.Gravity; import Android.view.View; import Android.view.ViewGroup; import