android:

public class AesFileIo {     private static final String EOL = "\n";     private static final String AES_ALGORITHM = "AES/CTR/NoPadding";     private SecretKeySpec secretKeySpec;     private IvParameterSpec ivSpec;     private static final String PROVIDER = "BC";       AesFileIo(byte[] aesKey, byte[] iv) {         ivSpec = new IvParameterSpec(iv);         secretKeySpec = new SecretKeySpec(aesKey, "AES");     }      public String readFile(Context c, String fileName) {         StringBuilder stringBuilder = new StringBuilder();         try {             InputStream is = c.openFileInput(fileName);             Cipher cipher = Cipher.getInstance(AES_ALGORITHM, PROVIDER);             cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);             CipherInputStream cis = new CipherInputStream(is, cipher);             InputStreamReader isr = new InputStreamReader(cis);             BufferedReader reader = new BufferedReader(isr);             String line;             while ((line = reader.readLine()) != null) {                 stringBuilder.append(line).append(EOL);             }             is.close();         } catch (java.io.FileNotFoundException e) {             // OK, file probably not created yet             Log.i(this.getClass().toString(), e.getMessage(), e);         } catch (Exception e) {             Log.e(this.getClass().toString(), e.getMessage(), e);         }         return stringBuilder.toString();     }      public void writeFile(Context c, String fileName, String theFile) {         try {             Cipher cipher = Cipher.getInstance(AES_ALGORITHM, PROVIDER);              cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);             byte[] encrypted = cipher.doFinal(theFile.getBytes());              OutputStream os = c.openFileOutput(fileName, 0);             os.write(encrypted);             os.flush();             os.close();         } catch (Exception e) {             Log.e(this.getClass().toString(), e.getMessage(), e);         }     } } 

pc

public class AesFileIo {      private static final String EOL = "\n";     private static final String AES_ALGORITHM = "AES/CTR/NoPadding";     private SecretKeySpec secretKeySpec;     private IvParameterSpec ivSpec;      AesFileIo(byte[] aesKey, byte[] iv) {         Security.addProvider(new org.bouncycastle.jce.provider                 .BouncyCastleProvider());         ivSpec = new IvParameterSpec(iv);         secretKeySpec = new SecretKeySpec(aesKey, "AES");     }      public String readFile(String fileName) {         StringBuilder stringBuilder = new StringBuilder();         try {             FileInputStream fis = new FileInputStream(fileName);             Cipher cipher = Cipher.getInstance(AES_ALGORITHM);             cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);             CipherInputStream cis = new CipherInputStream(fis, cipher);             InputStreamReader isr = new InputStreamReader(cis);             BufferedReader reader = new BufferedReader(isr);             String line;             while ((line = reader.readLine()) != null) {                 stringBuilder.append(line).append(EOL);             }             fis.close();         } catch (java.io.FileNotFoundException e) {             System.out.println("FileNotFoundException: probably OK");         } catch (Exception e) {             e.printStackTrace();         }         return stringBuilder.toString();     }      public void writeFile(String fileName, String theFile) {         try {             Cipher cipher = Cipher.getInstance(AES_ALGORITHM);             cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);             byte[] encrypted = cipher.doFinal(theFile.getBytes());             FileOutputStream fos = new FileOutputStream(fileName);             fos.write(encrypted);             fos.flush();             fos.close();         } catch (Exception e) {             e.printStackTrace();         }     } } 

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. android SystemServer详解
  2. 解决ADT中新建Android工程出现多个appcom
  3. android View中如何判断长按事件
  4. Android中由文件名获取文件Id的两种方法
  5. android Paint 渐变色
  6. Android添加依赖出现This support librar
  7. Android(安卓)软键盘自动弹出与关闭实例
  8. Android(安卓)-- Autosizing TextView 自
  9. Android绘制(二):来用Path绘出想要的图形
  10. Android(安卓)调用 startActivityForResu