Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class username extends javax.swing.JPanel {
- /**
- * Creates new form username
- */
- public username() {
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
- private void initComponents() {
- jLabel1 = new javax.swing.JLabel();
- jLabel2 = new javax.swing.JLabel();
- jTextField1 = new javax.swing.JTextField();
- jTextField2 = new javax.swing.JTextField();
- OK = new java.awt.Button();
- jLabel1.setText("Username");
- jLabel2.setText("PIN");
- OK.setActionCommand("OK");
- OK.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
- OK.setLabel("OK");
- ClickListener cl = new ClickListener();
- OK.addActionListener(cl);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
- this.setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(107, 107, 107)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel2)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(72, 72, 72)
- .addComponent(OK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGroup(layout.createSequentialGroup()
- .addGap(41, 41, 41)
- .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)))))
- .addContainerGap(74, Short.MAX_VALUE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addGap(55, 55, 55)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jLabel1)
- .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
- .addGap(18, 18, 18)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel2))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 90, Short.MAX_VALUE)
- .addComponent(OK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(72, 72, 72))
- );
- this.setSize(400,250);
- }// </editor-fold>//GEN-END:initComponents
- class ClickListener implements ActionListener
- {
- @Override
- public void actionPerformed(ActionEvent e)
- {
- if("OK".equals(e.getActionCommand()))
- {
- try
- {
- String username = jTextField1.getText();
- System.out.println(username);
- if (username.length() == 0)
- {
- JOptionPane.showMessageDialog(username.this, "You did not enter a username!", "Username", JOptionPane.INFORMATION_MESSAGE);
- }
- String pintest = jTextField2.getText();
- if (pintest.length() != 4)
- {
- JOptionPane.showMessageDialog(username.this, "Your PIN consists of 4 digits", "PIN", JOptionPane.INFORMATION_MESSAGE);
- }
- LocalTime now = LocalTime.now();
- int minutes = now.getMinute();
- System.out.println(minutes);
- String minstring = Integer.toString(minutes);
- System.out.println(minstring);
- pintest = pintest.concat(minstring);
- System.out.println(pintest);
- MessageDigest messageDigest=null;
- try
- {
- messageDigest = MessageDigest.getInstance("SHA-256");
- messageDigest.update((pintest).getBytes());
- }
- catch (NoSuchAlgorithmException f)
- {
- }
- @SuppressWarnings("null")
- String encryptedPassword = (new BigInteger(messageDigest.digest())).toString(16);
- System.out.println("Encrypted Password: " + encryptedPassword);
- String hashed = encryptedPassword.substring(0, 8);
- System.out.println(hashed);
- PublicKey publicKey = readPublicKey("public.der");
- PrivateKey privateKey = readPrivateKey("private.der");
- byte[] message = hashed.getBytes("UTF8");
- byte[] secret = encrypt(publicKey, message);
- byte[] recovered_message = decrypt(privateKey, secret);
- System.out.println(new String(message, "UTF8"));
- System.out.println(new String(recovered_message, "UTF8"));
- }
- catch (HeadlessException | IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException g)
- {
- } catch (InvalidKeySpecException ex) {
- Logger.getLogger(username.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- public PublicKey readPublicKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException
- {
- byte[] keyBytes = Files.readAllBytes(new File(filename).toPath());
- X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- return keyFactory.generatePublic(publicSpec);
- }
- public PrivateKey readPrivateKey(String filename) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException
- {
- byte[] keyBytes1 = Files.readAllBytes(new File(filename).toPath());
- PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(keyBytes1);
- KeyFactory keyFactory = KeyFactory.getInstance("RSA");
- return keyFactory.generatePrivate(privateSpec);
- }
- public byte[] encrypt(PublicKey key, byte[] plaintext) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
- {
- Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
- cipher.init(Cipher.ENCRYPT_MODE, key);
- return cipher.doFinal(plaintext);
- }
- public byte[] decrypt(PrivateKey key, byte[] ciphertext) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException
- {
- Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
- cipher.init(Cipher.DECRYPT_MODE, key);
- return cipher.doFinal(ciphertext);
- }
- }
- // Variables declaration - do not modify//GEN-BEGIN:variables
- private int minutes;
- private String minstring, pintest, hashed;
- private java.awt.Button OK;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JLabel jLabel2;
- private javax.swing.JTextField jTextField1;
- private javax.swing.JTextField jTextField2;
- // End of variables declaration//GEN-END:variables
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement