Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.03 KB | None | 0 0
  1. public class username extends javax.swing.JPanel {
  2.  
  3. /**
  4. * Creates new form username
  5. */
  6. public username() {
  7. initComponents();
  8. }
  9.  
  10. /**
  11. * This method is called from within the constructor to initialize the form.
  12. * WARNING: Do NOT modify this code. The content of this method is always
  13. * regenerated by the Form Editor.
  14. */
  15. @SuppressWarnings("unchecked")
  16. // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  17. private void initComponents() {
  18.  
  19. jLabel1 = new javax.swing.JLabel();
  20. jLabel2 = new javax.swing.JLabel();
  21. jTextField1 = new javax.swing.JTextField();
  22. jTextField2 = new javax.swing.JTextField();
  23. OK = new java.awt.Button();
  24.  
  25. jLabel1.setText("Username");
  26.  
  27. jLabel2.setText("PIN");
  28.  
  29. OK.setActionCommand("OK");
  30. OK.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
  31. OK.setLabel("OK");
  32. ClickListener cl = new ClickListener();
  33. OK.addActionListener(cl);
  34.  
  35. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  36. this.setLayout(layout);
  37. layout.setHorizontalGroup(
  38. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  39. .addGroup(layout.createSequentialGroup()
  40. .addGap(107, 107, 107)
  41. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  42. .addGroup(layout.createSequentialGroup()
  43. .addComponent(jLabel1)
  44. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  45. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
  46. .addGroup(layout.createSequentialGroup()
  47. .addComponent(jLabel2)
  48. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  49. .addGroup(layout.createSequentialGroup()
  50. .addGap(72, 72, 72)
  51. .addComponent(OK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  52. .addGroup(layout.createSequentialGroup()
  53. .addGap(41, 41, 41)
  54. .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))))
  55. .addContainerGap(74, Short.MAX_VALUE))
  56. );
  57. layout.setVerticalGroup(
  58. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  59. .addGroup(layout.createSequentialGroup()
  60. .addGap(55, 55, 55)
  61. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  62. .addComponent(jLabel1)
  63. .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  64. .addGap(18, 18, 18)
  65. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  66. .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  67. .addComponent(jLabel2))
  68. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
  69. .addComponent(OK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  70. .addGap(72, 72, 72))
  71. );
  72. this.setSize(400,250);
  73. }// </editor-fold>//GEN-END:initComponents
  74.  
  75. class ClickListener implements ActionListener
  76. {
  77. @Override
  78. public void actionPerformed(ActionEvent e)
  79. {
  80. if("OK".equals(e.getActionCommand()))
  81. {
  82. try
  83. {
  84. String username = jTextField1.getText();
  85. System.out.println(username);
  86. if (username.length() == 0)
  87. {
  88. JOptionPane.showMessageDialog(username.this, "You did not enter a username!", "Username", JOptionPane.INFORMATION_MESSAGE);
  89. }
  90. String pintest = jTextField2.getText();
  91. if (pintest.length() != 4)
  92. {
  93. JOptionPane.showMessageDialog(username.this, "Your PIN consists of 4 digits", "PIN", JOptionPane.INFORMATION_MESSAGE);
  94. }
  95. LocalTime now = LocalTime.now();
  96. int minutes = now.getMinute();
  97. System.out.println(minutes);
  98. String minstring = Integer.toString(minutes);
  99. System.out.println(minstring);
  100. pintest = pintest.concat(minstring);
  101. System.out.println(pintest);
  102. MessageDigest messageDigest=null;
  103. try
  104. {
  105. messageDigest = MessageDigest.getInstance("SHA-256");
  106. messageDigest.update((pintest).getBytes());
  107. }
  108. catch (NoSuchAlgorithmException f)
  109. {
  110. }
  111. @SuppressWarnings("null")
  112. String encryptedPassword = (new BigInteger(messageDigest.digest())).toString(16);
  113. System.out.println("Encrypted Password: " + encryptedPassword);
  114. String hashed = encryptedPassword.substring(0, 8);
  115. System.out.println(hashed);
  116. PublicKey publicKey = readPublicKey("public.der");
  117. PrivateKey privateKey = readPrivateKey("private.der");
  118. byte[] message = hashed.getBytes("UTF8");
  119. byte[] secret = encrypt(publicKey, message);
  120. byte[] recovered_message = decrypt(privateKey, secret);
  121. System.out.println(new String(message, "UTF8"));
  122. System.out.println(new String(recovered_message, "UTF8"));
  123.  
  124. }
  125. catch (HeadlessException | IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException g)
  126. {
  127. } catch (InvalidKeySpecException ex) {
  128. Logger.getLogger(username.class.getName()).log(Level.SEVERE, null, ex);
  129. }
  130. }
  131. }
  132.  
  133. public PublicKey readPublicKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException
  134. {
  135. byte[] keyBytes = Files.readAllBytes(new File(filename).toPath());
  136. X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(keyBytes);
  137. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  138. return keyFactory.generatePublic(publicSpec);
  139. }
  140. public PrivateKey readPrivateKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException
  141. {
  142. byte[] keyBytes1 = Files.readAllBytes(new File(filename).toPath());
  143. PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(keyBytes1);
  144. KeyFactory keyFactory = KeyFactory.getInstance("RSA");
  145. return keyFactory.generatePrivate(privateSpec);
  146. }
  147. public byte[] encrypt(PublicKey key, byte[] plaintext) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
  148. {
  149. Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
  150. cipher.init(Cipher.ENCRYPT_MODE, key);
  151. return cipher.doFinal(plaintext);
  152. }
  153. public byte[] decrypt(PrivateKey key, byte[] ciphertext) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
  154. {
  155. Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
  156. cipher.init(Cipher.DECRYPT_MODE, key);
  157. return cipher.doFinal(ciphertext);
  158. }
  159. }
  160.  
  161. // Variables declaration - do not modify//GEN-BEGIN:variables
  162. private int minutes;
  163. private String minstring, pintest, hashed;
  164. private java.awt.Button OK;
  165. private javax.swing.JLabel jLabel1;
  166. private javax.swing.JLabel jLabel2;
  167. private javax.swing.JTextField jTextField1;
  168. private javax.swing.JTextField jTextField2;
  169. // End of variables declaration//GEN-END:variables
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement