?? client.java
字號:
/**
* 客戶端
*/
package src;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.security.*;
import javax.crypto.spec.*;
import javax.crypto.*;
import javax.swing.*;
public class Client extends JFrame{
//GUI
private JTextField usernamefield;
private JPasswordField passwordfield;
private JTextArea textArea;
private JTextField messagefield;
private JPanel panel1,panel2;
private JButton connectButton ;
private JButton sendButton;
//
String IDc,ADc,password;
String IDtgs,IDs;
long TS1,TS2,TS3,TS4,TS5;
long Lifetime1,Lifetime2;
Ticket_tgs Tickettgs;
Ticket_s Tickets;
Authenticator_tgs at;
Authenticator_s as;
byte[] subkey1;
boolean pass;
//socket變量
Socket clientASSocket,clientTGSSocket,clientServerSocket;
int port;
ObjectOutputStream outstream,outstream2,outstream3;
ObjectInputStream instream,instream2,instream3;
//Key變量
PrivateKey clientprikey;
//加密解密輔助類實(shí)例
Encryption en;
public Client() throws Exception{
//GUI
super("SSKBS客戶端");
Container container = getContentPane();
container.setLayout( new BorderLayout());
panel1 = new JPanel();
panel1.setLayout( new FlowLayout());
container.add(panel1,BorderLayout.NORTH);
JLabel usernameLabel = new JLabel("帳號:");
panel1.add(usernameLabel);
usernamefield = new JTextField(10);
panel1.add(usernamefield);
JLabel passwordLabel = new JLabel("密碼:");
panel1.add(passwordLabel);
passwordfield = new JPasswordField(10);
panel1.add(passwordfield);
connectButton = new JButton("連接");
panel1.add(connectButton);
connectButton.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent event)
{
try{
IDc = usernamefield.getText();
authentication();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
);
textArea = new JTextArea(10,20);
textArea.setEditable(false);
container.add( new JScrollPane(textArea),BorderLayout.CENTER);
panel2 = new JPanel();
panel2.setLayout( new FlowLayout());
container.add(panel2,BorderLayout.SOUTH);
messagefield = new JTextField(20);
panel2.add(messagefield);
sendButton = new JButton("發(fā)送");
sendButton.addActionListener(
new ActionListener(){
public void actionPerformed (ActionEvent event)
{
try{
if(pass){
talkToServer("[From IP:"+ADc+"]->" +messagefield.getText());
}
else{
JOptionPane.showMessageDialog(null,"還未登錄驗(yàn)證,請先登錄驗(yàn)證后再發(fā)送消息!","", JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
);
panel2.add(sendButton);
setSize(400, 300);
setVisible(true);
en = new Encryption();
ADc = InetAddress.getLocalHost().toString();
}
/**
* 初始化client端的socket方法
* port:所要連接的服務(wù)器的端口:5000--AS
*/
public void initClientASSocket(int port)
{
this.port = port;
try {
clientASSocket = new Socket(InetAddress.getLocalHost(), port);
//clientASSocket = new Socket(InetAddress.getByName("10.130.202.251"), port);
outstream = new ObjectOutputStream(clientASSocket.getOutputStream());
instream = new ObjectInputStream(clientASSocket.getInputStream());
outstream.flush();
textArea.append("與AS通信的socket初始化完畢...\n");
System.out.println("與AS通信的socket初始化完畢");
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化client端的socket方法
* port:所要連接的服務(wù)器的端口:6000--TGS
*/
public void initClientTGSSocket(int port){
this.port = port;
try {
System.out.println(ADc+"*****"+port);
clientTGSSocket = new Socket(InetAddress.getLocalHost(), port);
//clientTGSSocket = new Socket(InetAddress.getByName("10.130.202.251"), port);
outstream2 = new ObjectOutputStream(clientTGSSocket.getOutputStream());
instream2 = new ObjectInputStream(clientTGSSocket.getInputStream());
outstream2.flush();
System.out.println("tgs socket init ok");
textArea.append("與TGS通信的socket初始化完畢...\n");
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 初始化client端的socket方法
* port:所要連接的服務(wù)器的端口:7000--Server
*/
public void initClientServerSocket(int port){
this.port = port;
try {
clientServerSocket = new Socket(InetAddress.getLocalHost(), port);
//clientServerSocket = new Socket(InetAddress.getByName("10.130.202.251"), port);
outstream3 = new ObjectOutputStream(clientServerSocket.getOutputStream());
instream3 = new ObjectInputStream(clientServerSocket.getInputStream());
outstream3.flush();
System.out.println("service socket init ok");
textArea.append("與Service Server通信的socket初始化完畢...\n");
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* 加密解密部分
* AES加密方法
* @param plainText 明文
* @param k 密鑰
* @return 密文
* @throws Exception
*/
public byte[] AESencrypt(byte[] plainText,Key k)throws Exception
{
Cipher cipher=Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE,k);
byte[] cipherText=cipher.doFinal(plainText);
return cipherText;
}
/**
* 加密解密部分
* AES解密方法
* @param t 密文
* @param k 密鑰
* @return 明文
* @throws Exception
*/
public String AESdecrypt(byte[] t,Key k)throws Exception
{
Cipher cipher=Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE,k);
byte[] newPlainText = cipher.doFinal(t);
return new String(newPlainText,"UTF8");
}
/**
* 對明文進(jìn)行加密,這里都是用client的私鑰加密,所以采用RSA非對稱加密算法
* @param t 明文
* @param k client的私鑰
* @return 密文
* @throws Exception
*/
public byte[] decrypt(byte[] t,Key k)throws Exception
{
Cipher cipher=Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE,k);
byte[] newPlainText = cipher.doFinal(t);
System.out.println("用client的私鑰解密完畢,client_privatekey: "+new String(newPlainText,"UTF8"));
textArea.append("用client的私鑰解密完畢,client_privatekey:"+new String(newPlainText)+"...\n");
return newPlainText;
}
//讀取客戶端密鑰c_private.rsa
public void getKeyFromFile()
{
try
{
System.out.println(System.getProperty("user.dir"));
String file = "c_private.rsa";
ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
clientprikey = (PrivateKey)in.readObject();
System.out.println(clientprikey);
//textArea.append("用password解密完畢,已獲得當(dāng)前client的私鑰"+clientprikey+"...\n");
in.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* 生成一個(gè)時(shí)間戳。
* @return
*/
public long getTimeStamp()
{
long now = System.currentTimeMillis();
return now;
}
/**
* 判斷當(dāng)前會(huì)話是否在有效期內(nèi)
* @param ts1
* @param ts2
* @param lifetime 有效期的長度
* @return true 有效 ;false 無效
*/
public boolean isInSession(long ts1,long ts2,long lifetime)
{
if(ts1 + lifetime > ts2)
return true;
else return false;
}
/**
* 驗(yàn)證步驟
* 驗(yàn)證第一步,
* 向 Authenticator Server 認(rèn)證
* 如果用戶名未samantha則繼續(xù)認(rèn)證
* 如果不是則退出認(rèn)證
*/
public boolean Step1(){
textArea.append("開始向Authenticator Server發(fā)送認(rèn)證請求...\n");
try
{
/**
* (1)C->AS: IDc || ADc || TS1
*/
TS1 = getTimeStamp();
C_AS c_as = new C_AS(IDc,ADc,TS1);
//Client->AS
sendmessage(c_as,outstream);
/**AS->Client
* (2)AS->C:
* Tickettgs = Epub-tgs[Kpub-c || IDc || ADc || IDtgs || TS2 || Lifetime1]
* Epub-c[IDtgs || TS2 || Lifttime1 || Tickettgs]
*/
while(true)
{
Object object = receivemessage(instream);
//用戶名不合法
if(object!=null&&object instanceof String)
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -