2012-02-06 40 views
1

如何在SD卡中的應用程序文件上進行加密和解密?這樣我就可以保護SD卡上的文件,並且沒有其他人能夠訪問該應用程序以外的文件而無需解密這些文件。Android文件密碼學

有沒有人可以給我任何好的示例源來實現Android應用程序的加密?

回答

5

我已經編寫了這個程序,它將使用AES加密文件並解密相同的文件。這一定會幫助你。

FileInputStream fis = new FileInputStream(new File("D:/Shashank/Test123.txt")); 
     File outfile = new File("D:/Shashank/encTest1234.txt"); 
     int read; 
     if(!outfile.exists()) 
      outfile.createNewFile(); 
     File decfile = new File("D:/Shashank/dec123.txt"); 
     if(!decfile.exists()) 
      decfile.createNewFile(); 
     FileOutputStream fos = new FileOutputStream(outfile); 
     FileInputStream encfis = new FileInputStream(outfile); 
     FileOutputStream decfos = new FileOutputStream(decfile); 
     Cipher encipher = Cipher.getInstance("AES"); 
     Cipher decipher = Cipher.getInstance("AES"); 
     KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
     SecretKey skey = kgen.generateKey(); 
     encipher.init(Cipher.ENCRYPT_MODE, skey); 
     CipherInputStream cis = new CipherInputStream(fis, encipher); 
     decipher.init(Cipher.DECRYPT_MODE, skey); 
     CipherOutputStream cos = new CipherOutputStream(decfos,decipher); 
     while((read = cis.read())!=-1) 
       { 
        fos.write((char)read); 
        fos.flush(); 
       } 
     fos.close(); 
     while((read=encfis.read())!=-1) 
     { 
      cos.write(read); 
      cos.flush(); 
     } 
    cos.close(); 
+1

您好感謝這個這麼多,我可以能夠解決我的問題....非常感謝.... – 2012-02-06 10:04:19

+0

這似乎使用ECB模式,無法初始化的RNG。我強烈建議任何人不要複製此代碼並期望獲得任何安全性。 – 2014-12-01 19:37:54

2

您可以在寫入文件的同時簡單地加密文件的內容,然後在檢索時解密文件。 SIMPLE EXAMPLE:提供的鏈接顯示加密/解密字符串的示例代碼。希望它有幫助

3

我發現了Android應用程序中的加密解決方案,通過它可以使應用程序數據在應用程序內部和應用程序外部形成安全,sdcard文件管理器將無法訪問機密信息應用程序..

因爲您可以使用兩種可能的方法來確保應用程序文件的安全。

  1. 變化與其他擴展名的文件擴展名的格式,做到真正的文件將無法被用戶

  2. 你能夠用戶對應用程序文件的內容的密碼才能打開。

    加密

    在這裏,我要發佈一個示例源代碼中的應用程序文件將與AES算法進行加密,它將不能夠由用戶在文件管理器,但一旦用戶訪問將進入應用程序它將解密與真實的形式和工作正常。


package com.filepermition.android; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 

import javax.crypto.Cipher; 
import javax.crypto.CipherInputStream; 
import javax.crypto.CipherOutputStream; 
import javax.crypto.KeyGenerator; 
import javax.crypto.SecretKey; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class AndroidFilePermitionActivity extends Activity 
{ 
    Button btn_button; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btn_button = (Button)findViewById(R.id.btn_button); 

     btn_button.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       try{ 
        FileInputStream fis = new FileInputStream(
         new File("/mnt/sdcard/testfile/file.wav")); 
        File outfile = new File("/mnt/sdcard/testfile/encTest1234.wav"); 

        int read; 
        if(!outfile.exists()) 
         outfile.createNewFile(); 

        File decfile = new File("/mnt/sdcard/testfile/dec123.wav"); 
        if(!decfile.exists()) 
         decfile.createNewFile(); 

        FileOutputStream fos = new FileOutputStream(outfile); 
        FileInputStream encfis = new FileInputStream(outfile); 
        FileOutputStream decfos = new FileOutputStream(decfile); 

        Cipher encipher = Cipher.getInstance("AES"); 
        Cipher decipher = Cipher.getInstance("AES"); 

        KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
        SecretKey skey = kgen.generateKey(); 
        encipher.init(Cipher.ENCRYPT_MODE, skey); 
        CipherInputStream cis = new CipherInputStream(fis, encipher); 
        decipher.init(Cipher.DECRYPT_MODE, skey); 
        CipherOutputStream cos = new CipherOutputStream(decfos,decipher); 

        while((read = cis.read())!=-1) 
        { 
         fos.write((char)read); 
         fos.flush(); 
        } 
        fos.close(); 
        while((read=encfis.read())!=-1) 
        { 
         cos.write(read); 
         cos.flush(); 
        } 
        cos.close(); 

       }catch (Exception e) { 
        // TODO: handle exceptione 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
}