Guest User

createUserPass

a guest
Oct 21st, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.04 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package ScheduleManager.pkg;
  7.  
  8. import java.io.BufferedWriter;
  9. import java.io.File;
  10. import java.io.FileWriter;
  11. import java.io.IOException;
  12. import java.util.logging.Level;
  13. import java.util.logging.Logger;
  14. import javax.swing.JOptionPane;
  15.  
  16. public class createUserPass extends javax.swing.JFrame {
  17.  
  18.     File file;
  19.  
  20.     /**
  21.      * Creates new form createUserPass
  22.      */
  23.     public createUserPass()  {
  24.         String path = "\\src\\Schedulemanager\\pkg\\info.txt";
  25.         file = new File(path); //creates new File
  26.         /* if (file.exists()) { //make a new file if it's not already existent
  27.                     logIn loginPage = new logIn();
  28.                     loginPage.setVisible(true);
  29.                     loginPage.setAlwaysOnTop(true);
  30.                     this.dispose();
  31.                     this.setVisible(false);
  32.                 } else {
  33.                     file.createNewFile(); //creates the new file
  34.                    
  35.                 } */
  36.         initComponents();
  37.     }
  38.  
  39.     /**
  40.      * 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.
  41.      */
  42.     @SuppressWarnings("unchecked")
  43.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  44.     private void initComponents() {
  45.  
  46.         jLabel1 = new javax.swing.JLabel();
  47.         jLabel2 = new javax.swing.JLabel();
  48.         jLabel3 = new javax.swing.JLabel();
  49.         userNameField = new javax.swing.JTextField();
  50.         passwordField = new javax.swing.JTextField();
  51.         jLabel4 = new javax.swing.JLabel();
  52.         securityAnswer = new javax.swing.JTextField();
  53.         submitButton = new javax.swing.JButton();
  54.  
  55.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  56.  
  57.         jLabel1.setText("Please set your username, password and security question.");
  58.  
  59.         jLabel2.setText("username:");
  60.  
  61.         jLabel3.setText("password:");
  62.  
  63.         jLabel4.setText("security question: what was your favourite class in college?");
  64.  
  65.         submitButton.setText("submit");
  66.         submitButton.addActionListener(new java.awt.event.ActionListener() {
  67.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  68.                 submitButtonActionPerformed(evt);
  69.             }
  70.         });
  71.  
  72.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  73.         getContentPane().setLayout(layout);
  74.         layout.setHorizontalGroup(
  75.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  76.             .addGroup(layout.createSequentialGroup()
  77.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  78.                     .addGroup(layout.createSequentialGroup()
  79.                         .addGap(30, 30, 30)
  80.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  81.                             .addGroup(layout.createSequentialGroup()
  82.                                 .addComponent(jLabel3)
  83.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  84.                                 .addComponent(passwordField))
  85.                             .addGroup(layout.createSequentialGroup()
  86.                                 .addComponent(jLabel2)
  87.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  88.                                 .addComponent(userNameField))))
  89.                     .addGroup(layout.createSequentialGroup()
  90.                         .addContainerGap()
  91.                         .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  92.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  93.                         .addGap(0, 27, Short.MAX_VALUE)
  94.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  95.                             .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 391, javax.swing.GroupLayout.PREFERRED_SIZE)
  96.                             .addComponent(securityAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, 378, javax.swing.GroupLayout.PREFERRED_SIZE))))
  97.                 .addContainerGap())
  98.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  99.                 .addGap(0, 0, Short.MAX_VALUE)
  100.                 .addComponent(submitButton)
  101.                 .addGap(159, 159, 159))
  102.         );
  103.         layout.setVerticalGroup(
  104.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  105.             .addGroup(layout.createSequentialGroup()
  106.                 .addContainerGap()
  107.                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
  108.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  109.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  110.                     .addComponent(jLabel2)
  111.                     .addComponent(userNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  112.                 .addGap(24, 24, 24)
  113.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  114.                     .addComponent(jLabel3)
  115.                     .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  116.                 .addGap(18, 18, 18)
  117.                 .addComponent(jLabel4)
  118.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  119.                 .addComponent(securityAnswer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  120.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  121.                 .addComponent(submitButton)
  122.                 .addContainerGap())
  123.         );
  124.  
  125.         pack();
  126.     }// </editor-fold>                        
  127.  
  128.     private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
  129.         // TODO add your handling code here:
  130.         String username = userNameField.getText();
  131.         String password = passwordField.getText();
  132.         String answer = securityAnswer.getText();
  133.         if (!userNameField.getText().equals("") && !passwordField.getText().equals("")
  134.                 && !securityAnswer.getText().equals("")) {
  135.             try {
  136.                 String encryptedUsername = encrypt(username);
  137.                 String encryptedPassword = encrypt(password);
  138.                 String encryptedAnswer = encrypt(answer);
  139.                 String content = encryptedUsername + "\n" + encryptedPassword + "\n" + encryptedAnswer;
  140.  
  141.                 file.createNewFile(); //creates the new file
  142.  
  143.                 FileWriter fw = new FileWriter(file.getAbsoluteFile());
  144.                 BufferedWriter bw = new BufferedWriter(fw);
  145.                 bw.write(content);
  146.                 bw.close();
  147.  
  148.                 logIn loginPage = new logIn();
  149.                 loginPage.setVisible(true);
  150.                 loginPage.setAlwaysOnTop(true);
  151.                 this.dispose();
  152.             } catch (Exception e) {
  153.                 System.out.println(e);
  154.             }
  155.         } else {
  156.             JOptionPane.showMessageDialog(null,
  157.                     "All fields must be filled.",
  158.                     "ERROR",
  159.                     JOptionPane.WARNING_MESSAGE);
  160.         }
  161.     }                                            
  162.  
  163.     /**
  164.      * @param args the command line arguments
  165.      */
  166.     public static void main(String args[]) {
  167.         /* Set the Nimbus look and feel */
  168.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  169.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  170.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  171.          */
  172.         try {
  173.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  174.                 if ("Nimbus".equals(info.getName())) {
  175.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  176.                     break;
  177.                 }
  178.             }
  179.         } catch (ClassNotFoundException ex) {
  180.             java.util.logging.Logger.getLogger(createUserPass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  181.         } catch (InstantiationException ex) {
  182.             java.util.logging.Logger.getLogger(createUserPass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  183.         } catch (IllegalAccessException ex) {
  184.             java.util.logging.Logger.getLogger(createUserPass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  185.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  186.             java.util.logging.Logger.getLogger(createUserPass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  187.         }
  188.         //</editor-fold>
  189.  
  190.         /* Create and display the form */
  191.         java.awt.EventQueue.invokeLater(new Runnable() {
  192.             public void run() {
  193.                 //try {
  194.                     new createUserPass().setVisible(true);
  195.                 /*}  catch (IOException ex) {
  196.                     Logger.getLogger(createUserPass.class.getName()).log(Level.SEVERE, null, ex);
  197.                 } */
  198.             }
  199.         });
  200.  
  201.     }
  202.  
  203.     public static String encrypt(String x) { //caesar cipher shifts letters by 3 places
  204.         char[] message = x.toCharArray();
  205.         for (int i = 0; i < message.length; i++) {
  206.             char character = message[i];
  207.             character = (char) (character + 3);
  208.             if (character > 'z') {
  209.                 character = (char) (character - 26);
  210.             } else if (character < 'a') {
  211.                 character = (char) (character + 26);
  212.             }
  213.             message[i] = character;
  214.         }
  215.         String output = new String(message);
  216.         System.out.println("from " + x + " to " + output);
  217.         return output;
  218.     }
  219.  
  220.     // Variables declaration - do not modify                    
  221.     private javax.swing.JLabel jLabel1;
  222.     private javax.swing.JLabel jLabel2;
  223.     private javax.swing.JLabel jLabel3;
  224.     private javax.swing.JLabel jLabel4;
  225.     private javax.swing.JTextField passwordField;
  226.     private javax.swing.JTextField securityAnswer;
  227.     private javax.swing.JButton submitButton;
  228.     private javax.swing.JTextField userNameField;
  229.     // End of variables declaration                  
  230. }
Add Comment
Please, Sign In to add comment