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

📄 cppusbconnector.java

📁 打印管理程序,测试完全通过.windows开发环境.
💻 JAVA
字号:
package jp.co.ntl.swing.ext;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Vector;

import jp.co.ntl.Util;
import jp.co.ntl.NoUSBException;
///import jp.co.ntl.codec.MsgDigest;
import jp.co.ntl.cppuser.CPPUserInfo;

public class CPPUSBConnector {
	private final static String	usbUtilCommand	= "./USBUtil";
	private final static String	readOption		= "-rir843fjdc";
	private final static String	writeOption		= "-ry47jcklos";
	
	public String checkUnRegisteredUSB(Vector vcUserInfo) throws IOException, NoUSBException {
		String			usbSerial = "";
		CPPUserInfo		userInfo = new CPPUserInfo();

		userInfo = getUSBData(0);

		for (int i = 0; i < vcUserInfo.size(); i++) {
			CPPUserInfo	srvUserInfo = (CPPUserInfo)vcUserInfo.elementAt(i);
			if (!userInfo.getSPSerial().equals(srvUserInfo.getSPSerial())) {
				usbSerial = userInfo.getSPSerial();
				break;
			}
		}
		
		return usbSerial;
	}
	
	public boolean checkRegisteredUSB(CPPUserInfo userInfo) throws IOException, NoUSBException {
		boolean		ret = false;
		CPPUserInfo		usbUserInfo = new CPPUserInfo();

		usbUserInfo = getUSBData(Long.parseLong(userInfo.getSPSerial()));
		
		// USB Key撪偺儐乕僓柤丄僷僗儚乕僪偑堦抳偟偨偲偒
		Util.debugPrint("user name = " + userInfo.getUsername());
		Util.debugPrint("usb user name = " + usbUserInfo.getUsername());
		Util.debugPrint("password = " + userInfo.getPassword());
		Util.debugPrint("usb password = " + usbUserInfo.getPassword());
		if (userInfo.getUsername().equals(usbUserInfo.getUsername())) {
			ret = isMatchedPassword(userInfo.getPassword(), usbUserInfo.getPassword());
		}
		
		return ret;
	}
	
///	protected native CPPUserInfo getUSBData(long usbSerial) throws NoUSBException;
///	protected native boolean setUSBData(long usbSerial, String userName, String password) throws NoUSBException;

	protected CPPUserInfo getUSBData(long usbSerial) throws IOException, NoUSBException {
		BufferedReader	br = null;
		Runtime			runtime = Runtime.getRuntime();
		String[]		cmd = null;
		Process			proc;
		InputStream		in;
		CPPUserInfo		userInfo = new CPPUserInfo();
		
		if (usbSerial == 0) {
			cmd = new String[] {usbUtilCommand, readOption};
		} else {
			cmd = new String[] {usbUtilCommand, readOption, Long.toString(usbSerial)};
		}
		
		try {
			proc = runtime.exec(cmd);
			if (proc != null) {
				in = proc.getInputStream();
				if (in != null) {
					br = new BufferedReader(new InputStreamReader(in));
				} else {
					throw new NoUSBException();
				}
			} else {
				throw new NoUSBException();
			}
		} catch (IOException e) {
			throw new NoUSBException(e.getMessage());
		}
		
		try {
			String		s, key, value;
			int			index;
			while ((s = br.readLine()) != null) {
				if ((index = s.indexOf(' ')) == -1) {
					break;
				}
				key = s.substring(0, index);
				value = s.substring(index + 1);
				put(key, value, userInfo);
				if (key.equals("password")) {
					break;
				}
			}
		} finally {
			br.close();
		}
		
		return userInfo;
	}
	
	protected boolean setUSBData(CPPUserInfo userInfo) throws NoUSBException {
		Runtime 		runtime = Runtime.getRuntime();
		String[] 		cmd = null;
		Process	 		proc;
		boolean		ret = false;
		int				exitValue;
		
		cmd = new String[] {
				usbUtilCommand,
				writeOption,
				userInfo.getSPSerial(),
				userInfo.getUsername(),
				userInfo.getPassword()
		};
		
		try {
			proc = runtime.exec(cmd);
			if (proc != null) {
				try {
					proc.waitFor();
				} catch (InterruptedException e) {
					throw new NoUSBException(e.getMessage());
				}
				exitValue = proc.exitValue();
				if (exitValue == 0) {
					ret = true;
				}
			}
		} catch (IOException e) {
			throw new NoUSBException(e.getMessage());
		}
	   
		return ret;
	}
	
    protected void put(String key, String value, CPPUserInfo userInfo) {
        if (key.equals("serial")) {
        	userInfo.setSPSerial(value);
        } else if (key.equals("username")) {
        	userInfo.setUsername(value);
        } else if (key.equals("password")) {
       		userInfo.setPassword(value);
        }
    }
			
    protected boolean isMatchedPassword(String password, String usbPassword) {
		boolean	ret = false;

		if (password == null && usbPassword.equals("")) {
			ret = true;
		} else {
			if (password != null) {
				if (password.equals("") && usbPassword.equals("")) {
					ret = true;
				} else {
///					byte[]	byte_pass = MsgDigest.getDigest(password);
///					byte[]	byte_usbPass = MsgDigest.getDigest(usbPassword);
///					if (MsgDigest.compareDigest(byte_pass, byte_usbPass)) {
					Util.debugPrint("password is matched");
					if (password.equals(usbPassword)) {
						ret = true;
					}
				}
			}
		}
		
		return ret;
	}
}

⌨️ 快捷键说明

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