【问题标题】:SQLite Data Base decryption using AES使用 AES 解密 SQLite 数据库
【发布时间】:2014-08-30 07:10:53
【问题描述】:

我正在尝试使用this post 的答案加密我的数据库并将其保存到 sdCard。 但问题是当我将文件放回我的应用程序包中的databases 文件夹时, SQLiteOpenHelper 无法读取它。错误:

  • 08-30 10:58:35.692: E/SQLiteLog(3801): (26) 文件已加密或不是数据库

  • 08-30 10:58:35.692:E/DefaultDatabaseErrorHandler(3801):sqlite 报告的数据库损坏:/data/data/com.padra_tech.karamad/databases/PrimaryInformation

  • 08-30 10:58:35.692: E/DefaultDatabaseErrorHandler(3801): !@ make .back 文件

这是我的课:

package dataBases;


public class BackupHelper {

public static int SECURITY_NONE = 1 ;
public static int SECURITY_ENCRYPTED = 2 ;

public static void backup(Context context, int securityMode) {
    
    File backup = new File(Environment.getExternalStorageDirectory()+ 
            "/" + "KarAmad" + "/" + "backup");
    backup.mkdirs();
    
    List<File> src = new ArrayList<File>();
    List<File> dst = new ArrayList<File>();
    
    
    try {
        src.add( new File(new PrimaryInformationDataBase(context).getDirectory()) );
        dst.add( new File(backup.getPath() + "/" + "PI") );
        
        src.add( new File(new TransactionDataBase(context).getDirectory()) );
        dst.add( new File(backup.getPath() + "/" + "T") );
        
        src.add( new File(new NoteDataBase(context).getDirectory()) );
        dst.add( new File(backup.getPath() + "/" + "N") );
        
        src.add( new File(new PictureDataBase(context).getDirectory()) );
        dst.add( new File(backup.getPath() + "/" + "P") );
        
        for(int i = 0 ; i < src.size() ; i ++) {
            dst.get(i).createNewFile();
            if(securityMode == SECURITY_ENCRYPTED)
                BackupHelper.encrypt(src.get(i), dst.get(i));
            else
                LeftFragment.copy(src.get(i), dst.get(i));
        }
        
        Toast.makeText(context, "پشتیبان گیری انجام شد", Toast.LENGTH_SHORT).show();
        
    } catch (Exception e) {
        
        Toast.makeText(context, "پشتیبان گیری انجام نشد!", Toast.LENGTH_SHORT).show();
        e.printStackTrace();

    }
}

public static void restore(Context context, int securityMode) {
    
    NoteDataBase dummyNoteDataBase = new NoteDataBase(context);
    String temp = dummyNoteDataBase.getDirectory();
    String dataBasesPath = temp.substring(0, temp.lastIndexOf("/"));
    
    File source = new File(Environment.getExternalStorageDirectory()+ 
            "/" + "KarAmad" + "/" + "backup");
    
    List<File> src = new ArrayList<File>();
    List<File> dst = new ArrayList<File>();
    
    try {
        src.add( new File(source.getPath() + "/" + "PI") );
        dst.add( new File(dataBasesPath + "/" + "PrimaryInformation") );
        
        src.add( new File(source.getPath() + "/" + "T") );
        dst.add( new File(dataBasesPath + "/" + "Transaction") );
        
        src.add( new File(source.getPath() + "/" + "N") );
        dst.add( new File(dataBasesPath + "/" + "Note") );
        
        src.add( new File(source.getPath() + "/" + "P") );
        dst.add( new File(dataBasesPath + "/" + "Picture") );
        
        for(int i = 0 ; i < src.size() ; i++) {
            
            dst.get(i).createNewFile();
            
            if(securityMode == SECURITY_ENCRYPTED)
                BackupHelper.decrypt(src.get(i), dst.get(i));
            else
                LeftFragment.copy(src.get(i), dst.get(i));
        }
        
        
        
        Toast.makeText(context, "بازیابی فایل پشتیبان انجام شد", Toast.LENGTH_SHORT).show();
        
    } catch (Exception e) {
        
        Toast.makeText(context, "بازیابی فایل پشتیبان انجام نشد!", Toast.LENGTH_SHORT).show();
        e.printStackTrace();

    }
}

public static void encrypt(File src, File dst) throws IOException,
    NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    
    FileInputStream inputStream = new FileInputStream(src);
    FileOutputStream outputStream = new FileOutputStream(dst);
    
    SecretKeySpec keySpec = new SecretKeySpec("1393032613930326".getBytes(), "AES");
    
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, keySpec);
    
    CipherOutputStream cipherOutputStream = 
            new CipherOutputStream(outputStream, cipher);
    
    int b;
    byte[] d = new byte[8];
    while((b = inputStream.read(d)) > 0) {
        cipherOutputStream.write(d, 0, b);
    }
    
    cipherOutputStream.flush();
    cipherOutputStream.close();
    inputStream.close();
}


public static void decrypt(File src, File dst) throws IOException,
NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {

    FileInputStream inputStream = new FileInputStream(src);
    FileOutputStream outputStream = new FileOutputStream(dst);

    SecretKeySpec keySpec = new SecretKeySpec("1393032613930326".getBytes(), "AES");

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, keySpec);

    CipherInputStream cipherOutputStream = 
            new CipherInputStream(inputStream, cipher);

    int b;
    byte[] d = new byte[8];
    while((b = inputStream.read(d)) > 0) {
        outputStream.write(d, 0, b);
    }

    outputStream.flush();
    outputStream.close();
    cipherOutputStream.close();

}
}

【问题讨论】:

  • 你使用的代码和帖子一样吗?如果不能,您能否提供用于加密和解密数据库的代码 sn-ps
  • @hoomi 我添加了代码
  • 我只是想知道。您是要加密目录还是文件?

标签: android sqlite encryption


【解决方案1】:

decrypt 方法从 inputStream 读取,而不是从错误命名的 cipherOutputStream 读取。所以你只是制作一个副本而不是解密数据库。

【讨论】:

    【解决方案2】:
    For Encrypting sql database in android please kindly use SQLCipher open library,
    follow this simple steps to change existing database to  CipherDB.
    

    • 改变

      导入 android.database.sqlite.SQLiteDatabase;

                        To
      

      导入 net.sqlcipher.database.SQLiteDatabase;

    • 改变

      OpenHelper cipherHelper = new OpenHelper(Context); SQLiteHelper.sdb = cipherHelper.getWritableDatabase();

                        To
      

      OpenHelper cipherHelper = new OpenHelper(Context); SQLiteHelper.sdb = cipherHelper.getWritableDatabase(encryptionPassword);

      注意:运行前请先卸载旧应用程序。

    来源链接: https://www.zetetic.net/sqlcipher/sqlcipher-for-android/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-28
      • 2011-02-04
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 2016-04-25
      • 1970-01-01
      相关资源
      最近更新 更多