Showing
7 changed files
with
504 additions
and
67 deletions
... | @@ -48,6 +48,11 @@ class DecryptWindow extends Frame { | ... | @@ -48,6 +48,11 @@ class DecryptWindow extends Frame { |
48 | String folderDir = ".\\" + userId + "\\Encrypted"; | 48 | String folderDir = ".\\" + userId + "\\Encrypted"; |
49 | 49 | ||
50 | final File dirToLock = new File(folderDir); | 50 | final File dirToLock = new File(folderDir); |
51 | + if(!dirToLock.exists()) | ||
52 | + { | ||
53 | + JOptionPane.showMessageDialog(null,"No files have been encrypted!"); | ||
54 | + return; | ||
55 | + } | ||
51 | JFileChooser jfc = new JFileChooser(dirToLock); | 56 | JFileChooser jfc = new JFileChooser(dirToLock); |
52 | jfc.setFileView(new FileView() { | 57 | jfc.setFileView(new FileView() { |
53 | @Override | 58 | @Override |
... | @@ -62,7 +67,7 @@ class DecryptWindow extends Frame { | ... | @@ -62,7 +67,7 @@ class DecryptWindow extends Frame { |
62 | 67 | ||
63 | lpwd = new Label("Enter password: ",Label.RIGHT); | 68 | lpwd = new Label("Enter password: ",Label.RIGHT); |
64 | 69 | ||
65 | - lmod = new Label("열람 목적: ",Label.RIGHT); | 70 | + lmod = new Label("Enter purpose: ",Label.RIGHT); |
66 | 71 | ||
67 | 72 | ||
68 | //약 10개의 글자를 입력할 수 있는 TextField 생성. | 73 | //약 10개의 글자를 입력할 수 있는 TextField 생성. |
... | @@ -71,7 +76,7 @@ class DecryptWindow extends Frame { | ... | @@ -71,7 +76,7 @@ class DecryptWindow extends Frame { |
71 | 76 | ||
72 | tfMod = new TextField(10); | 77 | tfMod = new TextField(10); |
73 | //입력한 값 대신 '*'이 보이게 한다. | 78 | //입력한 값 대신 '*'이 보이게 한다. |
74 | - | 79 | + tfPwd.setEchoChar('*'); |
75 | 80 | ||
76 | 81 | ||
77 | ok = new Button("OK"); | 82 | ok = new Button("OK"); |
... | @@ -102,7 +107,9 @@ class DecryptWindow extends Frame { | ... | @@ -102,7 +107,9 @@ class DecryptWindow extends Frame { |
102 | 107 | ||
103 | add(ok); | 108 | add(ok); |
104 | 109 | ||
105 | - setSize(450,65); | 110 | + setSize(450,100); |
111 | + | ||
112 | + this.setLocationRelativeTo(null); | ||
106 | 113 | ||
107 | //화면이 보이게 한다. | 114 | //화면이 보이게 한다. |
108 | 115 | ||
... | @@ -126,6 +133,12 @@ class DecryptWindow extends Frame { | ... | @@ -126,6 +133,12 @@ class DecryptWindow extends Frame { |
126 | 133 | ||
127 | dispose(); | 134 | dispose(); |
128 | 135 | ||
136 | + if(tfMod.getText().length() < 1) | ||
137 | + { | ||
138 | + JOptionPane.showMessageDialog(null,"Purpose cannot be null."); | ||
139 | + return; | ||
140 | + } | ||
141 | + | ||
129 | String passwd = userId + ' ' + tfPwd.getText(); | 142 | String passwd = userId + ' ' + tfPwd.getText(); |
130 | char[] password = passwd.toCharArray(); | 143 | char[] password = passwd.toCharArray(); |
131 | String passwd2 = userId + ' ' + userPW; | 144 | String passwd2 = userId + ' ' + userPW; |
... | @@ -151,7 +164,11 @@ class DecryptWindow extends Frame { | ... | @@ -151,7 +164,11 @@ class DecryptWindow extends Frame { |
151 | } catch (IOException ioException) { | 164 | } catch (IOException ioException) { |
152 | ioException.printStackTrace(); | 165 | ioException.printStackTrace(); |
153 | } catch (GeneralSecurityException generalSecurityException) { | 166 | } catch (GeneralSecurityException generalSecurityException) { |
154 | - generalSecurityException.printStackTrace(); | 167 | + String tempdir = ".\\" + userId + "\\logs\\" + fileName + ".txt"; |
168 | + File temp = new File(tempdir); | ||
169 | + temp.delete(); | ||
170 | + JOptionPane.showMessageDialog(null,"Password is invalid!"); | ||
171 | + return; | ||
155 | } | 172 | } |
156 | 173 | ||
157 | dir = ".\\" + userId + "\\logs\\" + fileName + ".txt"; | 174 | dir = ".\\" + userId + "\\logs\\" + fileName + ".txt"; |
... | @@ -175,7 +192,7 @@ class DecryptWindow extends Frame { | ... | @@ -175,7 +192,7 @@ class DecryptWindow extends Frame { |
175 | Date time = new Date(); | 192 | Date time = new Date(); |
176 | 193 | ||
177 | String time1 = format1.format(time); | 194 | String time1 = format1.format(time); |
178 | - String toWrite = "열람\t" + time1 + '\t' + modeIn + '\n'; | 195 | + String toWrite = "OPN\t" + time1 + '\t' + modeIn + '\n'; |
179 | byte[] b = toWrite.getBytes(); | 196 | byte[] b = toWrite.getBytes(); |
180 | try { | 197 | try { |
181 | fos.write(b); | 198 | fos.write(b); | ... | ... |
... | @@ -6,8 +6,12 @@ import java.awt.*; | ... | @@ -6,8 +6,12 @@ import java.awt.*; |
6 | import java.awt.event.ActionEvent; | 6 | import java.awt.event.ActionEvent; |
7 | import java.awt.event.ActionListener; | 7 | import java.awt.event.ActionListener; |
8 | import java.io.File; | 8 | import java.io.File; |
9 | +import java.io.FileNotFoundException; | ||
10 | +import java.io.FileOutputStream; | ||
9 | import java.io.IOException; | 11 | import java.io.IOException; |
10 | import java.security.GeneralSecurityException; | 12 | import java.security.GeneralSecurityException; |
13 | +import java.text.SimpleDateFormat; | ||
14 | +import java.util.Date; | ||
11 | 15 | ||
12 | class DeleteEncypted extends Frame { | 16 | class DeleteEncypted extends Frame { |
13 | 17 | ||
... | @@ -33,6 +37,11 @@ class DeleteEncypted extends Frame { | ... | @@ -33,6 +37,11 @@ class DeleteEncypted extends Frame { |
33 | String folderDir = ".\\" + userId + "\\Encrypted"; | 37 | String folderDir = ".\\" + userId + "\\Encrypted"; |
34 | 38 | ||
35 | final File dirToLock = new File(folderDir); | 39 | final File dirToLock = new File(folderDir); |
40 | + if(!dirToLock.exists()) | ||
41 | + { | ||
42 | + JOptionPane.showMessageDialog(null,"No files have been encrypted!"); | ||
43 | + return; | ||
44 | + } | ||
36 | JFileChooser jfc = new JFileChooser(dirToLock); | 45 | JFileChooser jfc = new JFileChooser(dirToLock); |
37 | jfc.setFileView(new FileView() { | 46 | jfc.setFileView(new FileView() { |
38 | @Override | 47 | @Override |
... | @@ -48,7 +57,61 @@ class DeleteEncypted extends Frame { | ... | @@ -48,7 +57,61 @@ class DeleteEncypted extends Frame { |
48 | 57 | ||
49 | if(file.exists()) | 58 | if(file.exists()) |
50 | { | 59 | { |
51 | - file.deleteOnExit(); | 60 | + String fileName = file.getName(); |
61 | + String passwd2 = userId + ' ' + userPW; | ||
62 | + char[] password2 = passwd2.toCharArray(); | ||
63 | + String dir = ".\\" + userId + "\\logs\\" + fileName + ".txt"; | ||
64 | + String dir2 = ".\\" + userId + "\\logs\\" + fileName + ".log"; | ||
65 | + | ||
66 | + try { | ||
67 | + Cryptor.decrypt(dir2, dir, password2); | ||
68 | + } catch (IOException ioException) { | ||
69 | + ioException.printStackTrace(); | ||
70 | + } catch (GeneralSecurityException generalSecurityException) { | ||
71 | + generalSecurityException.printStackTrace(); | ||
72 | + } | ||
73 | + | ||
74 | + FileOutputStream fos = null; | ||
75 | + try { | ||
76 | + fos = new FileOutputStream(dir,true); | ||
77 | + } catch (FileNotFoundException fileNotFoundException) { | ||
78 | + fileNotFoundException.printStackTrace(); | ||
79 | + } | ||
80 | + SimpleDateFormat format1 = new SimpleDateFormat( "yyyy-MM-dd HH:mm"); | ||
81 | + | ||
82 | + Date time = new Date(); | ||
83 | + | ||
84 | + String time1 = format1.format(time); | ||
85 | + String toWrite = "DEL\t" + time1 + '\t' + "Finished using File" + '\n'; | ||
86 | + byte[] b = toWrite.getBytes(); | ||
87 | + try { | ||
88 | + fos.write(b); | ||
89 | + } catch (IOException ioException) { | ||
90 | + ioException.printStackTrace(); | ||
91 | + } | ||
92 | + try { | ||
93 | + fos.flush(); | ||
94 | + } catch (IOException ioException) { | ||
95 | + ioException.printStackTrace(); | ||
96 | + } | ||
97 | + try { | ||
98 | + fos.close(); | ||
99 | + } catch (IOException ioException) { | ||
100 | + ioException.printStackTrace(); | ||
101 | + } | ||
102 | + | ||
103 | + try { | ||
104 | + Cryptor.encrypt(dir, dir2, password2); | ||
105 | + } catch (IOException ioException) { | ||
106 | + ioException.printStackTrace(); | ||
107 | + } catch (GeneralSecurityException generalSecurityException) { | ||
108 | + generalSecurityException.printStackTrace(); | ||
109 | + } | ||
110 | + | ||
111 | + File temp = new File(dir); | ||
112 | + temp.delete(); | ||
113 | + | ||
114 | + file.delete(); | ||
52 | } | 115 | } |
53 | } | 116 | } |
54 | 117 | ... | ... |
... | @@ -51,7 +51,7 @@ class EncryptWindow extends Frame { | ... | @@ -51,7 +51,7 @@ class EncryptWindow extends Frame { |
51 | 51 | ||
52 | lpwd = new Label("Enter password: ",Label.RIGHT); | 52 | lpwd = new Label("Enter password: ",Label.RIGHT); |
53 | 53 | ||
54 | - lmod = new Label("사용 목적: ",Label.RIGHT); | 54 | + lmod = new Label("Enter purpose: ",Label.RIGHT); |
55 | 55 | ||
56 | 56 | ||
57 | //약 10개의 글자를 입력할 수 있는 TextField 생성. | 57 | //약 10개의 글자를 입력할 수 있는 TextField 생성. |
... | @@ -60,7 +60,7 @@ class EncryptWindow extends Frame { | ... | @@ -60,7 +60,7 @@ class EncryptWindow extends Frame { |
60 | 60 | ||
61 | tfMod = new TextField(10); | 61 | tfMod = new TextField(10); |
62 | //입력한 값 대신 '*'이 보이게 한다. | 62 | //입력한 값 대신 '*'이 보이게 한다. |
63 | - | 63 | + tfPwd.setEchoChar('*'); |
64 | 64 | ||
65 | 65 | ||
66 | ok = new Button("OK"); | 66 | ok = new Button("OK"); |
... | @@ -90,7 +90,9 @@ class EncryptWindow extends Frame { | ... | @@ -90,7 +90,9 @@ class EncryptWindow extends Frame { |
90 | 90 | ||
91 | add(ok); | 91 | add(ok); |
92 | 92 | ||
93 | - setSize(450,65); | 93 | + setSize(450,100); |
94 | + | ||
95 | + this.setLocationRelativeTo(null); | ||
94 | 96 | ||
95 | //화면이 보이게 한다. | 97 | //화면이 보이게 한다. |
96 | 98 | ||
... | @@ -112,6 +114,12 @@ class EncryptWindow extends Frame { | ... | @@ -112,6 +114,12 @@ class EncryptWindow extends Frame { |
112 | 114 | ||
113 | dispose(); | 115 | dispose(); |
114 | 116 | ||
117 | + if(tfPwd.getText().length() < 1 || tfMod.getText().length() < 1) | ||
118 | + { | ||
119 | + JOptionPane.showMessageDialog(null,"Password and purpose cannot be null."); | ||
120 | + return; | ||
121 | + } | ||
122 | + | ||
115 | String passwd = userId + ' ' + tfPwd.getText(); | 123 | String passwd = userId + ' ' + tfPwd.getText(); |
116 | char[] password = passwd.toCharArray(); | 124 | char[] password = passwd.toCharArray(); |
117 | String passwd2 = userId + ' ' + userPW; | 125 | String passwd2 = userId + ' ' + userPW; |
... | @@ -166,7 +174,7 @@ class EncryptWindow extends Frame { | ... | @@ -166,7 +174,7 @@ class EncryptWindow extends Frame { |
166 | Date time = new Date(); | 174 | Date time = new Date(); |
167 | 175 | ||
168 | String time1 = format1.format(time); | 176 | String time1 = format1.format(time); |
169 | - String toWrite = "등록\t" + time1 + '\t' + modeIn + '\n'; | 177 | + String toWrite = "REG\t" + time1 + '\t' + modeIn + '\n'; |
170 | byte[] b = toWrite.getBytes(); | 178 | byte[] b = toWrite.getBytes(); |
171 | try { | 179 | try { |
172 | fos.write(b); | 180 | fos.write(b); | ... | ... |
... | @@ -37,6 +37,11 @@ class LogWindow extends Frame { | ... | @@ -37,6 +37,11 @@ class LogWindow extends Frame { |
37 | String folderDir = ".\\" + userId + "\\logs"; | 37 | String folderDir = ".\\" + userId + "\\logs"; |
38 | 38 | ||
39 | final File dirToLock = new File(folderDir); | 39 | final File dirToLock = new File(folderDir); |
40 | + if(!dirToLock.exists()) | ||
41 | + { | ||
42 | + JOptionPane.showMessageDialog(null,"No files have been encrypted!"); | ||
43 | + return; | ||
44 | + } | ||
40 | JFileChooser jfc = new JFileChooser(dirToLock); | 45 | JFileChooser jfc = new JFileChooser(dirToLock); |
41 | jfc.setFileView(new FileView() { | 46 | jfc.setFileView(new FileView() { |
42 | @Override | 47 | @Override |
... | @@ -83,7 +88,7 @@ class LogWindow extends Frame { | ... | @@ -83,7 +88,7 @@ class LogWindow extends Frame { |
83 | } catch (InterruptedException interruptedException) { | 88 | } catch (InterruptedException interruptedException) { |
84 | interruptedException.printStackTrace(); | 89 | interruptedException.printStackTrace(); |
85 | } | 90 | } |
86 | - file.deleteOnExit(); | 91 | + file.delete(); |
87 | } | 92 | } |
88 | 93 | ||
89 | public static void main(String args[]){ | 94 | public static void main(String args[]){ | ... | ... |
... | @@ -70,7 +70,7 @@ class MainMenu extends Frame { | ... | @@ -70,7 +70,7 @@ class MainMenu extends Frame { |
70 | 70 | ||
71 | //LayoutManager를 FlowLayout으로 | 71 | //LayoutManager를 FlowLayout으로 |
72 | 72 | ||
73 | - setLayout(new FlowLayout()); | 73 | + setLayout(new GridLayout(4,1)); |
74 | 74 | ||
75 | //생성한 component들을 Frame에 포함시킨다. | 75 | //생성한 component들을 Frame에 포함시킨다. |
76 | 76 | ||
... | @@ -82,7 +82,9 @@ class MainMenu extends Frame { | ... | @@ -82,7 +82,9 @@ class MainMenu extends Frame { |
82 | 82 | ||
83 | add(bDelete); | 83 | add(bDelete); |
84 | 84 | ||
85 | - setSize(450,200); | 85 | + setSize(450,400); |
86 | + | ||
87 | + this.setLocationRelativeTo(null); | ||
86 | 88 | ||
87 | //화면이 보이게 한다. | 89 | //화면이 보이게 한다. |
88 | 90 | ||
... | @@ -102,60 +104,6 @@ class MainMenu extends Frame { | ... | @@ -102,60 +104,6 @@ class MainMenu extends Frame { |
102 | if (file.isDirectory()) { | 104 | if (file.isDirectory()) { |
103 | deleteDirectory(file, userId, userPW); | 105 | deleteDirectory(file, userId, userPW); |
104 | } else { | 106 | } else { |
105 | - String fileName = file.getName(); | ||
106 | - String passwd2 = userId + ' ' + userPW; | ||
107 | - char[] password2 = passwd2.toCharArray(); | ||
108 | - String dir = ".\\" + userId + "\\logs\\" + fileName + ".txt"; | ||
109 | - String dir2 = ".\\" + userId + "\\logs\\" + fileName + ".log"; | ||
110 | - | ||
111 | - try { | ||
112 | - Cryptor.decrypt(dir2, dir, password2); | ||
113 | - } catch (IOException ioException) { | ||
114 | - ioException.printStackTrace(); | ||
115 | - } catch (GeneralSecurityException generalSecurityException) { | ||
116 | - generalSecurityException.printStackTrace(); | ||
117 | - } | ||
118 | - | ||
119 | - FileOutputStream fos = null; | ||
120 | - try { | ||
121 | - fos = new FileOutputStream(dir,true); | ||
122 | - } catch (FileNotFoundException fileNotFoundException) { | ||
123 | - fileNotFoundException.printStackTrace(); | ||
124 | - } | ||
125 | - SimpleDateFormat format1 = new SimpleDateFormat( "yyyy-MM-dd HH:mm"); | ||
126 | - | ||
127 | - Date time = new Date(); | ||
128 | - | ||
129 | - String time1 = format1.format(time); | ||
130 | - String toWrite = "삭제\t" + time1 + '\t' + "사용 완료" + '\n'; | ||
131 | - byte[] b = toWrite.getBytes(); | ||
132 | - try { | ||
133 | - fos.write(b); | ||
134 | - } catch (IOException ioException) { | ||
135 | - ioException.printStackTrace(); | ||
136 | - } | ||
137 | - try { | ||
138 | - fos.flush(); | ||
139 | - } catch (IOException ioException) { | ||
140 | - ioException.printStackTrace(); | ||
141 | - } | ||
142 | - try { | ||
143 | - fos.close(); | ||
144 | - } catch (IOException ioException) { | ||
145 | - ioException.printStackTrace(); | ||
146 | - } | ||
147 | - | ||
148 | - try { | ||
149 | - Cryptor.encrypt(dir, dir2, password2); | ||
150 | - } catch (IOException ioException) { | ||
151 | - ioException.printStackTrace(); | ||
152 | - } catch (GeneralSecurityException generalSecurityException) { | ||
153 | - generalSecurityException.printStackTrace(); | ||
154 | - } | ||
155 | - | ||
156 | - File temp = new File(dir); | ||
157 | - temp.delete(); | ||
158 | - | ||
159 | file.delete(); | 107 | file.delete(); |
160 | } | 108 | } |
161 | } | 109 | } | ... | ... |
DLPonUSB/src/cryptor/SignIn.java
0 → 100644
1 | +package cryptor; | ||
2 | + | ||
3 | +import javax.annotation.processing.Filer; | ||
4 | +import javax.swing.*; | ||
5 | +import java.awt.*; | ||
6 | +import java.awt.event.ActionEvent; | ||
7 | +import java.awt.event.ActionListener; | ||
8 | +import java.awt.event.WindowAdapter; | ||
9 | +import java.awt.event.WindowEvent; | ||
10 | +import java.io.*; | ||
11 | +import java.security.GeneralSecurityException; | ||
12 | +import java.security.MessageDigest; | ||
13 | +import java.security.NoSuchAlgorithmException; | ||
14 | + | ||
15 | +class SignIn extends Frame { | ||
16 | + | ||
17 | + Label lid; | ||
18 | + | ||
19 | + Label lpwd; | ||
20 | + | ||
21 | + TextField tfId; | ||
22 | + | ||
23 | + TextField tfPwd; | ||
24 | + | ||
25 | + Button ok; | ||
26 | + | ||
27 | + Button su; | ||
28 | + | ||
29 | + SignIn(String title){ | ||
30 | + | ||
31 | + //Frame(String title)을 호출한다. | ||
32 | + | ||
33 | + super(title); | ||
34 | + | ||
35 | + addWindowListener(new WindowAdapter() { | ||
36 | + @Override | ||
37 | + public void windowClosing(WindowEvent e) { | ||
38 | + dispose(); | ||
39 | + System.exit(0); | ||
40 | + } | ||
41 | + }); | ||
42 | + | ||
43 | + //Label의 text정렬을 오른쪽으로 | ||
44 | + | ||
45 | + lid = new Label("ID : ",Label.RIGHT); | ||
46 | + | ||
47 | + lpwd = new Label("Password : ",Label.RIGHT); | ||
48 | + | ||
49 | + | ||
50 | + //약 10개의 글자를 입력할 수 있는 TextField 생성. | ||
51 | + | ||
52 | + tfId = new TextField(10); | ||
53 | + | ||
54 | + tfPwd = new TextField(10); | ||
55 | + | ||
56 | + //입력한 값 대신 '*'이 보이게 한다. | ||
57 | + tfPwd.setEchoChar('*'); | ||
58 | + | ||
59 | + ok = new Button("OK"); | ||
60 | + | ||
61 | + su = new Button("Sign Up"); | ||
62 | + | ||
63 | + //OK버튼과 TextField에 이벤트처리를 위한 Listener를 추가해준다. | ||
64 | + | ||
65 | + tfId.addActionListener(new EventHandler()); | ||
66 | + | ||
67 | + tfPwd.addActionListener(new EventHandler()); | ||
68 | + | ||
69 | + ok.addActionListener(new EventHandler()); | ||
70 | + | ||
71 | + su.addActionListener(e -> { | ||
72 | + SignUp f = new SignUp("Sign Up"); | ||
73 | + }); | ||
74 | + | ||
75 | + | ||
76 | + //LayoutManager를 FlowLayout으로 | ||
77 | + | ||
78 | + setLayout(new FlowLayout()); | ||
79 | + | ||
80 | + //생성한 component들을 Frame에 포함시킨다. | ||
81 | + | ||
82 | + add(lid); | ||
83 | + | ||
84 | + add(tfId); | ||
85 | + | ||
86 | + add(lpwd); | ||
87 | + | ||
88 | + add(tfPwd); | ||
89 | + | ||
90 | + | ||
91 | + add(ok); | ||
92 | + | ||
93 | + add(su); | ||
94 | + | ||
95 | + setSize(450,80); | ||
96 | + | ||
97 | + this.setLocationRelativeTo(null); | ||
98 | + | ||
99 | + //화면이 보이게 한다. | ||
100 | + | ||
101 | + setVisible(true); | ||
102 | + | ||
103 | + } | ||
104 | + | ||
105 | + public static void main(String args[]){ | ||
106 | + | ||
107 | + SignIn f = new SignIn("Sign In"); | ||
108 | + | ||
109 | + } | ||
110 | + | ||
111 | + | ||
112 | + | ||
113 | + class EventHandler implements ActionListener{ | ||
114 | + | ||
115 | + public void actionPerformed(ActionEvent e){ | ||
116 | + | ||
117 | + String passwd = tfId.getText() + ' ' + tfPwd.getText(); | ||
118 | + char[] password = passwd.toCharArray(); | ||
119 | + | ||
120 | + String dir = ".\\Users\\" + tfId.getText() + ".txt"; | ||
121 | + String dir2 = ".\\Users\\" + tfId.getText() + ".info"; | ||
122 | + File f = new File(dir2); | ||
123 | + if(f.exists()) | ||
124 | + { | ||
125 | + try { | ||
126 | + Cryptor.decrypt(dir2,dir,password); | ||
127 | + } catch (IOException ioException) { | ||
128 | + ioException.printStackTrace(); | ||
129 | + } catch (GeneralSecurityException generalSecurityException) { | ||
130 | + JOptionPane.showMessageDialog(null,"Password is invalid!"); | ||
131 | + return; | ||
132 | + } | ||
133 | + | ||
134 | + dispose(); | ||
135 | + | ||
136 | + File file = new File(dir); | ||
137 | + FileReader fr = null; | ||
138 | + try { | ||
139 | + fr = new FileReader(file); | ||
140 | + } catch (FileNotFoundException fileNotFoundException) { | ||
141 | + fileNotFoundException.printStackTrace(); | ||
142 | + } | ||
143 | + | ||
144 | + BufferedReader bufReader = new BufferedReader(fr); | ||
145 | + String line = ""; | ||
146 | + try { | ||
147 | + line = bufReader.readLine(); | ||
148 | + } catch (IOException ioException) { | ||
149 | + ioException.printStackTrace(); | ||
150 | + } | ||
151 | + String[] strs = line.split(String.valueOf('\t')); | ||
152 | + | ||
153 | + try { | ||
154 | + fr.close(); | ||
155 | + } catch (IOException ioException) { | ||
156 | + ioException.printStackTrace(); | ||
157 | + } | ||
158 | + | ||
159 | + file.delete(); | ||
160 | + | ||
161 | + try { | ||
162 | + if(strs[1].equals(byteToHexString(sha256(tfPwd.getText())))) | ||
163 | + { | ||
164 | + MainMenu mm = new MainMenu("Main Menu", strs[0], tfPwd.getText()); | ||
165 | + } | ||
166 | + else | ||
167 | + { | ||
168 | + JOptionPane.showMessageDialog(null,"Password is invalid!"); | ||
169 | + } | ||
170 | + } catch (NoSuchAlgorithmException noSuchAlgorithmException) { | ||
171 | + noSuchAlgorithmException.printStackTrace(); | ||
172 | + } | ||
173 | + } | ||
174 | + else | ||
175 | + { | ||
176 | + JOptionPane.showMessageDialog(null,tfId.getText() + " has never signed Up!"); | ||
177 | + } | ||
178 | + } | ||
179 | + | ||
180 | + public byte[] sha256(String msg) throws NoSuchAlgorithmException { | ||
181 | + MessageDigest md = MessageDigest.getInstance("SHA-256"); | ||
182 | + md.update(msg.getBytes()); | ||
183 | + | ||
184 | + return md.digest(); | ||
185 | + } | ||
186 | + | ||
187 | + public String byteToHexString(byte[] data) { | ||
188 | + StringBuilder sb = new StringBuilder(); | ||
189 | + | ||
190 | + for (byte b : data) { | ||
191 | + sb.append(String.format("%02x", b)); | ||
192 | + } | ||
193 | + | ||
194 | + return sb.toString(); | ||
195 | + } | ||
196 | + } | ||
197 | + | ||
198 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
DLPonUSB/src/cryptor/SignUp.java
0 → 100644
1 | +package cryptor; | ||
2 | + | ||
3 | +import javax.swing.*; | ||
4 | +import java.awt.*; | ||
5 | +import java.awt.event.ActionEvent; | ||
6 | +import java.awt.event.ActionListener; | ||
7 | +import java.awt.event.WindowAdapter; | ||
8 | +import java.awt.event.WindowEvent; | ||
9 | +import java.io.File; | ||
10 | +import java.io.FileNotFoundException; | ||
11 | +import java.io.FileOutputStream; | ||
12 | +import java.io.IOException; | ||
13 | +import java.security.GeneralSecurityException; | ||
14 | +import java.security.MessageDigest; | ||
15 | +import java.security.NoSuchAlgorithmException; | ||
16 | + | ||
17 | +class SignUp extends Frame { | ||
18 | + | ||
19 | + Label lid; | ||
20 | + | ||
21 | + Label lpwd; | ||
22 | + | ||
23 | + TextField tfId; | ||
24 | + | ||
25 | + TextField tfPwd; | ||
26 | + | ||
27 | + Button ok; | ||
28 | + | ||
29 | + SignUp(String title){ | ||
30 | + | ||
31 | + //Frame(String title)을 호출한다. | ||
32 | + | ||
33 | + super(title); | ||
34 | + | ||
35 | + addWindowListener(new WindowAdapter() { | ||
36 | + @Override | ||
37 | + public void windowClosing(WindowEvent e) { | ||
38 | + dispose(); | ||
39 | + } | ||
40 | + }); | ||
41 | + | ||
42 | + //Label의 text정렬을 오른쪽으로 | ||
43 | + | ||
44 | + lid = new Label("ID : ",Label.RIGHT); | ||
45 | + | ||
46 | + lpwd = new Label("Password : ",Label.RIGHT); | ||
47 | + | ||
48 | + | ||
49 | + //약 10개의 글자를 입력할 수 있는 TextField 생성. | ||
50 | + | ||
51 | + tfId = new TextField(10); | ||
52 | + | ||
53 | + tfPwd = new TextField(10); | ||
54 | + | ||
55 | + //입력한 값 대신 '*'이 보이게 한다. | ||
56 | + tfPwd.setEchoChar('*'); | ||
57 | + | ||
58 | + ok = new Button("OK"); | ||
59 | + | ||
60 | + //OK버튼과 TextField에 이벤트처리를 위한 Listener를 추가해준다. | ||
61 | + | ||
62 | + tfId.addActionListener(new EventHandler()); | ||
63 | + | ||
64 | + tfPwd.addActionListener(new EventHandler()); | ||
65 | + | ||
66 | + ok.addActionListener(new EventHandler()); | ||
67 | + | ||
68 | + | ||
69 | + | ||
70 | + //LayoutManager를 FlowLayout으로 | ||
71 | + | ||
72 | + setLayout(new FlowLayout()); | ||
73 | + | ||
74 | + //생성한 component들을 Frame에 포함시킨다. | ||
75 | + | ||
76 | + add(lid); | ||
77 | + | ||
78 | + add(tfId); | ||
79 | + | ||
80 | + add(lpwd); | ||
81 | + | ||
82 | + add(tfPwd); | ||
83 | + | ||
84 | + | ||
85 | + add(ok); | ||
86 | + | ||
87 | + setSize(450,80); | ||
88 | + | ||
89 | + this.setLocationRelativeTo(null); | ||
90 | + | ||
91 | + //화면이 보이게 한다. | ||
92 | + | ||
93 | + setVisible(true); | ||
94 | + | ||
95 | + } | ||
96 | + | ||
97 | + public static void main(String args[]){ | ||
98 | + | ||
99 | + SignUp f = new SignUp("Sign Up"); | ||
100 | + | ||
101 | + } | ||
102 | + | ||
103 | + | ||
104 | + | ||
105 | + class EventHandler implements ActionListener{ | ||
106 | + | ||
107 | + public void actionPerformed(ActionEvent e){ | ||
108 | + | ||
109 | + if(tfId.getText().length() < 1 || tfPwd.getText().length() < 1) | ||
110 | + { | ||
111 | + JOptionPane.showMessageDialog(null,"ID and password cannot be null."); | ||
112 | + return; | ||
113 | + } | ||
114 | + | ||
115 | + String passwd = tfId.getText() + ' ' + tfPwd.getText(); | ||
116 | + char[] password = passwd.toCharArray(); | ||
117 | + | ||
118 | + String dir = ".\\Users\\" + tfId.getText() + ".txt"; | ||
119 | + String dir2 = ".\\Users\\" + tfId.getText() + ".info"; | ||
120 | + File f = new File(dir2); | ||
121 | + if(!f.exists()) | ||
122 | + { | ||
123 | + dispose(); | ||
124 | + | ||
125 | + File file = new File(dir); | ||
126 | + file.getParentFile().mkdir(); | ||
127 | + try { | ||
128 | + file.createNewFile(); | ||
129 | + } catch (IOException ioException) { | ||
130 | + ioException.printStackTrace(); | ||
131 | + } | ||
132 | + | ||
133 | + FileOutputStream fos = null; | ||
134 | + try { | ||
135 | + fos = new FileOutputStream(file); | ||
136 | + } catch (FileNotFoundException fileNotFoundException) { | ||
137 | + fileNotFoundException.printStackTrace(); | ||
138 | + } | ||
139 | + | ||
140 | + String toWrite = null; | ||
141 | + try { | ||
142 | + toWrite = tfId.getText() + '\t' + byteToHexString(sha256(tfPwd.getText())); | ||
143 | + } catch (NoSuchAlgorithmException noSuchAlgorithmException) { | ||
144 | + noSuchAlgorithmException.printStackTrace(); | ||
145 | + } | ||
146 | + | ||
147 | + byte[] b = toWrite.getBytes(); | ||
148 | + try { | ||
149 | + fos.write(b); | ||
150 | + } catch (IOException ioException) { | ||
151 | + ioException.printStackTrace(); | ||
152 | + } | ||
153 | + try { | ||
154 | + fos.flush(); | ||
155 | + } catch (IOException ioException) { | ||
156 | + ioException.printStackTrace(); | ||
157 | + } | ||
158 | + try { | ||
159 | + fos.close(); | ||
160 | + } catch (IOException ioException) { | ||
161 | + ioException.printStackTrace(); | ||
162 | + } | ||
163 | + | ||
164 | + try { | ||
165 | + Cryptor.encrypt(dir, dir2, password); | ||
166 | + } catch (IOException ioException) { | ||
167 | + ioException.printStackTrace(); | ||
168 | + } catch (GeneralSecurityException generalSecurityException) { | ||
169 | + generalSecurityException.printStackTrace(); | ||
170 | + } | ||
171 | + | ||
172 | + file.delete(); | ||
173 | + } | ||
174 | + else | ||
175 | + { | ||
176 | + JOptionPane.showMessageDialog(null,tfId.getText() + " already exists!"); | ||
177 | + } | ||
178 | + } | ||
179 | + | ||
180 | + public byte[] sha256(String msg) throws NoSuchAlgorithmException { | ||
181 | + MessageDigest md = MessageDigest.getInstance("SHA-256"); | ||
182 | + md.update(msg.getBytes()); | ||
183 | + | ||
184 | + return md.digest(); | ||
185 | + } | ||
186 | + | ||
187 | + public String byteToHexString(byte[] data) { | ||
188 | + StringBuilder sb = new StringBuilder(); | ||
189 | + | ||
190 | + for (byte b : data) { | ||
191 | + sb.append(String.format("%02x", b)); | ||
192 | + } | ||
193 | + | ||
194 | + return sb.toString(); | ||
195 | + } | ||
196 | + } | ||
197 | + | ||
198 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment