2017-04-26 88 views
1

我创建了一个应用程序,用户可以保存,编辑和删除便笺,并将其存储在应用程序私有存储区域中。正在存储的数据需要加密,但我是编程新手,并不知道如何做到这一点,所以如果任何人可以建议请?我将下面的代码放在用于保存笔记的方法中,但出于安全原因,需要加密,对于初学者来说,最简单的方法是什么?如何加密存储在应用程序中的数据私人存储

public class Utilities { 

    public static final String FILE_EXTENSION = ".bin"; 

    public static boolean saveNote(Context context, Notes notes){ 
     String fileName = String.valueOf(notes.getDateTime()) + FILE_EXTENSION; 

     FileOutputStream fos; 
     ObjectOutputStream oos; 

     try { 
      fos = context.openFileOutput(fileName, context.MODE_PRIVATE); 
      oos = new ObjectOutputStream(fos); 
      oos.writeObject(notes); 
      oos.close(); 
      fos.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return false; //tell the user something went wrong 
     } 
     return true; 
    } 

    public static ArrayList<Notes> getSavedNotes(Context context) { 
     ArrayList<Notes> notes = new ArrayList<>(); 

     File filesDir = context.getFilesDir(); 
     filesDir.getAbsolutePath(); 
     ArrayList<String> noteFiles = new ArrayList<>(); 

     for(String file : filesDir.list()) { 
      if(file.endsWith(FILE_EXTENSION)) { 
       noteFiles.add(file); 
      } 
     } 

     FileInputStream fis; 
     ObjectInputStream ois; 

     for(int i = 0; i < noteFiles.size(); i++) { 
      try{ 
       fis = context.openFileInput(noteFiles.get(i)); 
       ois = new ObjectInputStream(fis); 

       notes.add((Notes)ois.readObject()); 

       fis.close(); 
       ois.close(); 



      } catch (IOException | ClassNotFoundException e) { 
       e.printStackTrace(); 
       return null; 

      } 
     } 

     return notes; 

    } 

    public static Notes getNoteByName(Context context, String fileName) { 
     File file = new File(context.getFilesDir(), fileName); 
     Notes notes; 

     if(file.exists()) { 
      FileInputStream fis; 
      ObjectInputStream ois; 

      try { 
       fis = context.openFileInput(fileName); 
       ois = new ObjectInputStream(fis); 

       notes = (Notes) ois.readObject(); 

       fis.close(); 
       ois.close(); 

      } catch(IOException | ClassNotFoundException e){ 
       e.printStackTrace(); 
       return null; 
      } 

      return notes; 
     } 

     return null; 
    } 

    public static void deleteNote(Context context, String fileName) { 
     File Dir = context.getFilesDir(); 
     File file = new File(Dir, fileName); 

     if (file.exists()) file.delete(); 
    } 

    public static void main(String[] args) { 
     try { 
      String key = "squirrel123"; // needs to be at least 8 characters for DES 

      FileInputStream fis = new FileInputStream("original.txt"); 
      FileOutputStream fos = new FileOutputStream("encrypted.txt"); 
      encrypt(key, fis, fos); 

      FileInputStream fis2 = new FileInputStream("encrypted.txt"); 
      FileOutputStream fos2 = new FileOutputStream("decrypted.txt"); 
      decrypt(key, fis2, fos2); 
     } catch (Throwable e) { 
      e.printStackTrace(); 
     } 
    } 

    public static void encrypt(String key, InputStream is, OutputStream os) throws Throwable { 
     encryptOrDecrypt(key, Cipher.ENCRYPT_MODE, is, os); 
    } 

    public static void decrypt(String key, InputStream is, OutputStream os) throws Throwable { 
     encryptOrDecrypt(key, Cipher.DECRYPT_MODE, is, os); 
    } 

    public static void encryptOrDecrypt(String key, int mode, InputStream is, OutputStream os) throws Throwable { 
     DESKeySpec dks = new DESKeySpec(key.getBytes()); 
     SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); 
     SecretKey desKey = skf.generateSecret(dks); 
     Cipher cipher = Cipher.getInstance("DES"); // DES/ECB/PKCS5Padding for SunJCE 

