Android Open Source - br.com.mirabilis.sqlite Cypher File Manager






From Project

Back to project page br.com.mirabilis.sqlite.

License

The source code is released under:

Apache License

If you think the Android project br.com.mirabilis.sqlite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package br.com.mirabilis.sqlite.cypher;
//from w  w w  .ja  va 2s .  c  o  m
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.util.Base64;

/**
 * Class that crypt and decrypt file.
 * 
 * @author Rodrigo Sim?es Rosa.
 */
public class CypherFileManager {

  private File file;
  private CypherType cypherType;

  /**
   * {@link CypherType} Type of cypher
   * 
   * @author Rodrigo Sim?es Rosa
   */
  public enum CypherType {
    BASE64;
  }

  /**
   * Constructor
   * 
   * @param file
   * @param cypherType
   */
  public CypherFileManager(File file, CypherType cypherType) {
    this.file = file;
    this.cypherType = cypherType;
  }

  /**
   * Decrypt
   * 
   * @throws IOException
   */
  public void decrypt() throws IOException {
    BufferedReader reader = null;
    FileWriter fileWriter = null;
    reader = new BufferedReader(new FileReader(file));
    StringBuilder content = new StringBuilder();

    while (reader.ready()) {
      content.append(reader.readLine());
    }

    byte[] contentDecrypted = null;

    switch (cypherType) {
    case BASE64:
      contentDecrypted = Base64.decode(content.toString().getBytes(),
          Base64.DEFAULT);
      break;
    }

    File temp = new File(file.getAbsolutePath() + "_temp");

    if (!temp.exists()) {
      temp.createNewFile();
    }

    fileWriter = new FileWriter(temp);
    fileWriter.append(new String(contentDecrypted, "UTF-8"));
    fileWriter.flush();

    String fileOriginal = file.getAbsolutePath();
    file.delete();

    temp.renameTo(new File(fileOriginal));

    reader.close();
    fileWriter.close();

    reader.close();
  }

  /**
   * Encrypt
   * 
   * @throws IOException
   */
  public void encrypt() throws IOException {
    BufferedReader reader = null;
    FileWriter fileWriter = null;
    reader = new BufferedReader(new FileReader(file));

    StringBuilder content = new StringBuilder();

    while (reader.ready()) {
      content.append(reader.readLine());
    }

    byte[] contentDecrypted = null;

    switch (cypherType) {
    case BASE64:
      contentDecrypted = Base64.encode(content.toString().getBytes(),
          Base64.DEFAULT);
      break;
    }

    File temp = new File(file.getAbsolutePath() + "_temp");

    if (!temp.exists()) {
      temp.createNewFile();
    }

    fileWriter = new FileWriter(temp);
    fileWriter.append(new String(contentDecrypted, "UTF-8"));
    fileWriter.flush();

    String fileOriginal = file.getAbsolutePath();
    file.delete();

    temp.renameTo(new File(fileOriginal));

    reader.close();
    fileWriter.close();

    reader.close();
  }

  /**
   * Encrypt
   * 
   * @param file
   * @param cypherType
   * @throws IOException
   */
  public static void encrypt(File file, CypherType cypherType)
      throws IOException {
    new CypherFileManager(file, cypherType).encrypt();
  }

  /**
   * Decrypt
   * 
   * @param file
   * @param cypherType
   * @throws IOException
   */
  public static void decrypt(File file, CypherType cypherType)
      throws IOException {
    new CypherFileManager(file, cypherType).decrypt();
  }

}




Java Source Code List

br.com.mirabilis.sample.BuildConfig.java
br.com.mirabilis.sample.MainActivity.java
br.com.mirabilis.sqlite.BuildConfig.java
br.com.mirabilis.sqlite.BuildConfig.java
br.com.mirabilis.sqlite.annotation.SQLiteParseAnnotation.java
br.com.mirabilis.sqlite.annotation.model.SQLiteAnnotationEntity.java
br.com.mirabilis.sqlite.annotation.model.SQLiteAnnotationField.java
br.com.mirabilis.sqlite.cypher.CypherFileManager.java
br.com.mirabilis.sqlite.manager.core.SQLiteConnection.java
br.com.mirabilis.sqlite.manager.core.SQLiteCore.java
br.com.mirabilis.sqlite.manager.dao.DAO.java
br.com.mirabilis.sqlite.manager.dao.SQLiteDAO.java
br.com.mirabilis.sqlite.manager.dao.util.SQLiteDataManager.java
br.com.mirabilis.sqlite.manager.dao.util.SQLiteDataType.java
br.com.mirabilis.sqlite.manager.exception.SQLiteEmptyException.java
br.com.mirabilis.sqlite.manager.exception.SQLiteException.java
br.com.mirabilis.sqlite.manager.exception.SQLiteNotNullFieldException.java
br.com.mirabilis.sqlite.manager.model.SQLiteEntity.java
br.com.mirabilis.sqlite.manager.model.SQLiteField.java
br.com.mirabilis.sqlite.manager.model.SQLiteTable.java
br.com.mirabilis.sqlite.manager.util.SQLiteDatabaseFile.java
br.com.mirabilis.sqlite.view.SqliteActivity.java