This repository was archived by the owner on Mar 28, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.java
More file actions
52 lines (47 loc) · 2.04 KB
/
main.java
File metadata and controls
52 lines (47 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
private static final int SHIFT = 150;
private static final String PATH = "data/pass.txt";
public static void main(String[] args) {
System.out.print("Пароль: ");
Scanner in = new Scanner(System.in);
String password = in.nextLine();
System.out.println(password);
String encodeText = encode(password, SHIFT);
System.out.println("Зашифрованный пароль: " + encodeText);
save(encodeText, PATH);
System.out.println("Расшифрованный пароль: " + decode(write(PATH), SHIFT));
in.close();
}
//Шифрование (сдвиг символов на величину shift. Каждый последующий символ сдвигается на одну позицию больше предыдущего)
public static String encode(String text, int shift) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++)
sb.append((char) (text.charAt(i) + shift + i));
return sb.toString();
}
//Расшифровка
public static String decode(String text, int shift) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < text.length(); i++)
sb.append((char) (text.charAt(i) - shift - i));
return sb.toString();
}
//Запись в файл
public static void save(String text, String path) {
try (BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path)))) {
bw.write(text);
} catch (IOException exc) {
System.out.println(exc.getMessage());
}
}
//Чтение из файла
public static String write(String path) {
String decode = null;
try (BufferedReader br = new BufferedReader(new FileReader(new File(path)))) {
decode = br.readLine();
} catch (FileNotFoundException exc) {
System.out.println("Файл не найден");
} catch (IOException exc) {
System.out.println(exc.getMessage());
}
return decode;
}