     if (mode == Cipher.ENCRYPT_MODE) { 
      cipher.init(Cipher.ENCRYPT_MODE, desKey); 
      CipherInputStream cis = new CipherInputStream(is, cipher); 
      doCopy(cis, os); 
     } else if (mode == Cipher.DECRYPT_MODE) { 
      cipher.init(Cipher.DECRYPT_MODE, desKey); 
      CipherOutputStream cos = new CipherOutputStream(os, cipher); 
      doCopy(is, cos); 
     } 
    } 

    public static void doCopy(InputStream is, OutputStream os) throws IOException { 
     byte[] bytes = new byte[64]; 
     int numBytes; 
     while ((numBytes = is.read(bytes)) != -1) { 
      os.write(bytes, 0, numBytes); 
     } 
     os.flush(); 
     os.close(); 
     is.close(); 

    } 

} 

编辑: 我现在已经添加了一个例子,在原有代码现在看起来像下面这样DES加密,还我怎么会知道这个数据实际上是加密的?

public class Utilities { 

    public static final String FILE_EXTENSION = ".bin"; 

    public static boolean saveNote(Context context, Notes notes){ 
     String fileName = String.valueOf(notes.getDateTime()) + FILE_EXTENSION; 

     FileOutputStream fos; 
     ObjectOutputStream oos; 

     try { 
      fos = context.openFileOutput(fileName, context.MODE_PRIVATE); 
      oos = new ObjectOutputStream(fos); 
      oos.writeObject(notes); 
      oos.close(); 
      fos.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return false; //tell the user something went wrong 
     } 
     return true; 
    } 

    public static ArrayList<Notes> getSavedNotes(Context context) { 
     ArrayList<Notes> notes = new ArrayList<>(); 

     File filesDir = context.getFilesDir(); 
     filesDir.getAbsolutePath(); 
     ArrayList<String> noteFiles = new ArrayList<>(); 

     for(String file : filesDir.list()) { 
      if(file.endsWith(FILE_EXTENSION)) { 
       noteFiles.add(file); 
      } 
     } 

     FileInputStream fis; 
     ObjectInputStream ois; 

     for(int i = 0; i < noteFiles.size(); i++) { 
      try{ 
       fis = context.openFileInput(noteFiles.get(i)); 
       ois = new ObjectInputStream(fis); 

       notes.add((Notes)ois.readObject()); 

       fis.close(); 
       ois.close(); 
      } catch (IOException | ClassNotFoundException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 

     return notes; 
    } 

    public static Notes getNoteByName(Context context, String fileName) { 
     File file = new File(context.getFilesDir(), fileName); 
     Notes notes; 

     if(file.exists()) { 
      FileInputStream fis; 
      ObjectInputStream ois; 

      try { 
       fis = context.openFileInput(fileName); 
       ois = new ObjectInputStream(fis); 

       notes = (Notes) ois.readObject(); 

       fis.close(); 
       ois.close(); 
      } catch(IOException | ClassNotFoundException e){ 
       e.printStackTrace(); 
       return null; 
      } 

      return notes; 
     } 

     return null; 
    } 

    public static void deleteNote(Context context, String fileName) { 
     File Dir = context.getFilesDir(); 
     File file = new File(Dir, fileName); 

     if(file.exists()) { 
      file.delete(); 
     } 
    } 
} 

回答

0

DES(数据加密标准)对于像您这样的简单任务很常见。网上有很多关于如何使用它的教程。以下是我用过的一个示例:http://www.avajava.com/tutorials/lessons/how-do-i-encrypt-and-decrypt-files-using-des.html

还有另一个线程,用户共享一个更高级的方法,即基于密码的密钥派生函数,也值得尝试。这里的链接:How to encrypt and salt the password using BouncyCastle API in Java?

+0

如果我要使用该链接的示例代码为DES,我会创建一个新的java类文件,并添加它或替换当前我已经把这个问题? – Jay1

+0

@ Jay1您可以将它添加到您当前的课程中。只需添加新的方法(加密,解密等)到你的班级。你应该能够精确地复制这些内容。您将需要为您的项目定制实现。这个例子的主要方法应该把你放在正确的轨道上! – coinbird

+0

好吧,我已经将示例代码添加到当前源代码下面的现有类中,我将编辑帖子以显示我已完成的操作,但是如何知道数据实际上是加密/解密的? – Jay1

相关问题