Pet*_*vdL 9 security string android textview
我想从Android上的用户那里获取密码字符串.
我不希望这个字符串在用户输入过程中的任何时刻存储在Java String中,也不希望它存储在char或字节数组中的代码中.
其原因是Sun禁止将Java字符串用于敏感数据. "String类型的对象是不可变的,即没有定义允许您在使用后更改(覆盖)或清零String内容的方法.此功能使String对象不适合存储安全敏感信息,如用户密码.您应该始终在char数组中收集和存储安全敏感信息." http://docs.oracle.com/javase/1.5.0/docs/guide/security/jce/JCERefGuide.html#PBEEx
所以我不能使用EditText,因为它在内部使用了Strings(即使它返回一个可编辑的,可以想象是由char []或Char []支持?).
从用户接受char数组的最简单方法是什么?我猜一个Canvas,我在那里听关键事件?
我没有在内部看到EditText.java(API 17)使用String.它只是2页长的代码.当然,EditText继承的TextView.java在文件中有9k行.你仍然不会在内部使用String看到TextView.java,但是它有自己的CharSequence CharWrapper实现.(TextView.java行#8535 API 17).这里有方法调用getChars.正如您将注意到buf复制的mChars是char []而不是String.
private char[] mChars;
public void getChars(int start, int end, char[] buf, int off) {
if (start < 0 || end < 0 || start > mLength || end > mLength) {
throw new IndexOutOfBoundsException(start + ", " + end);
}
System.arraycopy(mChars, start + mStart, buf, off, end - start);
}
Run Code Online (Sandbox Code Playgroud)
现在你所要做的就是调用getChar并传递char []来填充.
int pl = mPasswordEt.length();
char[] password = new char[pl];
mPasswordEt.getText().getChars(0, pl, password, 0);
Run Code Online (Sandbox Code Playgroud)
char[] password没有使用String就可以获得所需的.完成后,您可以从内存中清除它,如下所示.
Arrays.fill(password, ' ');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2077 次 |
| 最近记录: |