0

I am making an Android application, and I want to prevent users from opening some folder. In that folder user can store image or video files. It would be great if I could protect that folder with password.

Which is the best way to do that?

Zookey
  • 2,637
  • 13
  • 46
  • 80
  • I don't know the best way.. but the easiest (or cheapest) way would be to use your app's private folder (only your app can access) and provide a UI on top of it.. – Madushan Mar 22 '13 at 11:43
  • Cool. But how I can make private folder? Is there any example? – Zookey Mar 22 '13 at 11:45
  • See http://developer.android.com/guide/topics/data/data-storage.html#filesInternal Every app gets a private folder only that app can access. (Android manages security and it's on internal storage, so not accessible via USB) You can use the above API to manipulate it. – Madushan Mar 22 '13 at 11:47
  • try this link, it is working fine, http://stackoverflow.com/questions/10782187/how-to-encrypt-file-from-sd-card-using-aes-in-android le me know. – Hasmukh Mar 22 '13 at 11:51

3 Answers3

5

Here is Both function for encrypt and decrypt file in Sdcard folder. we can not lock folder but we can encrypt file using AES in Android, it may help you.

static void encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    // Here you read the cleartext.
    FileInputStream fis = new FileInputStream("data/cleartext");
    // This stream write the encrypted text. This stream will be wrapped by another stream.
    FileOutputStream fos = new FileOutputStream("data/encrypted");

    // Length is 16 byte
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    // Create cipher
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, sks);
    // Wrap the output stream
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    // Write bytes
    int b;
    byte[] d = new byte[8];
    while((b = fis.read(d)) != -1) {
        cos.write(d, 0, b);
    }
    // Flush and close streams.
    cos.flush();
    cos.close();
    fis.close();
}

static void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    FileInputStream fis = new FileInputStream("data/encrypted");

    FileOutputStream fos = new FileOutputStream("data/decrypted");
    SecretKeySpec sks = new SecretKeySpec("MyDifficultPassw".getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, sks);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    int b;
    byte[] d = new byte[8];
    while((b = cis.read(d)) != -1) {
        fos.write(d, 0, b);
    }
    fos.flush();
    fos.close();
    cis.close();
}
Hasmukh
  • 4,632
  • 2
  • 30
  • 44
2

You should save this information on the internal storage. Normally other apps can't access these files. From the quideliness:

You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.

See the link: http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

RobinDeCroon
  • 331
  • 2
  • 11
  • So, as I get from this link, I just need to save application files into some folder on the internal storage, and that will be private? – Zookey Mar 22 '13 at 11:47
  • 1
    Yep, but don't forget the Private Mode: FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); Also be aware that rooted devices might be able to access these files. – RobinDeCroon Mar 22 '13 at 11:50
1

Insted of LOCK i will saggest you make folder with . like foldername -> .test

user cant see that folder here is code

File direct = new File(Environment.getExternalStorageDirectory() + "/.test");

   if(!direct.exists())
    {
        if(direct.mkdir()) 
          {
           //directory is created;
          }

    }

above code is create folder with name ".test" (in SD CARD) then save your data(file, video.. whatever) in this folder user cant access it.

if you create folder in internal storage then if user clear data of your app then that folder may EMPTY!

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177