网上看到两种方法:

1.SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(key), "AES");

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

红色的部分为注意项,不能写为SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");

2.Cipher cipher = Cipher.getInstance("AES"); ---------4.3以上有bug

修改为

Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding"); 

 

相关文章:

  • 2022-12-23
  • 2021-10-23
  • 2022-01-11
  • 2021-06-11
  • 2022-12-23
  • 2021-07-24
  • 2021-11-08
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-27
  • 2021-11-22
  • 2021-12-27
  • 2022-12-23
相关资源
相似解决方案