2016-08-25 2 views
0

ich TXT-Datei in android bin die Schaffung von unten Snippet mitWie erstellt man eine .txt-Datei mit Passwortschutz in Android programmatisch?

public void createTextFile(String fileName) { 
try { 
    File notesDirectory = new File(Environment.getExternalStorageDirectory(), "Text Files"); 
    if (!notesDirectory.exists()) { 
     notesDirectory.mkdirs(); 
    } 
    File textFile = new File(notesDirectory, fileName); 
    FileWriter writer = new FileWriter(textFile); 
    writer.append("Sample Content"); 
    writer.flush(); 
    writer.close(); 
    Toast.makeText(this, "File Created at "+textFile.getAbsolutePath(), Toast.LENGTH_SHORT).show(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

Hinzugefügt unten Erlaubnis

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

Ist es möglich, in Android Kennwort für die TXT-Datei programmatisch zur Verfügung zu stellen?

+2

Ich denke, Sie müssen Ihre Dateien Inhalt mit einem Passwort verschlüsseln. Sie können die Datei selbst nicht mit einem Passwort "sperren" – IIIIIIIIIIIIIIIIIIIIII

+0

siehe auch http://stackoverflow.com/questions/15742091/password-protection-of-file-in-android –

Antwort

0

Sie können Jasypt verwenden. http://www.jasypt.org/

Unterhalb des Beispiels.

StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor(); 
String encryptedPassword = passwordEncryptor.encryptPassword(userPassword); 
... 
if (passwordEncryptor.checkPassword(inputPassword, encryptedPassword)) { 
    // correct! 
} else { 
    // bad login! 
} 

...encrypting and decrypting a text... 

StrongTextEncryptor textEncryptor = new StrongTextEncryptor(); 
textEncryptor.setPassword(myEncryptionPassword); 
String myEncryptedText = textEncryptor.encrypt(myText); 
... 
String plainText = textEncryptor.decrypt(myEncryptedText); 
Verwandte Themen