Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fea342e

Browse filesBrowse files
committed
新增“什么在java中存放密码更倾向于char[]而不是String”
1 parent b8a64ab commit fea342e
Copy full SHA for fea342e

File tree

Expand file treeCollapse file tree

2 files changed

+17
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-2
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ stackoverflow-Java-top-qa
3636
* [反射是什么及其用途](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What-is-reflection-and-why-is-it-useful.md.md)
3737
* [为什么不能用string类型进行switch判断](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Why-can't-I-switch-on-a-String.md)
3838

39-
4039
> 编程技巧
4140
4241
* [去掉烦人的“!=null"(判空语句](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/avoiding-null-statements-in-java.md)
@@ -46,6 +45,7 @@ stackoverflow-Java-top-qa
4645
* [给3个布尔变量,当其中有2个或者2个以上为true才返回true](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Check-if-at-least-two-out-of-three-booleans-are-true.md)
4746
* [Java中打印一个数组最简单的方法是什么](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/What's-the-simplest-way-to-print-a-Java-array.md)
4847
* [为什么以下用随机生成的文字会得出 “hello world”?](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-does-this-code-using-random-strings-print-hello-world.md)
48+
* [什么在java中存放密码更倾向于char[]而不是String](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/why-is-cha[]-preferred-over-String-for-passwords-in-java.md)
4949

5050
> 网络
5151
@@ -62,14 +62,14 @@ stackoverflow-Java-top-qa
6262
* [如何测试 private 方法,变量或者内部类](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/How_to_test_a_class_that_has_private_methods,_fields_or_inner_classes.md)
6363

6464
> Android
65+
6566
* [在Android里面下载文件,并在ProgressDialog显示进度](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/Download_a_file_with_Android_and_showing_the_progress_in_a_ProgressDialog.md)
6667
* [如何获取Android设备唯一ID](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-there-a-unique-android-device-id.md)
6768

6869
### 待翻译问题链接(还剩x问题)
6970
- [Why is subtracting these two times (in 1927) giving a strange result?](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result)
7071
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
7172
- [Creating a memory leak with Java [closed]](http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java)
72-
- [Why is char[] preferred over String for passwords?](http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords)
7373
- [Why is printing “B” dramatically slower than printing “#”?](http://stackoverflow.com/questions/21947452/why-is-printing-b-dramatically-slower-than-printing)
7474
- [How can I create an executable jar with dependencies using Maven?](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-with-dependencies-using-maven)
7575
- [How to avoid Java code in JSP files?](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files)
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 为什么在java中存放密码更倾向于char[]相对于String
2+
3+
### 问题
4+
5+
在Swing中,password字段有一个getPassword()方法(返回char[]),而不是通常的getText()方法(返回字符串)。同样的,我遇到一个建议不要使用字符串处理密码。
6+
为什么在谈论passwords时,认为字符串会对安全构成威胁?感觉使用char[]不是那么的方便。
7+
8+
### 回答
9+
String是不可变的。这意味着,一旦你创建了一个String,如果另一个线程可以进行内存转存,在GC回收之前,没有办法可以摆脱数据(除了反射)。(这段翻译的不好,希望大家帮助改正)
10+
然而对于数组,你可以在使用完就明确的擦除它,你可以用任何你喜欢的数据覆盖这个数组,而且password不会出现在系统的任何地方,甚至在垃圾回收之前。
11+
所以,这是一个安全性的问题--但是,即使使用char[]也仅仅是降低了攻击者攻击的机会,而且仅仅对这种特定的攻击有效。
12+
编辑:正如评论中指出的,垃圾收集器在移动数组数据时可能会在内存中留下杂散的数据副本。我认为这是特定于实现的--GC会清除所有的将要清除的数据,避免这种情况。即使是这样,还是会存在char[]保存有password字段的时间可以被攻击。
13+
14+
**stackoverflow链接**
15+
http://stackoverflow.com/questions/8881291/why-is-char-preferred-over-string-for-passwords-in-java

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.