参考文章:AES加密解密(ECB模式)
工具:在线AES加密解密
使用固定的key
package com.eshore.cloud.utils; import android.text.TextUtils; import android.util.Base64; import java.security.Provider; 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; public class AesUtil { //偏移量 public static final String VIPARA = "XXX"; //AES 为16位. DES 为8bytes //AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式 private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding"; //AES 加密 private static final String AES = "AES"; // SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法 private static final String SHA1PRNG = "SHA1PRNG"; //编码方式 public static final String CODE_TYPE = "UTF-8"; /** * 加密 * * @param key * @param cleartext * @return */ public static String encrypt(String key, String cleartext) { if (TextUtils.isEmpty(cleartext)) { return cleartext; } try { byte[] result = encrypt(key, cleartext.getBytes(CODE_TYPE)); return new String(Base64.encode(result, Base64.NO_WRAP),CODE_TYPE); } catch (Exception e) { e.printStackTrace(); } return null; } public static byte[] encrypt(String key, byte[] clear) throws Exception { IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes(CODE_TYPE)); /*byte[] raw = getRawKey(key.getBytes("utf-8")); SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);*/ byte[] raw = key.getBytes(CODE_TYPE); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, zeroIv); byte[] encrypted = cipher.doFinal(clear); return encrypted; } /** * 解密 * * @param key * @param encrypted * @return */ public static String decrypt(String key, String encrypted) { if (TextUtils.isEmpty(encrypted)) { return encrypted; } try { byte[] enc = Base64.decode(encrypted.getBytes(CODE_TYPE), Base64.NO_WRAP); 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 { IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes(CODE_TYPE)); /* byte[] raw = getRawKey(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(raw, AES);*/ byte[] raw = key.getBytes(CODE_TYPE); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING); cipher.init(Cipher.DECRYPT_MODE, skeySpec, zeroIv); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; } }使用动态的key:
Android使用AES加密和解密
package com.iefeel.tool; import android.text.TextUtils; import android.util.Base64; import java.security.Provider; 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; public class AesUtil { private final static String HEX = "0123456789ABCDEF"; //AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式 private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding"; //AES 加密 private static final String AES = "AES"; // SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法 private static final String SHA1PRNG = "SHA1PRNG"; /** * @return 动态生成秘钥 */ public static String generateKey() { try { SecureRandom localSecureRandom = SecureRandom.getInstance(SHA1PRNG); byte[] bytes_key = new byte[20]; localSecureRandom.nextBytes(bytes_key); String str_key = toHex(bytes_key); return str_key; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 对秘钥进行处理 * * @param seed 动态生成的秘钥 * @return * @throws Exception */ private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance(AES); //for android SecureRandom sr = null; // 在4.2以上版本中,SecureRandom获取方式发生了改变 int sdk_version = android.os.Build.VERSION.SDK_INT; // Android 6.0 以上 if (sdk_version > 23) { sr = SecureRandom.getInstance(SHA1PRNG, new CryptoProvider()); //4.2及以上 } else 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); //256 bits or 128 bits,192bits kgen.init(128, sr); //AES中128位密钥版本有10个加密循环,192比特密钥版本有12个加密循环,256比特密钥版本则有14个加密循环。 SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return raw; } /** * 加密 * * @param key * @param cleartext * @return */ public static String encrypt(String key, String cleartext) { if (TextUtils.isEmpty(cleartext)) { return cleartext; } try { byte[] result = encrypt(key, cleartext.getBytes()); return new String(Base64.encode(result, Base64.DEFAULT)); } catch (Exception e) { e.printStackTrace(); } return null; } private static byte[] encrypt(String key, byte[] clear) throws Exception { byte[] raw = getRawKey(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(raw, AES); Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byte[] encrypted = cipher.doFinal(clear); return encrypted; } /** * 解密 * * @param key * @param encrypted * @return */ public static String decrypt(String key, String encrypted) { if (TextUtils.isEmpty(encrypted)) { return encrypted; } try { byte[] enc = Base64.decode(encrypted, Base64.DEFAULT); byte[] result = decrypt(key, enc); return new String(result); } catch (Exception e) { e.printStackTrace(); } return null; } private static byte[] decrypt(String key, byte[] encrypted) throws Exception { byte[] raw = getRawKey(key.getBytes()); SecretKeySpec skeySpec = new SecretKeySpec(raw, AES); Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING); cipher.init(Cipher.DECRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()])); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; } // ----------------------------------------------- 辅助方法 ------------------------------------ //二进制转字符 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(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f)); } // 增加 CryptoProvider 类 public static class CryptoProvider extends Provider { /** * Creates a Provider and puts parameters */ public CryptoProvider() { super("Crypto", 1.0, "HARMONY (SHA1 digest; SecureRandom; SHA1withDSA signature)"); put("SecureRandom.SHA1PRNG", "org.apache.harmony.security.provider.crypto.SHA1PRNG_SecureRandomImpl"); put("SecureRandom.SHA1PRNG ImplementedIn", "Software"); } } }
本文地址:https://blog.csdn.net/tantion/article/details/107404940
您可能感兴趣的文章: