Android数据加密之Aes加密

家电修理 2023-07-16 19:17www.caominkang.com电器维修

前言

项目中除了登陆,支付等接口采用rsa非对称加密,之外的采用aes对称加密,今天我们来认识一下aes加密。 

其他几种加密方式
 •Android数据加密之Rsa加密
 •Android数据加密之Aes加密
 •Android数据加密之Des加密
 •Android数据加密之MD5加密
 •Android数据加密之base64编码算法
 •Android数据加密之SHA安全散列算法 

什么是aes加密?

      高级加密标准(英语Advanced Encryption Standard,缩写AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。 

接下来我们来实际看下具体怎么实现 

对于AesUtils类常量简介 

  private final static String HEX = "0123456789ABCDEF";
  private static final String CBC_PKCS5_PADDING = "AES/CBC/PKCS5Padding";//AES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
  private static final String AES = "AES";//AES 加密
  private static final String SHA1PRNG="SHA1PRNG";//// SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法

如何生成一个随机Key?

  
  public static String generateKey() {
 try {
   SecureRandom localSecureRandom = SecureRandom.getInstance(SHA1PRNG);
   byte[] bytes_key = ne byte[20];
   localSecureRandom.nextBytes(bytes_key);
   String str_key = toHex(bytes_key);
   return str_key;
 } catch (Exception e) {
   e.printStackTrace();
 }
 return null;
  }

Aes密钥处理 

  // 对密钥进行处理
  private static byte[] getRaKey(byte[] seed) thros 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[] ra = skey.getEncoded();
 return ra;
  }

Aes加密过程 


  public static String encrypt(String key, String cleartext) {
 if (TextUtils.isEmpty(cleartext)) {
   return cleartext;
 }
 try {
   byte[] result = encrypt(key, cleartext.getBytes());
   return base64Encoder.encode(result);
 } catch (Exception e) {
   e.printStackTrace();
 }
 return null;
  }

  
  private static byte[] encrypt(String key, byte[] clear) thros Exception {
 byte[] ra = getRaKey(key.getBytes());
 SecretKeySpec skeySpec = ne SecretKeySpec(ra, AES);
 Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
 cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ne IvParameterSpec(ne byte[cipher.getBlockSize()]));
 byte[] encrypted = cipher.doFinal(clear);
 return encrypted;
  }

Aes解密过程 

  
  public static String decrypt(String key, String encrypted) {
 if (TextUtils.isEmpty(encrypted)) {
   return encrypted;
 }
 try {
   byte[] enc = base64Decoder.decodeToBytes(encrypted);
   byte[] result = decrypt(key, enc);
   return ne String(result);
 } catch (Exception e) {
   e.printStackTrace();
 }
 return null;
  }

  
  private static byte[] decrypt(String key, byte[] encrypted) thros Exception {
 byte[] ra = getRaKey(key.getBytes());
 SecretKeySpec skeySpec = ne SecretKeySpec(ra, AES);
 Cipher cipher = Cipher.getInstance(CBC_PKCS5_PADDING);
 cipher.init(Cipher.DECRYPT_MODE, skeySpec, ne IvParameterSpec(ne byte[cipher.getBlockSize()]));
 byte[] decrypted = cipher.doFinal(encrypted);
 return decrypted;
  }

二进制转字符 

 //二进制转字符
  public static String toHex(byte[] buf) {
 if (buf == null)
   return "";
 StringBuffer result = ne 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));
  }

测试程序 

 List personList = ne ArrayList<>();
 int testMaxCount = 1000;//测试的最大数据条数
 //添加测试数据
 for (int i = 0; i < testMaxCount; i++) {
   Person person = ne Person();
   person.setAge(i);
   person.setName(String.valueOf(i));
   personList.add(person);
 }
 //FastJson生成json数据
 String jsonData = JsonUtils.objectToJsonForFastJson(personList);
 Log.e("MainActivity", "AES加密前json数据 ---->" + jsonData);
 Log.e("MainActivity", "AES加密前json数据长度 ---->" + jsonData.length());

 //生成一个动态key
 String secretKey = AesUtils.generateKey();
 Log.e("MainActivity", "AES动态secretKey ---->" + secretKey);

 //AES加密
 long start = System.currentTimeMillis();
 String encryStr = AesUtils.encrypt(secretKey, jsonData);
 long end = System.currentTimeMillis();
 Log.e("MainActivity", "AES加密耗时 cost time---->" + (end - start));
 Log.e("MainActivity", "AES加密后json数据 ---->" + encryStr);
 Log.e("MainActivity", "AES加密后json数据长度 ---->" + encryStr.length());

 //AES解密
 start = System.currentTimeMillis();
 String decryStr = AesUtils.decrypt(secretKey, encryStr);
 end = System.currentTimeMillis();
 Log.e("MainActivity", "AES解密耗时 cost time---->" + (end - start));
 Log.e("MainActivity", "AES解密后json数据 ---->" + decryStr);

运行耗时

 由此可见对称Aes效率还是比较高的。

Copyright © 2016-2025 www.caominkang.com 曹敏电脑维修网 版权所有 Power by