방치하기

현재 만들고있는 메신저 프로그램 본문

카테고리 없음

현재 만들고있는 메신저 프로그램

Yi Junho 2009. 8. 1. 11:02
반응형
import javax.swing.*;
import java.awt.*;
import java.awt.Color;
import java.awt.event.*;

import java.security.InvalidKeyException;
import java.security.Key;
 
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
 
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


//안호화 클래스 
class cPW{
    //DESede 는 암호화시 사용되는 최대 키 사이즈를 지정하는 키워드임 : 관련페이지 내용 링크
    private static String algorithm = "DESede";
    private static Key    key       = null;
    private static Cipher cipher    = null;
 
    public static String returnEncryptCode(String str) throws Exception {
        byte [] encryptionBytes = null;
       
        setUp();
              
        // 입력받은 문자열을 암호화 하는 부분
        encryptionBytes = encrypt( str );
        BASE64Encoder encoder = new BASE64Encoder();
         String encodeString = encoder.encode(encryptionBytes);
         //encoder.encode(encryptionBytes) 으로 encrypt 된 값 출력
        return encodeString;
    }

   private static void setUp() throws Exception {
        key = KeyGenerator.getInstance( algorithm ).generateKey();
        cipher = Cipher.getInstance( algorithm );
    }

    public static String returnDecryptCode(String str) throws Exception {
        BASE64Decoder decoder = new BASE64Decoder();
        String decode = decrypt( decoder.decodeBuffer(str) );
        return decode;
    }
 
    // encryptionBytes = encrypt( input ), input을 변조하여 encryptionBytes에 대입함.
    private static byte [] encrypt(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException  {
        cipher.init( Cipher.ENCRYPT_MODE, key );
        byte [] inputBytes = input.getBytes();
        return cipher.doFinal( inputBytes );
    }
 
    //decrypt( decoder.decodeBuffer(encodeString) ) 처리부분.
    private static String decrypt(byte [] encryptionBytes) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        cipher.init( Cipher.DECRYPT_MODE, key );
        byte [] recoveredBytes = cipher.doFinal( encryptionBytes );
        String recovered = new String( recoveredBytes );
        return recovered;
    }
 
}


















public class LogIn extends JFrame {
       	
       
    private JLabel jLabel1 = new JLabel("아이디");
    private JTextField jTextField1 = new JTextField(8);
	private JPasswordField jPasswordField1 = new JPasswordField(8);
	private JLabel jLabel2 ;
	private JButton jButton1;
	private JButton jButton2;
	private JButton jButton3;
	private JCheckBox jCheckBox1 = new JCheckBox("아이디저장");
	private JCheckBox jCheckBox2 = new JCheckBox("패스워드저장");
	static String option="1";		

    public LogIn() //view
	{
		super("Moon-LogIn");
		ImageIcon mainIcon = new ImageIcon("mmicon.png");
		setIconImage(mainIcon.getImage()); 
        initComponents();
    }
     
    
		

