1

I want to set password to my log file. I don't want to let a user to see this log. I only want to save to one file (for one day) logs from logcat and let a user to see only that the file is exist, but he can not open and read this file. The user can open the file when he write a correct password.

public class App extends Application {
    public static String TOKEN = "1234";
    public static boolean gpsOn = true;
    public static double longtitude = 0.0, latitude = 0.0;
    public static int SYNC = 60;
    private String device_id;

    @Override
    public void onCreate() {
        super.onCreate();
        getUqid();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(
                    "yyyy-MM-dd", Locale.US);
            String currentDateandTime = sdf.format(new Date());
            final File path = new File(
                    Environment.getExternalStorageDirectory(), "***");
            if (!path.exists()) {
                path.mkdir();
            }
            Log.e("path " , path.getAbsolutePath());
            Runtime.getRuntime().exec("logcat  -f " + path + File.separator + "dbo_logcat_" + currentDateandTime + ".txt");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void getUqid() {
        device_id = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
        //Log.e("dev",""+device_id);
        Util.UNIQ = device_id;
    }
}
user207421
  • 305,947
  • 44
  • 307
  • 483
  • 1
    Does Android support file passwords? If not, you can't do it. – user207421 May 08 '17 at 10:19
  • The only way to "secure" your logs ( IMO if the log isn't sensitive, then it's not needed. If it's sensitive ... then uhm ... * _no words_ *) is to use a "password" to encrypt your log file. When the user has entered the password through your app, use it to decrypt the log file and display it. – KarelG May 08 '17 at 10:25
  • Did you try my answer? – Kaushal28 May 08 '17 at 11:37

2 Answers2

2

Hint: For setting password, I think you can follow this approach: Encrypt the file with the user password as a key and when you want to open that file, decypt it. For encryption and decryption, you can use Java's inbuilt Cryptography API. If you don't want to use cryptography, see this: This answer.

Here is actual implementation of password based encryption in Java.

Community
  • 1
  • 1
Kaushal28
  • 5,377
  • 5
  • 41
  • 72
-2

I wrote with java. You can use it for android.

public static void main(String[] args)
{

    try {

      File file = new File("/home/kerim/Masaüstü/kerim.txt");//log file path..

      if(file.exists()){
          System.out.println("Is Execute allow : " + file.canExecute());
      System.out.println("Is Write allow : " + file.canWrite());
      System.out.println("Is Read allow : " + file.canRead());
      }

      file.setExecutable(false);
      file.setReadable(false);
      file.setWritable(false);

      System.out.println("Is Execute allow : " + file.canExecute());
      System.out.println("Is Write allow : " + file.canWrite());
      System.out.println("Is Read allow : " + file.canRead());

      if (file.createNewFile()){
        System.out.println("File is created!");
      }else{
        System.out.println("File already exists.");
      }

    } catch (IOException e) {
      e.printStackTrace();
    }
}

doc is here enter link description hereenter link description hereenter link description here

user207421
  • 305,947
  • 44
  • 307
  • 483
kfc
  • 567
  • 1
  • 5
  • 10
  • Ok thanks but I create a file which I all time use in app (write new logs) –  May 08 '17 at 10:12
  • 1
    No password here, so it doesn't answer the question in any way, and calling `setExecutable()/setReadable()/setWritable()` on a file that doesn't exist is completely and utterly pointless, as is creating the file afterwards and not ensuring those permissions. Answer is basically nonsense. – user207421 May 08 '17 at 10:17
  • Then you can use chmod commands with "exec". Runtime.getRuntime().exec("chmod 600 '/home/kerim/Masaüstü/kerim.txt'"); – kfc May 08 '17 at 10:22
  • 1
    That is merely equivalent to the code you posted, only without the bugs, and still without the password. If you wish to amend your answer don't let us stop you. – user207421 May 08 '17 at 10:23
  • I see that this file doesn't have a password , how in commands I can set a passwort ? –  May 08 '17 at 11:05