public static String encrypt(String seed, String cleartext) throws Exception { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKeySpec keyspec = new SecretKeySpec(seed.getBytes(), "AES"); cipher.init(Cipher.ENCRYPT_MODE, keyspec); byte[] encrypted = cipher.doFinal(cleartext.getBytes()); return Base64.encodeToString(encrypted, Base64.DEFAULT); } catch (Exception e) { e.printStackTrace(); return null; } } public static String decrypt(String seed, String encrypted) throws Exception { try { byte[] encrypted1 = Base64.decode(encrypted.getBytes(), Base64.DEFAULT); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); SecretKeySpec keyspec = new SecretKeySpec(seed.getBytes(), "AES"); cipher.init(Cipher.DECRYPT_MODE, keyspec); byte[] original = cipher.doFinal(encrypted1); return new String(original, "UTF-8"); } catch (Exception e) { e.printStackTrace(); return null; } }
Android AES加密解密
最新推荐文章于 2024-08-19 19:15:48 发布