    private void initComponents() {
		
		String savedID;
		String savedPW;
		if ("0".equals(option))
		{
		}
		if ("1".equals(option))
		{
			System.out.println("아이디 저장모드");
		}
		if ("2".equals(option))
		{
			System.out.println("아이디&비번 저장모드");
		}
		Handler handler=new Handler();
		setSize(300,450);
		setLayout(null);
		setBackground(Color.RED);
		setVisible(true);
		jLabel2 = new JLabel("패스워드");
		//라벨들
		add(jLabel1);
		add(jLabel2);
		jLabel1.setBounds(33, 280, 200, 30);		
		jLabel2.setBounds(20, 300, 200, 30);
		//텍스트 필드들
		add(jTextField1);
		jTextField1.setBounds(75, 285, 120, 20);
		
		add(jPasswordField1);
		jPasswordField1.setBounds(75, 305, 120, 20);
		
		
		
		//로그인 버튼
		ImageIcon mainIcon = new ImageIcon("mmicon.png");
		jButton1= new JButton("로그인");
		add(jButton1);
		jButton1.setBounds(197, 285, 75, 40);
		//회원가입
		jButton2 =new JButton("회원가입") ;
		add(jButton2);
		jButton2.setBounds(95, 370, 100, 40);
		
		

		//체크 박스.
		add(jCheckBox1);
		add(jCheckBox2);
		jCheckBox1.setBackground(Color.WHITE);
		jCheckBox2.setBackground(Color.WHITE);
		jCheckBox1.setBounds(60, 330, 100, 40);
		jCheckBox2.setBounds(160, 330, 140, 40);
		jCheckBox2.setEnabled(false);
		// 메인 화면
		ImageIcon main = new ImageIcon("mm.png");
		jButton3 = new JButton(main);
		jButton3.setBorderPainted(false);
		jButton3.setContentAreaFilled(false);
		jButton3.setFocusPainted(false);
		jButton3.setBounds(0, 0, 300, 430);
		add(jButton3);
		//핸들러 모음 
		jCheckBox1.addItemListener( handler );
		jCheckBox2.addItemListener( handler );
		jButton1.addActionListener( handler );
		jButton2.addActionListener( handler );
		jPasswordField1.addActionListener( handler );
		

	}
private class Handler implements ActionListener,ItemListener {
      public void actionPerformed( ActionEvent event ){
//Model
		System.out.println(event.getActionCommand());
		new Join();
	  }        
		 
		 
		 public void itemStateChanged( ItemEvent event )
		  {
			  if ( event.getSource() == jCheckBox1 )
			  {
				 if(jCheckBox1.isSelected())
				 {
					jCheckBox2.setEnabled(true);
				 }
				 
				 else if (!(jCheckBox1.isSelected()))
				 {
					jCheckBox2.setSelected(false);
					jCheckBox2.setEnabled(false);
				 }
				
			  }
			  if ( event.getSource() == jCheckBox2 )
			  {
				 if(jCheckBox1.isSelected())
				 {
					 if(jCheckBox2.isSelected())
					 {
						 System.out.println("완벽");
					 }
				 }
				 
				 else if (!(jCheckBox1.isSelected()))
				 {
					 	jCheckBox2.setSelected(false);						 
				 }
				
			  }
			  
			   
		  }
	
	
	}
 

    public static void main(String args[]) {
        LogIn jh=new LogIn();
            
	}
    

    
}























// 여기부터 새 Class
class Join extends JFrame
{
        private JLabel jLabel1 = new JLabel("ID");
		private JLabel jLabel2 = new JLabel("PW");
        private JLabel jLabel3 = new JLabel("비밀번호는 6~8자리만 생성 가능 합니다.");
		private JLabel jLabel4 = new JLabel("ㅡ");
		private JLabel jLabel5 = new JLabel("주민등록번호");
		private JLabel jLabel6 = new JLabel("E-Mail");
		private static JTextField jTextField1 = new JTextField(8);
        private static JPasswordField jTextField2 = new JPasswordField(8);  
        private static JTextField jTextField3 = new JTextField(6);
		private static JTextField jTextField4 = new JTextField(6);
		private static JTextField jTextField5 = new JTextField(8);
        private JButton jButton1= new JButton("ID중복검사");
        private JButton jButton2 =new JButton("회원가입") ;
		static String sendPW;

		public String getText1()
		{
			return this.jTextField1.getText();
		}
		public String getText2()
		{
			return this.jTextField2.getText();
		}
		public String getText3()
		{
			return this.jTextField3.getText();
		}
		public String getText4()
		{
			return this.jTextField4.getText();
		}
		public String getText5()
		{
			return this.jTextField5.getText();
		}
		public boolean pwLength(String a)
		{
			if(a.length()>5 &&a.length()<9)
			{	
				return true;
			}
			else
			{
				return false;
			}
		}

	public Join() //view1
		{
			super("회원가입");
			
			ImageIcon mainIcon = new ImageIcon("mmicon.png");
			setIconImage(mainIcon.getImage()); 
			initComponents();
		}
	public Join(int a) //view1
		{
			
		}

