在Swing中,密码字段有一个getPassword()(returns char[])方法,而不是通常的getText()(return String)方法。同样,我也遇到了一个建议,不要使用String来处理密码。
为什么字符串对密码安全构成威胁?使用char[]感觉不方便。
在Swing中,密码字段有一个getPassword()(returns char[])方法,而不是通常的getText()(return String)方法。同样,我也遇到了一个建议,不要使用String来处理密码。
为什么字符串对密码安全构成威胁?使用char[]感觉不方便。
当前回答
大小写字符串:
String password = "ill stay in StringPool after Death !!!";
// some long code goes
// ...Now I want to remove traces of password
password = null;
password = "";
// above attempts wil change value of password
// but the actual password can be traced from String pool through memory dump, if not garbage collected
案例CHAR阵列:
char[] passArray = {'p','a','s','s','w','o','r','d'};
// some long code goes
// ...Now I want to remove traces of password
for (int i=0; i<passArray.length;i++){
passArray[i] = 'x';
}
// Now you ACTUALLY DESTROYED traces of password form memory
其他回答
大小写字符串:
String password = "ill stay in StringPool after Death !!!";
// some long code goes
// ...Now I want to remove traces of password
password = null;
password = "";
// above attempts wil change value of password
// but the actual password can be traced from String pool through memory dump, if not garbage collected
案例CHAR阵列:
char[] passArray = {'p','a','s','s','w','o','r','d'};
// some long code goes
// ...Now I want to remove traces of password
for (int i=0; i<passArray.length;i++){
passArray[i] = 'x';
}
// Now you ACTUALLY DESTROYED traces of password form memory
编辑:经过一年的安全研究后,我回到了这个答案,我意识到这意味着你可能会比较明文密码。请不要。使用一个安全的单向散列和一个合理的迭代次数。考虑使用图书馆:这东西很难弄对!
原始答案:String.equals()使用短路求值,因此容易受到定时攻击,这一事实如何?这可能不太可能,但理论上可以对密码比较计时,以确定正确的字符序列。
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
// Quits here if Strings are different lengths.
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
// Quits here at first different character.
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
关于定时攻击的更多资源:
定时攻击的教训关于信息安全堆栈交换上定时攻击的讨论当然,定时攻击维基百科页面
有些人认为,一旦您不再需要密码,就必须覆盖用于存储密码的内存。这缩短了攻击者从系统读取密码的时间窗口,并完全忽略了这样一个事实,即攻击者已经需要足够的访问权限来劫持JVM内存。具有如此多访问权限的攻击者可以捕获您的关键事件,使其完全无用(AFAIK,所以如果我错了,请纠正我)。
使现代化
感谢这些评论,我不得不更新我的答案。显然,在两种情况下,这可以增加(非常)轻微的安全改进,因为它可以减少密码在硬盘上的时间。但我仍然认为这对大多数用例来说都是过度的。
您的目标系统可能配置不正确,或者您必须假设它是错误的,并且您必须对核心转储(如果系统不是由管理员管理的,则可能是有效的)心存疑虑。您的软件必须过于偏执,以防止攻击者使用TrueCrypt(已停产)、VeraCrypt或CipherShed等工具访问硬件时发生数据泄漏。
如果可能,禁用内核转储和交换文件可以解决这两个问题。然而,它们需要管理员权限,可能会减少功能(使用的内存更少),从运行系统中取出RAM仍然是一个值得关注的问题。
虽然这里的其他建议似乎有效,但还有一个很好的理由。使用纯字符串时,意外将密码打印到日志、监视器或其他不安全的地方的可能性要大得多。char[]不那么脆弱。
考虑一下:
public static void main(String[] args) {
Object pw = "Password";
System.out.println("String: " + pw);
pw = "Password".toCharArray();
System.out.println("Array: " + pw);
}
打印:
String: Password
Array: [C@5829428e
Java中的字符串是不可变的。因此,无论何时创建字符串,它都将保留在内存中,直到被垃圾收集。因此,任何访问内存的人都可以读取字符串的值。
如果字符串的值被修改,那么它将最终创建一个新字符串。因此,原始值和修改后的值都保留在内存中,直到被垃圾收集。
使用字符数组,一旦达到密码的目的,就可以修改或删除数组的内容。在修改数组之后,甚至在垃圾收集开始之前,都不会在内存中找到数组的原始内容。
出于安全考虑,最好将密码存储为字符数组。