?? register.java
字號:
/*
* register.java
*
* Created on 2007年12月20日, 下午7:37
*/
/**
*
* @author Admin
*用戶注冊窗口
*/
import java.io.*;
import java.net.*;
import java.lang.*;
import javax.swing.*;
public class register extends javax.swing.JFrame {
private Socket skt = null;
//*************************************************************************************************************************
private static String rsaN = "BE211D1627D0FA0EDE557CB342256C9B6B7CEDB5A727BF732499393428EF2EC8704ADE733C65B234" +
"0B9C1DA282A57EC3B8352D6382E3DC32951EE741181A60FF";
private static String rsaD = "463367CFF4679ECCBA9F09EA0E344F439B8E871E99577DC2FFF26B42AD12934CDE8A43617F97D277" +
"F856A42EA37D9936A2CE81351A432EB9B8A4DF818DA506F1";//D,N public key
private static String rsaE = "10001";//E,N private key
//*************************************************************************************************************************
/** Creates new form register */
public register() {
initComponents();
}
private void request() //向服務器發送請求消息,請求建立連接.此消息明文發送;
{
try{
skt = new Socket("127.0.0.1",2000);
DataOutputStream dos = new DataOutputStream(skt.getOutputStream());
dos.writeBytes("1");
dos.close();
skt.close();
}catch(UnknownHostException unex)
{
System.err.println(unex);
}catch(IOException ioex)
{
System.err.println(ioex);
}
}
private void logonAndnewuser(int mode, String username, String password, String sessionkey) //向服務器發送用戶名,密碼,session key;
{
String plaintext = mode+username+"&"+password+"&"+sessionkey;
String ciphertext = EncryptAndDecrypt.encryption(plaintext, "", "RSA"); //此消息RSA加密;
try{
skt = new Socket("127.0.0.1", 2000);
DataOutputStream dos = new DataOutputStream(skt.getOutputStream());
dos.writeBytes(ciphertext);
dos.close();
skt.close();
}catch(UnknownHostException uhex)
{
System.err.println(uhex);
}catch(IOException ioex)
{
System.err.println(ioex);
}
}
private void errormessage() //發送錯誤消息,表明接受到非預期的消息格式;
{
try{
skt = new Socket("127.0.0.1",2000);
DataOutputStream dos = new DataOutputStream(skt.getOutputStream());
dos.writeBytes("0");
dos.close();
skt.close();
}catch(UnknownHostException unex)
{
System.err.println(unex);
}catch(IOException ioex)
{
System.err.println(ioex);
}
}
private String receive() //接受服務器的返回消息,連接服務器的2001端口;
{
String recString = "";
try{
skt = new Socket("127.0.0.1", 2001);
BufferedReader binReader = new BufferedReader(new InputStreamReader(skt.getInputStream()));
recString = binReader.readLine();
skt.close();
}catch(UnknownHostException ex){
System.err.println(ex);
}catch(IOException ex){
System.err.println(ex);
}
return recString;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" 生成的代碼 ">//GEN-BEGIN:initComponents
private void initComponents() {
registerLabel = new javax.swing.JLabel();
usernameTF = new javax.swing.JTextField();
UsernameLabel = new javax.swing.JLabel();
passwordLabel = new javax.swing.JLabel();
passwordJPF = new javax.swing.JPasswordField();
passwordConfirmLabel = new javax.swing.JLabel();
passwordConfirmJPF = new javax.swing.JPasswordField();
registerBtn = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Register");
setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
setResizable(false);
registerLabel.setText("Register");
UsernameLabel.setText("Username");
passwordLabel.setText("Password");
passwordConfirmLabel.setText("Input password again");
registerBtn.setText("Submit");
registerBtn.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
registerBtnActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(passwordConfirmLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(passwordLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(passwordJPF, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE)
.addComponent(UsernameLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(registerLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
.addComponent(usernameTF)
.addComponent(passwordConfirmJPF))
.addContainerGap(208, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(262, Short.MAX_VALUE)
.addComponent(registerBtn)
.addGap(67, 67, 67))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(registerLabel)
.addGap(29, 29, 29)
.addComponent(UsernameLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(usernameTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19)
.addComponent(passwordLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(passwordJPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(20, 20, 20)
.addComponent(passwordConfirmLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(passwordConfirmJPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(registerBtn)
.addContainerGap(26, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void registerBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_registerBtnActionPerformed
// TODO 將在此處添加您的處理代碼:
String username = usernameTF.getText();
String password = String.valueOf(passwordJPF.getPassword());
String passwordConfirm = String.valueOf(passwordConfirmJPF.getPassword());
String ke = RC4.keyGet();
if(password.equals(passwordConfirm)) //兩次密碼相同;
{
String rec = "";
request();
rec = receive(); //接受服務器返回預期消息格式為 1&公鑰的N&公鑰的d
if(rec.equals("1&"+rsaN+"&"+rsaD));
{
logonAndnewuser(1, username, password, ke);
}
rec = receive(); //接受服務器返回,預期消息為welcom或not exist或user existed;
rec = EncryptAndDecrypt.decryption(rec, ke, "RC4");
if(rec.equals("welcome"))
{
JOptionPane.showMessageDialog(null,"register successfully, you can log on now.", "welcome", JOptionPane.INFORMATION_MESSAGE);
this.setVisible(false);
new logon().setVisible(true);
}else if(rec.equals("not exist"))
{
JOptionPane.showMessageDialog(null,"wrong username or password.", "Sorry", JOptionPane.INFORMATION_MESSAGE);
}else if(rec.equals("user existed"))
{
JOptionPane.showMessageDialog(null,"user has been existed ","Sorry", JOptionPane.INFORMATION_MESSAGE);
}
}else
{
JOptionPane.showMessageDialog(null,"password is illegal ","Error", JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_registerBtnActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new register().setVisible(true);
}
});
}
// 變量聲明 - 不進行修改//GEN-BEGIN:variables
private javax.swing.JLabel UsernameLabel;
private javax.swing.JPasswordField passwordConfirmJPF;
private javax.swing.JLabel passwordConfirmLabel;
private javax.swing.JPasswordField passwordJPF;
private javax.swing.JLabel passwordLabel;
private javax.swing.JButton registerBtn;
private javax.swing.JLabel registerLabel;
private javax.swing.JTextField usernameTF;
// 變量聲明結束//GEN-END:variables
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -