Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.math.BigInteger;
- import java.nio.charset.Charset;
- import java.nio.charset.StandardCharsets;
- import java.nio.file.Files;
- import java.nio.file.Path;
- import java.nio.file.Paths;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.util.Scanner;
- public class MainClass {
- public static void main(String[] args) throws IOException{
- System.out.println("Type 1 for registration or 2 for entering the system");
- Scanner in = new Scanner(System.in);
- int a = in.nextInt();
- //in.close();
- if (a == 1) {
- reg();
- }
- else if (a == 2){
- ent();
- }
- else{
- System.out.println("Error\n Type 1 for registration or 2 for entering the system");
- }
- }
- public static void reg(){
- String filename = "C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\logins.txt";
- String filename2 = "C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\passwords.txt";
- System.out.println("Create username");
- Scanner in = new Scanner(System.in);
- String userName = in.nextLine();
- //in.close();
- String userdata = "C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\" + userName + ".txt";
- System.out.println("Введите любую строку");
- //Scanner in = new Scanner(System.in);
- String line = in.nextLine();
- System.out.println(line);
- //in.close();
- //добавляем логин в файл логинов
- try{
- FileWriter sw = new FileWriter(filename,true);
- sw.write(userName);
- sw.append('\n');
- sw.flush();
- sw.close();
- }catch(Exception e){
- System.out.print(e.getMessage());
- }
- try{
- FileWriter fw = new FileWriter(userdata, true);
- for (int i = 0; i < 10; i++){
- String hashLine = md5Custom(line);
- fw.write(hashLine + "\n");
- line = hashLine;
- }
- fw.close();
- }catch(Exception e){
- System.out.print(e.getMessage());
- }
- //добавляем хэш в файл хэшей
- try{
- FileWriter sw = new FileWriter(filename2,true);
- sw.write(md5Custom(line) + "\n");
- sw.close();
- }catch(Exception e){
- System.out.print(e.getMessage());
- }
- }
- public static void ent() throws IOException{
- System.out.println("Type your username");
- Scanner in = new Scanner(System.in);
- String userName = in.nextLine();
- //in.close();
- System.out.println("Type your last password");
- Scanner in2 = new Scanner(System.in);
- String password = in2.nextLine();
- in2.close();
- String hashLine = md5Custom(password);
- int loginNumber = -1;
- FileInputStream fis = new FileInputStream(new File("C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\logins.txt"));
- byte[] content = new byte[fis.available()];
- fis.read(content);
- fis.close();
- String[] lines = new String(content, "Cp1251").split("\n");
- int i = 1;
- for (String line : lines) {
- String[] words = line.split(" ");
- int j = 1;
- for (String word : words) {
- if (word.equalsIgnoreCase(userName)) {
- loginNumber = i;
- }
- j++;
- }
- i++;
- }
- if (loginNumber != -1){
- int stringNumber = 1;
- Scanner scanner = new Scanner (new File ("C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\passwords.txt"));
- while (scanner.hasNextLine()) {
- String numline = scanner.nextLine();
- if(stringNumber == loginNumber){
- if(hashLine.equals(numline)){
- System.out.println("OK");
- Charset charset = StandardCharsets.UTF_8;
- Path path = Paths.get("C:\\Users\\nikishkin\\Documents\\Labs\\SKEY\\passwords.txt");
- Files.write(path,
- new String(Files.readAllBytes(path), charset).replace(hashLine, password)
- .getBytes(charset));
- }
- else{
- System.out.println("NOT OK");
- }
- break;
- }
- stringNumber++;
- }
- scanner.close();
- }
- }
- public static String md5Custom(String st) {
- MessageDigest messageDigest = null;
- byte[] digest = new byte[0];
- try {
- messageDigest = MessageDigest.getInstance("MD5");
- messageDigest.reset();
- messageDigest.update(st.getBytes());
- digest = messageDigest.digest();
- } catch (NoSuchAlgorithmException e) {
- // тут можно обработать ошибку
- // возникает она если в передаваемый алгоритм в getInstance(,,,) не существует
- e.printStackTrace();
- }
- BigInteger bigInt = new BigInteger(1, digest);
- String md5Hex = bigInt.toString(16);
- while( md5Hex.length() < 32 ){
- md5Hex = "0" + md5Hex;
- }
- return md5Hex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement