如何在android中锁定文件? [英] How can I lock a file in android?

查看:97
本文介绍了如何在android中锁定文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题所在是我的应用程序中,我正在从Internet上获取一些文件,然后将它们放在内部存储中,然后当用户想要访问这些文件时,用户可以通过我的应用程序访问它们.

So the scenario of my problem is in my application I am fetching some file from the internet then I put them in internal storage then when user wants to access these file User can access them through my application.

我做了所有事情,但是我想在我的应用程序中提供一些安全性,所以我想要的是什么?

I did all the thing but I want to provide some security in my application So what I want is?

  1. 用户只能在我的应用程序中看到我的文件.用户无法从文件管理器访问它们.不要告诉我解决点的方法 在文件名或文件夹名之前,我这样做是有问题的 解决方案是当我通过在文件名前加点号来访问它们时 文件再次可见.
  2. 我还希望当用户打开我的文件时,就像pdf阅读器中的pdf一样.他/她限制通过pdf保存或下载它们 阅读器或其他应用程序.
  1. User can see my file only in my application. User unable to access them from the file manager. Don't tell me the solution to put dot before file name or folder name I did that the problem with this solution is when I access them by putting dot before file name the file are again visible .
  2. I also want that when the user open my file ,just like a pdf in pdf reader He/She restricted to save or download them through pdf reader or another application.

任何帮助都会受到我的感谢.

Any Kind of help is appreciated by me.

推荐答案

您可以加密该文件,以便用户无法访问它们.这是对我有用的加密方法.

You can encrypt that file so user won't be able to access them. here is the encryption method which works for me.

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

   // 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();
}

这是解密特定文件的方法.

Here is the method for decryption of particular file.

 public void decrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream("Path Of your file");

FileOutputStream fos = new FileOutputStream("Path Of your file");
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();} 

这篇关于如何在android中锁定文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