目前
概述了口令屏蔽
描述了用於口令屏蔽的
AWT/Swing 實用程序
為命令行輸入口令屏蔽問題
提供獨立於平台的解決方案
為口令屏蔽提供一個改進的解決方案(可靠而安全)
口令屏蔽
登錄屏幕和登錄對話框使用口令屏蔽技術
如果操作系統是 UNIX
AWT/Swing 中的口令屏蔽
如果您希望為您的應用程序提供圖形化的登錄對話框
TextField password = new TextField(
password
基於所使用字體的平均字符寬度
在 Swing 中
JPasswordField password = new JPasswordField(
password
命令行輸入屏蔽
和 AWT/Swing 不同
這裡我針對這個問題提出一個解決方案
然而
簡單的解決方案
這個解決方案使用一個單獨的線程
代碼示例
import java
class EraserThread implements Runnable {
private boolean stop;
/**
*@param The prompt displayed to the user
*/
public EraserThread(String prompt) {
System
}
/**
* Begin masking
*/
public void run () {
stop = true;
while (stop) {
System
try {
Thread
} catch(InterruptedException ie) {
ie
}
}
}
/**
* Instruct the thread to stop masking
*/
public void stopMasking() {
this
}
}
注意
PasswordField 類使用了 EraserThread 類
代碼示例
public class PasswordField {
/**
*@param prompt The prompt to display to the user
*@return The password as entered by the user
*/
public static String readPassword (String prompt) {
EraserThread et = new EraserThread(prompt);
Thread mask = new Thread(et);
mask
BufferedReader in = new BufferedReader(new InputStreamReader(System
String password =
try {
password = in
} catch (IOException ioe) {
ioe
}
// stop masking
et
// return the password entered by the user
return password;
}
}
作為如何使用 PasswordField 類的一個例子
代碼示例
class TestApp {
public static void main(String argv[]) {
String password = PasswordField
System
}
}
如果您在 Windows
使代碼安全而可靠
上述的簡單解決方案有一個主要缺陷
然而
為了確保跨線程的可見性
為了確保屏蔽能夠在系統高負荷運轉時也能夠出現
代碼示例
import java
/**
* This class attempts to erase characters echoed to the console
*/
class MaskingThread extends Thread {
private volatile boolean stop;
private char echochar =
/**
*@param prompt The prompt displayed to the user
*/
public MaskingThread(String prompt) {
System
}
/**
* Begin masking until asked to stop
*/
public void run() {
int priority = Thread
Thread
try {
stop = true;
while(stop) {
System
try {
// attempt masking at this rate
Thread
}catch (InterruptedException iex) {
Thread
return;
}
}
} finally { // restore the original priority
Thread
}
}
/**
* Instruct the thread to stop masking
*/
public void stopMasking() {
this
}
}
盡管使用 Strings 收集和存儲口令看起來似乎很合邏輯
代碼示例
import java
import java
/**
* This class prompts the user for a password and attempts to mask input with
*/
public class PasswordField {
/**
*@param input stream to be used (e
*@param prompt The prompt to display to the user
*@return The password as entered by the user
*/
public static final char[] getPassword(InputStream in
throws IOException {
MaskingThread maskingthread = new MaskingThread(prompt);
Thread thread = new Thread(maskingthread);
thread
char[] lineBuffer;
char[] buf;
int i;
buf = lineBuffer = new char[
int room = buf
int offset =
int c;
loop: while (true) {
switch (c = in
case
case
break loop;
case
int c
if ((c
if (!(in instanceof PushbackInputStream)) {
in = new PushbackInputStream(in);
}
((PushbackInputStream)in)
} else {
break loop;
}
default:
if (
buf = new char[offset +
room = buf
System
Arrays
lineBuffer = buf;
}
buf[offset++] = (char) c;
break;
}
}
maskingthread
if (offset ==
return null;
}
char[] ret = new char[offset];
System
Arrays
return ret;
}
}
最後
代碼示例
import java
public class PasswordApp {
public static void main(String argv[]) {
char password[] = null;
try {
password = PasswordField
} catch(IOException ioe) {
ioe
}
if(password == null ) {
System
} else {
System
}
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25934.html