如何在android中以编程方式创建带密码保护的.txt文件?

时间:2016-08-25 13:06:54

标签: android file password-protection

我使用下面的Snippet

在android中创建.txt文件
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();
    }
}

添加了以下权限

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

是否可以在Android中以编程方式提供该.txt文件的密码?

1 个答案:

答案 0 :(得分:0)

你可以使用Jasypt。 http://www.jasypt.org/

在示例下面。

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);