作者:fuchen201101 | 来源:互联网 | 2023-10-12 21:55
1、aes加密工具类
上述就是android开发分享Android AES加密工具类分享的全部内容,如果对大家有所用处且需要了解更多关于Android学习教程,希望大家多多关注—编程笔记
java不支持pkcs7padding,只支持pkcs5padding。我们知道加密算法由算法+模式+填充组成,下一篇介绍ios和android通用的aes加密,本篇文章使用pkcs5padding加密方式。
package com.example.aesdemo; import java.io.unsupportedencodingexception; import javax.crypto.cipher; import javax.crypto.spec.secretkeyspec; ///** aes对称加密解密类 **/ public class aeshelper { // /** 算法/模式/填充 **/ private static final string ciphermode = "aes/ecb/pkcs5padding"; ///** 创建密钥 **/ private static secretkeyspec createkey(string password) { byte[] data = null; if (password == null) { password = ""; } stringbuffer sb = new stringbuffer(32); sb.append(password); while (sb.length() <32) { sb.append("0"); } if (sb.length() > 32) { sb.setlength(32); } try { data = sb.tostring().getbytes("utf-8"); } catch (unsupportedencodingexception e) { e.printstacktrace(); } return new secretkeyspec(data, "aes"); } // /** 加密字节数据 **/ public static byte[] encrypt(byte[] content, string password) { try { secretkeyspec key = createkey(password); system.out.println(key); cipher cipher = cipher.getinstance(ciphermode); cipher.init(cipher.encrypt_mode, key); byte[] result = cipher.dofinal(content); return result; } catch (exception e) { e.printstacktrace(); } return null; } ///** 加密(结果为16进制字符串) **/ public static string encrypt(string content, string password) { byte[] data = null; try { data = content.getbytes("utf-8"); } catch (exception e) { e.printstacktrace(); } data = encrypt(data, password); string result = byte2hex(data); return result; } // /** 解密字节数组 **/ public static byte[] decrypt(byte[] content, string password) { try { secretkeyspec key = createkey(password); cipher cipher = cipher.getinstance(ciphermode); cipher.init(cipher.decrypt_mode, key); byte[] result = cipher.dofinal(content); return result; } catch (exception e) { e.printstacktrace(); } return null; } ///** 解密16进制的字符串为字符串 **/ public static string decrypt(string content, string password) { byte[] data = null; try { data = hex2byte(content); } catch (exception e) { e.printstacktrace(); } data = decrypt(data, password); if (data == null) return null; string result = null; try { result = new string(data, "utf-8"); } catch (unsupportedencodingexception e) { e.printstacktrace(); } return result; } // /** 字节数组转成16进制字符串 **/ public static string byte2hex(byte[] b) { // 一个字节的数, stringbuffer sb = new stringbuffer(b.length * 2); string tmp = ""; for (int n = 0; n
2、使用
新建android工程
package com.example.aesdemo; import android.os.bundle; import android.app.activity; import android.view.menu; import android.util.log; public class mainactivity extends activity { protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string masterpassword = "a"; string originaltext = "于"; try { string encryptingcode = aeshelper.encrypt(originaltext,masterpassword); // system.out.println("加密结果为 " + encryptingcode); log.i("加密结果为 ",encryptingcode); string decryptingcode = aeshelper.decrypt(encryptingcode,masterpassword); // system.out.println("解密结果为 " + decryptingcode); log.i("解密结果",decryptingcode); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate the menu; this adds items to the action bar if it is present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
3、打印结果
09-19 10:41:05.467: i/加密结果为(707): e55c24701f6380478e1940addfd08d22 09-19 10:41:05.467: i/解密结果(707): 于