java,AES加密,解密 // 加密文件 public static String EFile(String in) { String hex = ""; try { //读文件 byte[] bytIn= in.getBytes(); //AES加密 SecretKeySpec skeySpec = new SecretKeySpec(Globals.RAW, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); //写文件 byte[] bytOut = cipher.doFinal(bytIn); //加密后转为可读的16进制String,以便于保存 hex = byte2hexString(bytOut); } catch (Exception e) { Log.d("TAG",e.toString()); } return hex; } //解密文件 public static String DFile(String hex) { String rr = ""; try { //读文件 //AES解密 //将保存的16进制String,并对他进行解码 byte[] bytIn= hex2Bin(hex); SecretKeySpec skeySpec = new SecretKeySpec(Globals.RAW, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); //写文件 byte[] bytOut = cipher.doFinal(bytIn); rr = new String(bytOut); } catch (Exception e) { Log.d("TAG",e.toString()); } return rr; } private static byte[] hex2Bin(String src) { if (src.length() < 1) return null; byte[] encrypted = new byte[src.length() / 2]; for (int i = 0; i < src.length() / 2; i++) { int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16); encrypted[i] = (byte) (high * 16 + low); } return encrypted; } private static String byte2hexString(byte buf[]) { StringBuffer strbuf = new StringBuffer(buf.length * 2); int i; for( i=0;i<buf.length;i++){ strbuf.append( Integer.toString((buf[i]>>4) & 0xf, 16 )+ Integer.toString( buf[i] & 0xf, 16 )); } //同上 // for (i = 0; i < buf.length; i++) { // if (((int) buf[i] & 0xff) < 0x10) // strbuf.append("0"); // // strbuf.append(Long.toString((int) buf[i] & 0xff, 16)); // } return strbuf.toString(); }