⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 face.java

📁 用于个人账户信息保护的。基于DES规则的密码保护!
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

				JPanel helpPanel8 = new JPanel();
				JLabel helpLabel8 = new JLabel("Copyright : a1110536@Yu Jia");	
				helpPanel8.add(helpLabel8);
					
				JPanel helpPanel7 = new JPanel();
				helpPanel7.setLayout(new BoxLayout( helpPanel7, BoxLayout.Y_AXIS ));
					
				helpPanel7.add(helpPanel );
				helpPanel7.add(helpPanel2 );
				helpPanel7.add(helpPanel3 );
				helpPanel7.add(helpPanel4 );
				helpPanel7.add(helpPanel5 );
				helpPanel7.add(helpPanel6 );
				helpPanel7.add(helpPanel8 );
					
				help.getContentPane().add(helpPanel7, BorderLayout.CENTER);				
				JButton tmpButton = new JButton("OK");
				JLabel lb = new JLabel();
				JPanel pl = new JPanel();
				pl.add(lb);
				pl.add(tmpButton);
				pl.add(lb);
				help.getContentPane().add(pl, BorderLayout.SOUTH);
				help.show();

				tmpButton.addActionListener(new ActionListener() 
				{	
						public void actionPerformed(ActionEvent e1)
						{
							help.setVisible(false);
							help.dispose();
						}
				});
			}
		}
		ActionListener listener  = new MenuItemListener();
		item.addActionListener(listener);
		return item;
	}

//---------------------------------------------------Button-----------------------------------------------------------------//
	// Event handler for  Username button
	private JButton createUsernameButton()
	{
		JButton username = new JButton("Username");
		return username;
	}

	// Event handler for  Password button
	private JButton createPasswordButton()
	{
		JButton password = new JButton("Password");
		return password;
	}

	// Event handler for  Remark button
	private JButton createRemarkButton()
	{
		JButton remark = new JButton("Remark");
		return remark;
	}
	
	// Event handler for pressing the Eidt button
	private JButton createEditButton()
	{
		JButton edit = new JButton("Edit");
		class ButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
				index = list.getSelectedIndex();

				final JDialog newInformation = new JDialog(frame, "Edit Information");
				newInformation.setSize(400,400);
				newInformation.getContentPane().setLayout(new BorderLayout());
					
				JPanel tmpPanel = new JPanel();
				JLabel tmpLabel = new JLabel("New Username: ");
				final TextField tmpText = new TextField( ( ( Information )inforList.get( index ) ).getName(), 30);
				tmpPanel.add(tmpLabel);
				tmpPanel.add(tmpText);
					
				JLabel setInit = new JLabel("New Password: ");
				final TextField tmpText2 = new TextField( ( ( Information )inforList.get( index ) ).getPassword(),30);
				tmpText2.setEchoChar('*');
				JPanel tmpPanel2 = new JPanel();
				tmpPanel2.add(setInit);
				tmpPanel2.add(tmpText2);
					
				JPanel tmpPanel3 = new JPanel();
				JLabel tmpLabel2 = new JLabel("New Remark: ");
				final TextField tmpText3 = new TextField( ( ( Information )inforList.get( index ) ).getRemark(),30);
				tmpPanel3.add(tmpLabel2);
				tmpPanel3.add(tmpText3);
					
				JPanel tmpPanel4 = new JPanel();
				tmpPanel4.setLayout(new BoxLayout( tmpPanel4, BoxLayout.Y_AXIS ));
					
				tmpPanel4.add(tmpPanel );
				tmpPanel4.add(tmpPanel2);
				tmpPanel4.add(tmpPanel3);
					
				newInformation.getContentPane().add(tmpPanel4, BorderLayout.CENTER);				
				JButton tmpButton = new JButton("OK");
				JLabel lb = new JLabel();
				JPanel pl = new JPanel();
				pl.add(lb);
				pl.add(tmpButton);
				pl.add(lb);
				newInformation.getContentPane().add(pl, BorderLayout.SOUTH);
				newInformation.show();
					
				tmpButton.addActionListener(new ActionListener() 
				{
						public void actionPerformed(ActionEvent e1)
						{
							String n = tmpText.getText();
							String p = tmpText2.getText();
							String r = tmpText3.getText();

							( ( Information )inforList.get( index ) ).setName( n );
							( ( Information )inforList.get( index ) ).setPassword( p );
							( ( Information )inforList.get( index ) ).setRemark( r );
							
							String[][] temp = new String[inforList.size()][3];

							for ( int i=0; i<inforList.size(); i++)
							{
								temp[i][0] = ( ( Information )inforList.get( i ) ).getName(); 
								temp[i][1] = ( ( Information )inforList.get( i ) ).getPassword(); 
								temp[i][2] = ( ( Information )inforList.get( i ) ).getRemark();
							}
				
							list.setListData(temp);
							list.setCellRenderer(new MyCellRenderer());
							
							try
							{
								writeFile();
							}
							catch (Exception e)
							{
								System.out.println(e);
							}
							
							newInformation.setVisible(false);
							newInformation.dispose();
							pb.repaint();
						}
				});
			}
		}
		ActionListener listener  = new ButtonListener();
		edit.addActionListener(listener);
		return edit;
	}

	// Event handler for pressing the Delete button
	private JButton createDeleteButton()
	{
		JButton delete = new JButton("Delete");
		class ButtonListener implements ActionListener
		{
			public void actionPerformed(ActionEvent event)
			{
				index = list.getSelectedIndex();
				if (index >= 0)
				{
					inforList.removeElementAt(index);

					String[][] temp = new String[inforList.size()][3];

					for ( int i=0; i<inforList.size(); i++)
					{
						temp[i][0] = ( ( Information )inforList.get( i ) ).getName(); 
						temp[i][1] = ( ( Information )inforList.get( i ) ).getPassword(); 
						temp[i][2] = ( ( Information )inforList.get( i ) ).getRemark();
					}
					
					list.setListData(temp);
					list.setCellRenderer(new MyCellRenderer());
								
					try
					{
						writeFile();
					}
					catch (Exception e)
					{
						System.out.println(e);
					}
				}
				else
				{
					JOptionPane.showMessageDialog(
												   null,
												   "You should select one row!",
												   "Notice",
												   JOptionPane.WARNING_MESSAGE);
				}
			}
		}
		ActionListener listener  = new ButtonListener();
		delete.addActionListener(listener);
		return delete;
	}

	// to write information file
	public void writeFile() throws Exception 
	{
		String temp = "";
		if (inforList.size() == 0)
		{
			temp += "0\r\n" ;
			temp += Integer.toString( securityFlag ) + "\r\n";
			temp += pw + "\r\n";
		}
		else
		{
			temp = temp + Integer.toString( inforList.size() ) + "\r\n";
			temp += Integer.toString( securityFlag ) + "\r\n";
			temp += pw + "\r\n";
			for ( int i=0; i<inforList.size(); i++)
			{
				temp += ( ( Information )inforList.get( i ) ).getName() + "\r\n";
				temp += ( ( Information )inforList.get( i ) ).getPassword() + "\r\n";
				temp += ( ( Information )inforList.get( i ) ).getRemark() + "\r\n";
			}
		}

		byte[] b = temp.getBytes();
		byte[] data = getencryptData( b );

		BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(file));
		bufferedOutputStream.write(data);
		bufferedOutputStream.close();
	}
	
	// to write key file
	public void writeKeyFile(byte[] data,String fn) throws IOException 
	{
		//File f =new File(fn);
		BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream(keyfile));
		bufferedOutputStream.write(data);
		bufferedOutputStream.close();
    }
	
	// to read information file
	public byte[] readFile(String fn) throws IOException 
	{
		if( fn==null || fn.equals("") )
		{
			throw new NullPointerException("File is not exist.");
		}
		long len = file.length();
		byte[] bytes = new byte[(int)len];

		BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
		int r = bufferedInputStream.read( bytes );
		if (r != len)
		  throw new IOException("Can not read file.");
		bufferedInputStream.close();
		
		return bytes;
	}
	
	// to read key file
	public byte[] readKeyFile(String fn) throws IOException 
	{
//		File f = new File( fn );
		if( fn==null || fn.equals("") )
		{
			throw new NullPointerException("File is not exist.");
		}
		long len = keyfile.length();
		byte[] bytes = new byte[(int)len];

		BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(keyfile));
		int r = bufferedInputStream.read( bytes );
		if (r != len)
		  throw new IOException("Can not read file.");
		bufferedInputStream.close();
		
		return bytes;
	}

	//to decrypt information file
	public byte[] getunEncryptData(byte[] bytes) throws Exception
    {
		//to create a believable random number
		SecureRandom sr = new SecureRandom();

		// to get the key from key file
		byte[] rawKeyData = readKeyFile(keyfilename);

		// to create a object of DESKeySpec
		Class classkeyspec=Class.forName("javax.crypto.spec.DESKeySpec");
		Constructor constructor = classkeyspec.getConstructor(new Class[]{byte[].class});
		KeySpec dks = (KeySpec) constructor.newInstance(new Object[]{rawKeyData}); //new DESKeySpec(rawKeyData);

		// to create a key factory, then use it to become DESKeySpec to Secret Key
		SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( algorithm );
		SecretKey key = keyFactory.generateSecret(dks);

		// Cipher instantiation
		Cipher cipher = Cipher.getInstance( algorithm );

		// to initialise the object of Cipher
		cipher.init(Cipher.DECRYPT_MODE, key, sr);
		
		//to decrypt
		bytes = cipher.doFinal(bytes);
		
		return bytes;
    }
	
	//to encrypt information file
    public byte[] getencryptData(byte[] bytes) throws Exception
    {
        SecureRandom sr = new SecureRandom();

        
        byte[] rawKeyData = readKeyFile(keyfilename);
        
        Class classkeyspec=Class.forName( "javax.crypto.spec.DESKeySpec" );
        Constructor constructor = classkeyspec.getConstructor(new Class[]{byte[].class});
        KeySpec dks = (KeySpec) constructor.newInstance(new Object[]{rawKeyData});
       
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( algorithm );
        SecretKey key = keyFactory.generateSecret(dks);
       
        Cipher cipher = Cipher.getInstance( algorithm );
        
        cipher.init(Cipher.ENCRYPT_MODE, key, sr);
       
        bytes = cipher.doFinal(bytes);
        
        return bytes;
    }
	
	// to create a key
    public void createKey() throws Exception
    {
		SecureRandom secureRandom = new SecureRandom();

        // to create a object of the KeyGenerator for DES algorithm
        KeyGenerator kg = KeyGenerator.getInstance( algorithm );
        kg.init(secureRandom);

        // to generate key
        SecretKey key = kg.generateKey();
        // to save key into key file
        writeKeyFile(key.getEncoded(),keyfilename);
    }

	public static void main(String[] args) 
	{
        Face f = new Face();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -