Advertisement
Guest User

lab1

a guest
Mar 30th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.05 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.math.BigInteger;
  7. import java.nio.charset.Charset;
  8. import java.nio.charset.StandardCharsets;
  9. import java.nio.file.Files;
  10. import java.nio.file.Path;
  11. import java.nio.file.Paths;
  12. import java.security.MessageDigest;
  13. import java.security.NoSuchAlgorithmException;
  14. import java.util.Scanner;
  15.  
  16. public class MainClass {
  17.    
  18.     public static void main(String[] args) throws IOException{
  19.         System.out.println("Type 1 for registration or 2 for entering the system");
  20.         Scanner in = new Scanner(System.in);
  21.         int a = in.nextInt();
  22.         //in.close();
  23.         if (a == 1) {
  24.             reg();
  25.         }
  26.         else if (a == 2){
  27.             ent();
  28.         }
  29.         else{
  30.             System.out.println("Error\n Type 1 for registration or 2 for entering the system");
  31.         }
  32.     }
  33.    
  34.     public static void reg(){
  35.          String filename = "C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\logins.txt";
  36.          String filename2 = "C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\passwords.txt";
  37.          
  38.          System.out.println("Create username");
  39.          Scanner in = new Scanner(System.in);
  40.          String userName = in.nextLine();
  41.          //in.close();
  42.          
  43.          String userdata = "C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\" + userName + ".txt";
  44.          
  45.          System.out.println("Введите любую строку");
  46.          //Scanner in = new Scanner(System.in);
  47.          String line = in.nextLine();
  48.          System.out.println(line);
  49.          //in.close();
  50.          
  51.          //добавляем логин в файл логинов
  52.          try{
  53.              FileWriter sw = new FileWriter(filename,true);
  54.              sw.write(userName);
  55.              sw.append('\n');
  56.              sw.flush();
  57.              sw.close();
  58.          }catch(Exception e){
  59.              System.out.print(e.getMessage());
  60.          }
  61.          
  62.          try{
  63.              FileWriter fw = new FileWriter(userdata, true);
  64.              for (int i = 0; i < 10; i++){
  65.                  String hashLine = md5Custom(line);
  66.                  fw.write(hashLine + "\n");
  67.                  line = hashLine;
  68.              }
  69.              fw.close();
  70.          }catch(Exception e){
  71.              System.out.print(e.getMessage());
  72.          }
  73.          
  74.          //добавляем хэш в файл хэшей
  75.          try{
  76.              FileWriter sw = new FileWriter(filename2,true);
  77.              sw.write(md5Custom(line) + "\n");
  78.              sw.close();
  79.          }catch(Exception e){
  80.              System.out.print(e.getMessage());
  81.          }    
  82.     }
  83.    
  84.     public static void ent() throws IOException{
  85.         System.out.println("Type your username");
  86.         Scanner in = new Scanner(System.in);
  87.         String userName = in.nextLine();
  88.         //in.close();
  89.        
  90.         System.out.println("Type your last password");
  91.         Scanner in2 = new Scanner(System.in);
  92.         String password = in2.nextLine();
  93.         in2.close();
  94.        
  95.         String hashLine = md5Custom(password);
  96.        
  97.         int loginNumber = -1;
  98.        
  99.         FileInputStream fis = new FileInputStream(new File("C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\logins.txt"));
  100.         byte[] content = new byte[fis.available()];
  101.         fis.read(content);
  102.         fis.close();
  103.         String[] lines = new String(content, "Cp1251").split("\n");
  104.         int i = 1;
  105.         for (String line : lines) {
  106.             String[] words = line.split(" ");
  107.             int j = 1;
  108.             for (String word : words) {
  109.                 if (word.equalsIgnoreCase(userName)) {
  110.                     loginNumber = i;
  111.                 }
  112.                 j++;
  113.             }
  114.             i++;
  115.         }
  116.        
  117.         if (loginNumber != -1){
  118.             int stringNumber = 1;
  119.             Scanner scanner = new Scanner (new File ("C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\passwords.txt"));
  120.             while (scanner.hasNextLine()) {
  121.                 String numline = scanner.nextLine();
  122.                 if(stringNumber == loginNumber){
  123.                     if(hashLine.equals(numline)){
  124.                         System.out.println("OK");
  125.                         Charset charset = StandardCharsets.UTF_8;
  126.                         Path path = Paths.get("C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\passwords.txt");
  127.                         Files.write(path,
  128.                             new String(Files.readAllBytes(path), charset).replace(hashLine, password)
  129.                                 .getBytes(charset));
  130.                     }
  131.                     else{
  132.                         System.out.println("NOT OK");
  133.                     }
  134.                     break;
  135.                 }
  136.                 stringNumber++;
  137.             }
  138.             scanner.close();
  139.         }
  140.     }
  141.    
  142.     public static String md5Custom(String st) {
  143.         MessageDigest messageDigest = null;
  144.         byte[] digest = new byte[0];
  145.      
  146.         try {
  147.             messageDigest = MessageDigest.getInstance("MD5");
  148.             messageDigest.reset();
  149.             messageDigest.update(st.getBytes());
  150.             digest = messageDigest.digest();
  151.         } catch (NoSuchAlgorithmException e) {
  152.             // тут можно обработать ошибку
  153.             // возникает она если в передаваемый алгоритм в getInstance(,,,) не существует
  154.             e.printStackTrace();
  155.         }
  156.      
  157.         BigInteger bigInt = new BigInteger(1, digest);
  158.         String md5Hex = bigInt.toString(16);
  159.      
  160.         while( md5Hex.length() < 32 ){
  161.             md5Hex = "0" + md5Hex;
  162.         }
  163.      
  164.         return md5Hex;
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement