Android——AES加密解密

来源:互联网 发布:ipad程序加密软件 编辑:程序博客网 时间:2024/06/14 00:08

AES:高级加密标准英语Advanced Encryption Standard缩写AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称密钥加密中最流行的算法之一。

aes经常用于网络间的传输,比DES加密更安全。

直接上代码,我已经整理了一下,copy可以直接使用

import java.security.SecureRandom;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.spec.SecretKeySpec;/** * AES加密解密类(密钥长度为128比特,不支持密钥长度192/256比特。) *  * @author luckchoudog */public class AesEncryption {private final static String HEX = "0123456789ABCDEF";/** * 加密字符串 *  * @param seed *                密钥 * @param clearText *                需要加密的字符串 * @return 加密后的字符串 * @throws Exception *                 直接抛出异常 */public static String encrypt(String seed, String clearText) throws Exception {byte[] rawKey = getRawKey(seed.getBytes());byte[] result = encrypt(rawKey, clearText.getBytes());return toHex(result);}/** * 加密字节流 *  * @param seed *                密钥 * @param clearText *                需要加密的byte数组 * @return 加密后的byte数组 * @throws Exception *                 直接抛出异常 */public static byte[] encryptByte(byte[] seed, byte[] clearText) throws Exception {byte[] rawKey = getRawKey(seed);return encrypt(rawKey, clearText);}/** * 解密字符串 *  * @param seed *                密钥 * @param encrypted *                需要解密的字符串 * @return 解密后的字符串 * @throws Exception *                 直接抛出异常 */public static String decrypt(String seed, String encrypted) throws Exception {byte[] rawKey = getRawKey(seed.getBytes());byte[] enc = toByte(encrypted);byte[] result = decrypt(rawKey, enc);return new String(result);}/** * 解密字节流 *  * @param seed *                密钥 * @param encrypted *                需要解密的byte数组 * @return 解密后的byte数组 * @throws Exception *                 直接抛出异常 */public static byte[] decryptByte(byte[] seed, byte[] encrypted) throws Exception {byte[] rawKey = getRawKey(seed);return decrypt(rawKey, encrypted);}private static byte[] getRawKey(byte[] seed) throws Exception {KeyGenerator kgen = KeyGenerator.getInstance("AES");SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");sr.setSeed(seed);kgen.init(128, sr); // 192 and 256 bits may not be availablereturn kgen.generateKey().getEncoded();}public static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.ENCRYPT_MODE, skeySpec);return cipher.doFinal(clear);}public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");Cipher cipher = Cipher.getInstance("AES");cipher.init(Cipher.DECRYPT_MODE, skeySpec);return cipher.doFinal(encrypted);}public static String toHex(String txt) {return toHex(txt.getBytes());}public static String fromHex(String hex) {return new String(toByte(hex));}public static byte[] toByte(String hexString) {int len = hexString.length() / 2;byte[] result = new byte[len];for (int i = 0; i < len; i++)result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();return result;}public static String toHex(byte[] buf) {if (buf == null)return "";StringBuffer result = new StringBuffer(2 * buf.length);int size = buf.length;for (int i = 0; i < size; i++) {appendHex(result, buf[i]);}return result.toString();}private static void appendHex(StringBuffer sb, byte b) {sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));}}
下边这个是供开发时直接使用的,上边的代码基本不用修改,直接拿去用jiuok了

import com.luckchoudog.base.Configs;//我将一些控制全局的变量存放在一个配置文件中import com.luckchoudog.utils.Logger;/** * AES加密解密工具类,AesEncryptionHelper供外部调用,一般使用的是此类 *  * @author luckchoudog */public class AesEncryptionHelper {private static String TAG = AesEncryptionHelper.class.getSimpleName();/** * 解密字符串 *  * @param msg *                需要解密的字符串 * @return String 解密之后的字符串 */public static String AesDecryption(String msg) {Logger.d(TAG, "AesDecryption ~ start");String decryptionMsg = "";try {decryptionMsg = AesEncryption.decrypt(Configs.ENCRYPTIONPASSWORD, msg);} catch (Exception e) {e.printStackTrace();Logger.d(TAG, "AesDecryption ~ end", e);}Logger.d(TAG, "AesDecryption ~ end");return decryptionMsg;}/** * 解密数据流 *  * @param buf *                需要解密的数据流 * @return byte[] */public static byte[] AesDecryption(byte[] buf) {Logger.d(TAG, "AesDecryption ~ start");try {return AesEncryption.decryptByte(Configs.ENCRYPTIONPASSWORDBYTE, buf);} catch (Exception e) {e.printStackTrace();Logger.d(TAG, "AesDecryption ~ end", e);}Logger.d(TAG, "AesDecryption ~ end");return null;}/** * 加密字符串 *  * @param msg *                需要加密的字符串 * @return String 加密之后的字符串 */public static String AesEncryption(String msg) {Logger.d(TAG, "AesEncryption ~ start");String encryptionMsg = "";try {encryptionMsg = AesEncryption.encrypt(Configs.ENCRYPTIONPASSWORD, msg);} catch (Exception e) {e.printStackTrace();Logger.d(TAG, "AesEncryption ~ end", e);}Logger.d(TAG, "AesEncryption ~ end");return encryptionMsg;}/** * 加密数据流 *  * @param buf *                需要加密的流 * @return byte[] 加密之后的流 */public static byte[] AesEncryption(byte[] buf) {Logger.d(TAG, "AesEncryption ~ start");try {return AesEncryption.encryptByte(Configs.ENCRYPTIONPASSWORDBYTE, buf);} catch (Exception e) {e.printStackTrace();Logger.d(TAG, "AesEncryption ~ end", e);}Logger.d(TAG, "AesEncryption ~ end");return null;}}


2 0