在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.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;
}

关于定时攻击的更多资源:

定时攻击的教训关于信息安全堆栈交换上定时攻击的讨论当然,定时攻击维基百科页面

大小写字符串:

    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

引用一份官方文档,Java Cryptography Architecture指南介绍了char[]与String密码(关于基于密码的加密,但这更一般地是关于密码):

将密码收集并存储在对象中似乎是合乎逻辑的然而,这里有一个警告:类型String是不可变的,即没有定义允许您更改(覆盖)或清零字符串的内容使用后。此功能使字符串对象不适合存储诸如用户密码之类的安全敏感信息。你应始终在char数组。

Java编程语言4.0版安全编码指南2-2也提到了类似的内容(尽管最初是在日志记录的上下文中):

准则2-2:不记录高度敏感信息一些信息,如社会安全号码(SSN)和密码是高度敏感的。此信息不应保留无论在什么地方,甚至在管理员。例如,不应将其发送到日志文件和它的存在不应该通过搜索来检测。一些瞬态数据可以保存在可变数据结构中,例如char数组,以及使用后立即清除。清除数据结构减少了当对象移入时,在典型的Java运行时系统上的有效性存储器对程序员来说是透明的。本指南还对实施和使用没有数据语义知识的低级库他们正在处理。例如,低级字符串解析库可以记录其工作的文本。应用程序可以解析SSN与图书馆。这会导致SSN管理员可以访问日志文件。

虽然这里的其他建议似乎有效,但还有一个很好的理由。使用纯字符串时,意外将密码打印到日志、监视器或其他不安全的地方的可能性要大得多。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

我认为这不是一个有效的建议,但是,我至少可以猜出原因。

我认为动机是希望确保在密码被使用后,您可以及时且确定地删除记忆中的所有密码痕迹。使用char[],您可以用空白或确定的内容覆盖数组的每个元素。不能这样编辑字符串的内部值。

但这并不是一个好答案;为什么不确保对char[]或String的引用不转义?那么就没有安全问题了。但问题是,String对象在理论上可以被内部化,并在常量池中保持活动状态。我想使用char[]禁止这种可能性。