	private void initComponents() 
		{
			setSize(280,200);
			setLocation(300, 120);//
			setLayout(null);
			
			setVisible(true);
			
			//라벨들
			add(jLabel1);
			add(jLabel2);
			add(jLabel3);
			add(jLabel4);
			add(jLabel5);
			add(jLabel6);
			jLabel1.setBounds(10, 0, 50, 30);		
			jLabel2.setBounds(5, 30,50, 30);
			jLabel3.setBounds(5, 50,250, 40);
			jLabel3.setForeground(Color.RED);
			jLabel4.setBounds(140, 85,20, 20);
			jLabel5.setBounds(0, 85,80, 20);
			jLabel6.setBounds(2, 110,80, 20);
			
			//텍스트 필드들2
			add(jTextField1);
			jTextField1.setBounds(40, 5, 120, 20);
			add(jTextField2);
			jTextField2.setBounds(40, 35, 120, 20);
			add(jTextField3);
			jTextField3.setBounds(90, 85, 50, 20);
			add(jTextField4);
			jTextField4.setBounds(155, 85, 55, 20);
			add(jTextField5);
			jTextField5.setBounds(40, 110, 170, 20);
			
			 
	  

			
			add(jButton1);
			jButton1.setBounds(160, 5, 100, 20);


			//회원가입
			
			add(jButton2);
			jButton2.setBounds(80, 135, 100, 20);
			Hand handler=new Hand();
			jButton1.addActionListener( handler );
			jButton2.addActionListener( handler );




			//텍스트필드 글자수들 제한 콘트롤러들 
			jTextField1.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent ke) {
					JTextField src = (JTextField) ke.getSource();
					if(src.getText().getBytes().length>=10) ke.consume();
				}
			}
		  );
			
			jTextField2.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent ke) {
					JTextField src = (JTextField) ke.getSource();
					if(src.getText().length()>=8) ke.consume();
				}
			}
		  );
			jTextField3.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent ke) {
					 
					
					
					if (Character.isDigit(  ke.getKeyChar()) ||'\b'==ke.getKeyChar()  ) //숫자와 백스페이스만 경고창 없이 진행 
					{
						
						JTextField src= (JTextField) ke.getSource();;
						if(src.getText().length()>=6) ke.consume();
						
						
					}
					
					
					else
					{
						
						JOptionPane.showMessageDialog(null,"숫자를 입력해주세요" );	
						
				
					}
					

				}
			}
		  );
			jTextField4.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent ke) {
									
					if (Character.isDigit(  ke.getKeyChar()) ||'\b'==ke.getKeyChar()  )
					{
						JTextField src= (JTextField) ke.getSource();;
						if(src.getText().length()>=7) ke.consume();
						
						
					}
					
					
					else
					{
						
						JOptionPane.showMessageDialog(null,"숫자를 입력해주세요" );	
							
					}
			   }
			}
		  );	
			jTextField5.addKeyListener(new KeyAdapter() {
				public void keyTyped(KeyEvent ke) {
					JTextField src = (JTextField) ke.getSource();
					if(src.getText().length()>=25) ke.consume();
			   }
			}
		  );	
	












			


	}
}
 
 
 
 //회원가입의 이벤트 .
 class Hand implements ActionListener{
         
		 
		public void actionPerformed( ActionEvent event )
		{
			
			cPW l =new cPW();

			if ( event.getActionCommand().equals("ID중복검사") )
			{
				System.out.println(new Join(1).getText1());
			}
			if ( event.getActionCommand().equals("회원가입"))
			{

				String str=new Join(1).getText2();
				if (new Join(1).pwLength(new Join(1).getText2()))
				{	
				
					try
					{	System.out.println("암호화된 패스워드");
						System.out.println(l.returnEncryptCode("천재"));
						System.out.println(l.returnEncryptCode(new Join(1).getText2()));
					}
						
						
					
					catch (Exception e)
					{
					}
				}
				else 
					JOptionPane.showMessageDialog(null,"패스워드는 6~8자리로 생성해주세요." );	
			
			}
		}	
	}
반응형
Comments