public GamePanel() { setBackground(new Color(0, 0, 0)); setFocusable(true); // setIgnoreRepaint(true); addKeyListener(KeyManager.getDefault()); // 因为游戏图画根据窗体大小变化 // 当PANEL大小变化时需要重画.否则由小变大不会调用paint addComponentListener( new ComponentAdapter() { public void componentResized(ComponentEvent e) { if (m_game != null) m_game.refreshScreen(); } }); // 当失去焦点,要清空键盘,否则可能导致只有按键无抬键 addFocusListener( new FocusAdapter() { public void focusLost(FocusEvent e) { // System.out.println("lost Focus"); KeyManager.clearKeyState(); m_prompt = "PAUSED"; repaint(); if (m_game != null) m_game.pause(); } public void focusGained(FocusEvent e) { // System.out.println("lost Focus"); if (m_game != null) m_game.resume(); m_prompt = "LOADING..."; } }); }
public static SmsMessage createSmsMessage(Contact contact, String content) throws FailedToCreateSmsMessageException { // TODO: Set message direction Calendar calendar; long dateNumber; DataManager dm; String messageId; String contactId; int messageCount; SmsMessage smsMessage; try { // Get sms message information from calendar and manager calendar = new GregorianCalendar(TimeZone.getDefault()); // dateNumber = calendar.getTime().getTime(); dateNumber = calendar.getTimeInMillis(); // Create sms message into storage dm = DataManager.getInstance(); // get key to encrypt messages byte[] salt = Cryptography.decodeFromStorage(dm.getAttributeString(dm.USER, dm.SALT)); byte[] iv = Cryptography.decodeFromStorage(dm.getAttributeString(dm.USER, dm.IV)); SecretKey key = KeyManager.getInstance().generateStorageKey(salt); // setup message id contactId = contact.getId(); messageCount = dm.getAttributeInt(contactId, dm.MESSAGE_COUNT); messageId = contactId + dm.MESSAGE_CLASS + String.format("%04d", messageCount); messageCount++; smsMessage = new SmsMessage(messageId, contact, dateNumber, content); String encryptedContent = smsMessage.encryptToStore(key, iv); // set attributes dm.setAttribute(contactId, dm.MESSAGE_COUNT, messageCount); dm.addAttribute(contactId, dm.MESSAGE_TABLE, messageId); dm.setAttribute(messageId, dm.MESSAGE_DATE_NUMBER, dateNumber); dm.setAttribute(messageId, dm.ENCRYPTED_CONTENT, encryptedContent); return smsMessage; } catch (FailedToLoadDataBaseException | KeyStoreIsLockedException | FailedToGenerateKeyException | FailedToStoreException | FailedToEncryptSmsMessageException | FailedToGetAttributeException | FailedToAddAttributeException exception) { throw new FailedToCreateSmsMessageException(exception); } }
public static ArrayList<SmsMessage> retrieveAllSmsMessages(Contact contact) throws FailedToRetrieveAllSmsMessagesException { ArrayList<SmsMessage> messages; Set<String> messageIds; DataManager dm; long dateNumber; String contactId; String encryptedContent, decryptedContent; SmsMessage sms; try { messages = new ArrayList<>(); dm = DataManager.getInstance(); // get key to decrypt messages byte[] salt = Cryptography.decodeFromStorage(dm.getAttributeString(dm.USER, dm.SALT)); byte[] iv = Cryptography.decodeFromStorage(dm.getAttributeString(dm.USER, dm.IV)); SecretKey key = KeyManager.getInstance().generateStorageKey(salt); contactId = contact.getId(); messageIds = dm.getAttributeSet(contactId, dm.MESSAGE_TABLE); // For each sms message id for (String messageId : messageIds) { // Get sms message information from storage dateNumber = dm.getAttributeLong(messageId, dm.MESSAGE_DATE_NUMBER); encryptedContent = dm.getAttributeString(messageId, dm.ENCRYPTED_CONTENT); // decrypt sms = new SmsMessage(contact, encryptedContent); decryptedContent = sms.decryptFromStorage(key, iv); sms = new SmsMessage(messageId, contact, dateNumber, decryptedContent); messages.add(sms); } // Return array list of contacts return messages; } catch (FailedToDecryptSmsMessageException | FailedToGenerateKeyException | FailedToStoreException | KeyStoreIsLockedException | FailedToLoadDataBaseException | FailedToGetAttributeException exception) { throw new FailedToRetrieveAllSmsMessagesException(exception); } }
public void scepCLI() throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyManager km = new KeyManager(); CertUtil certutil = new CertUtil(); KeyPair kp = km.createRSA(params.getKeySize()); X509Certificate cert = certutil.createSelfSignedCertificate(kp, params.getDn()); CertificationRequest request = certutil.createCertificationRequest(kp, params.getDn(), params.getChallenge()); CallbackHandler handler = new ConsoleCallbackHandler(); URL serverURL = new URL(params.getUrl()); try { if (params.getCsrFile() != null) { saveToPEM(params.getCsrFile(), (PKCS10CertificationRequest) request); } Client client = new Client(serverURL, cert, kp.getPrivate(), handler, params.getCaIdentifier()); client.getCaCertificate(); EnrolmentTransaction tx = client.enrol(request); Transaction.State response = tx.send(); /* * handle asynchronous response */ while (response == Transaction.State.CERT_REQ_PENDING) { Thread.currentThread().sleep(1000); System.out.println("CERT_REQ_PENDING, wait 1 second"); response = tx.poll(); } if (response == Transaction.State.CERT_ISSUED) { try { saveToPEM(params.getCrlFile(), (X509CRL) client.getRevocationList()); } catch (Exception e) { System.err.println("Exception while saving CRL"); } try { saveToPEM(params.getKeyFile(), (RSAPrivateCrtKey) kp.getPrivate()); CertStore store = tx.getCertStore(); Collection<? extends Certificate> certs = store.getCertificates(null); Iterator it = certs.iterator(); while (it.hasNext()) { X509Certificate certificate = (X509Certificate) it.next(); if (certificate.getBasicConstraints() != -1) { saveToPEM(params.getCaCertificateFile(), (X509Certificate) certificate); } else { saveToPEM(params.getCertificateFile(), (X509Certificate) certificate); } } System.out.println("Certificate issued"); } catch (Exception e) { System.err.println("Exception while saving files: " + e); } } else { System.err.println("Unknown error" + response); } } catch (IOException e) { if (params.getVerbose()) { e.printStackTrace(); } System.err.println(e.getMessage()); if (e.getMessage().contains("400")) { System.err.println(". Probably a template issue, look at PKI log"); } else if (e.getMessage().contains("404")) { System.err.println(". Invalid URL or CA identifier"); } else if (e.getMessage().contains("401")) { System.err.println(". Probably EJBCA invalid entity status"); } } catch (Exception e) { System.out.println(e); } }