2017-04-26 113 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

相關問題