android AES加密工具类(实测兼容所有版本,靠谱)

import android.annotation.SuppressLint;
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;

/**
 *
 *
 * Author:sunger
 */
public class AESUtils {

	public static String encrypt(String seed, String cleartext)
			throws Exception {

		byte[] rawKey = getRawKey(seed.getBytes());

		byte[] result = encrypt(rawKey, cleartext.getBytes());

		return toHex(result);

	}

	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);

	}

	@SuppressLint("TrulyRandom")
	private static byte[] getRawKey(byte[] seed) throws Exception {

		KeyGenerator kgen = KeyGenerator.getInstance("AES");

		SecureRandom sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");

		sr.setSeed(seed);

		kgen.init(128, sr); // 192 and 256 bits may not be available

		SecretKey skey = kgen.generateKey();

		byte[] raw = skey.getEncoded();

		return raw;

	}

	private 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, new IvParameterSpec(
				new byte[cipher.getBlockSize()]));

		byte[] encrypted = cipher.doFinal(clear);

		return encrypted;

	}

	private 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, new IvParameterSpec(
				new byte[cipher.getBlockSize()]));

		byte[] decrypted = cipher.doFinal(encrypted);

		return decrypted;

	}

	private 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;

	}

	private 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 final static String HEX = "0123456789ABCDEF";

	private static void appendHex(StringBuffer sb, byte b) {

		sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));

	}

}

时间: 2024-10-25 05:54:52

android AES加密工具类(实测兼容所有版本,靠谱)的相关文章

Android AES加密工具类实现(基础回顾)

1 package com.powercreator.cms.util; 2 3 import java.security.SecureRandom; 4 import javax.crypto.Cipher; 5 import javax.crypto.KeyGenerator; 6 import javax.crypto.SecretKey; 7 import javax.crypto.spec.IvParameterSpec; 8 import javax.crypto.spec.Secr

Java AES 加密工具类

package com.microwisdom.utils; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.logging.Level; import java.util.logging.Logger; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import jav

Java MD5,base64,AES加密工具类

import java.io.UnsupportedEncodingException; import java.math.BigInteger; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.regex.Matcher; import java.util.regex.Pat

AES加密工具类

import it.sauronsoftware.base64.Base64; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; /** DES 加密的一种,加密秘钥长度为16位. */ public class AESC { private static final String ALGORITHM = "AES"; /** * 16位的key. */ private static final St

wemall app商城源码android开发MD5加密工具类

wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享android开发MD5加密工具类主要代码,供技术员参考学习. package com.gzcivil.utils; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgori

android开发MD5加密工具类(一)

MD5加密工具类整理: 1 package com.gzcivil.utils; 2 3 import java.io.UnsupportedEncodingException; 4 import java.security.MessageDigest; 5 import java.security.NoSuchAlgorithmException; 6 7 public class MD5Tool { 8 9 public static String md5(String string) {

android加密工具类

import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * 加密工具类 * Created by Administrator on 2015/10/21 0021. */ public class EncryptUtils { /** * 字符串加密使用MD5算法 */ public final static String encryptMD5(String source) {

Android常用的工具类

主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java.目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.PreferencesUtils.JSONUtils.FileUtils.ResourceUtils.StringUtils.ParcelUtils.RandomUtils.ArrayUtils.ImageUtils.ListUtils.MapUtils.ObjectUtils.SerializeUtils.S

一个使用命令行编译Android项目的工具类

一个使用命令行编译Android项目的工具类 简介 编译apk项目需要使用的几个工具,基本都在sdk中,它们分别是(Windows系统): 1.aapt.exe 资源打包工具 2.android.jar Android编译工具 3.dx.bat dex文件生成工具 4.sdklib.jar 生成apk 5.jarsigner 签名工具 准备 在打包前,需要的环境如下: 1.JDK1.6+ 2.Android SDK 3.上述5个工具的路径 打包过程 1.生成R.java文件 比如: aapt p