· 7 years ago · Dec 04, 2018, 08:48 PM
1package Inicio;
2
3import org.apache.commons.codec.binary.Base64;
4import com.google.zxing.BinaryBitmap;
5import com.google.zxing.LuminanceSource;
6import com.google.zxing.Result;
7import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
8import com.google.zxing.common.BitMatrix;
9import com.google.zxing.common.HybridBinarizer;
10import com.google.zxing.qrcode.QRCodeReader;
11import com.google.zxing.qrcode.QRCodeWriter;
12
13import javax.swing.JFrame;
14import javax.swing.JPanel;
15
16import javax.swing.border.EmptyBorder;
17import javax.swing.JLabel;
18import javax.swing.JOptionPane;
19
20import javax.swing.JTextField;
21import javax.crypto.Cipher;
22import javax.crypto.SecretKey;
23import javax.crypto.spec.SecretKeySpec;
24import javax.imageio.ImageIO;
25
26import javax.swing.JButton;
27import java.awt.event.ActionListener;
28import java.awt.image.BufferedImage;
29import java.awt.Color;
30import java.awt.Graphics2D;
31import java.awt.event.ActionEvent;
32import java.io.*;
33import java.security.MessageDigest;
34import java.util.*;
35
36public class Crearcuenta extends JFrame {
37
38
39 private JPanel contentPane;
40 private JTextField NCompleto;
41 private JTextField RUT;
42
43
44 public static void main(String[] args) {
45 try {
46 Crearcuenta frame = new Crearcuenta();
47 frame.setVisible(true);
48
49 } catch (Exception e) {
50 e.printStackTrace();
51 }
52 }public static String Encriptar(String texto) {
53
54 String secretKey = "qualityinfosolutions"; //llave para encriptar datos
55 String base64EncryptedString = "";
56
57 try {
58
59 MessageDigest md = MessageDigest.getInstance("MD5");
60 byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
61 byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
62
63 SecretKey key = new SecretKeySpec(keyBytes, "DESede");
64 Cipher cipher = Cipher.getInstance("DESede");
65 cipher.init(Cipher.ENCRYPT_MODE, key);
66
67 byte[] plainTextBytes = texto.getBytes("utf-8");
68 byte[] buf = cipher.doFinal(plainTextBytes);
69 byte[] base64Bytes = Base64.encodeBase64(buf);
70 base64EncryptedString = new String(base64Bytes);
71
72 } catch (Exception ex) {
73 }
74 return base64EncryptedString;
75 }
76
77 public static String Desencriptar(String textoEncriptado) throws Exception {
78
79 String secretKey = "qualityinfosolutions"; //llave para desenciptar datos
80 String base64EncryptedString = "";
81
82 try {
83 byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
84 MessageDigest md = MessageDigest.getInstance("MD5");
85 byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
86 byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
87 SecretKey key = new SecretKeySpec(keyBytes, "DESede");
88
89 Cipher decipher = Cipher.getInstance("DESede");
90 decipher.init(Cipher.DECRYPT_MODE, key);
91
92 byte[] plainText = decipher.doFinal(message);
93
94 base64EncryptedString = new String(plainText, "UTF-8");
95
96 } catch (Exception ex) {
97 }
98 return base64EncryptedString;
99 }
100
101
102 public File generateQR(File file, String eMD5, int h, int w) throws Exception {
103
104 QRCodeWriter writer = new QRCodeWriter();
105 BitMatrix matrix = writer.encode(eMD5, com.google.zxing.BarcodeFormat.QR_CODE, w, h);
106
107 BufferedImage image = new BufferedImage(matrix.getWidth(), matrix.getHeight(), BufferedImage.TYPE_INT_RGB);
108 image.createGraphics();
109
110 Graphics2D graphics = (Graphics2D) image.getGraphics();
111 graphics.setColor(Color.WHITE);
112 graphics.fillRect(0, 0, matrix.getWidth(), matrix.getHeight());
113 graphics.setColor(Color.BLACK);
114
115 for (int i = 0; i < matrix.getWidth(); i++) {
116 for (int j = 0; j < matrix.getHeight(); j++) {
117 if (matrix.get(i, j)) {
118 graphics.fillRect(i, j, 1, 1);
119 }
120 }
121 }
122 ImageIO.write(image, "png", file);
123 return file;
124 }
125
126
127 public String decoder(File file) throws Exception {
128
129 FileInputStream inputStream = new FileInputStream(file);
130
131 BufferedImage image = ImageIO.read(inputStream);
132
133 int width = image.getWidth();
134 int height = image.getHeight();
135 int[] pixels = new int[width * height];
136
137 LuminanceSource source = new BufferedImageLuminanceSource(image);
138 BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
139
140 // decode the barcode
141 QRCodeReader reader = new QRCodeReader();
142 Result result = reader.decode(bitmap);
143 return new String(result.getText());
144 }
145 private static boolean isNumeric(String Rut){
146 try {
147 Integer.parseInt(Rut);
148 return true;
149 }
150 catch (NumberFormatException nfe){
151 return false;
152 }
153 }
154 public Crearcuenta() {
155 this.setTitle("Crear cuenta");
156 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
157 setBounds(500, 300, 450, 300);
158 contentPane = new JPanel();
159 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
160 setContentPane(contentPane);
161 contentPane.setLayout(null);
162
163 JLabel lblIngreseSuNombre = new JLabel("Ingrese su nombre completo");
164 lblIngreseSuNombre.setBounds(10, 61, 192, 22);
165 contentPane.add(lblIngreseSuNombre);
166
167 JLabel lblIngreseSuRut = new JLabel("Ingrese su rut");
168 lblIngreseSuRut.setBounds(10, 94, 145, 22);
169 contentPane.add(lblIngreseSuRut);
170
171 NCompleto = new JTextField();
172 NCompleto.setBounds(191, 62, 192, 20);
173 contentPane.add(NCompleto);
174 NCompleto.setColumns(10);
175
176 RUT = new JTextField();
177 RUT.setBounds(191, 94, 192, 20);
178 contentPane.add(RUT);
179 RUT.setColumns(10);
180
181
182 JButton Aceptar = new JButton("ACEPTAR");
183 Aceptar.setBounds(153, 155, 89, 23);
184 contentPane.add(Aceptar);
185 Aceptar.addActionListener(new ActionListener() {
186
187
188
189 public void actionPerformed(ActionEvent arg0) {
190
191 String nombre = NCompleto.getText();
192 String Rut = RUT.getText();
193
194 try {
195
196 if(nombre.equals("") ||Rut.equals("")) { //preguntar profe
197 JOptionPane.showMessageDialog(null,"Por favor, ingrese correctamente sus datos");
198 }else if(Crearcuenta.isNumeric(Rut) ==false) {
199 JOptionPane.showMessageDialog(null,"Por favor, Escriba un Rut Valido");
200 RUT.setText(null);
201 NCompleto.setText(null);
202 }else {
203 String a =nombre+Rut;
204 String encriptado = Encriptar(a);
205 System.out.println(encriptado);
206 BufferedWriter bw = null;
207 FileWriter fw = null;
208 File Datos = new File("Datos.txt");
209 File Llaves = new File("Llaves.txt");
210 if (!Datos.exists())
211 Datos.createNewFile();
212 if (!Llaves.exists())
213 Llaves.createNewFile();
214
215 fw = new FileWriter(Datos.getAbsoluteFile(), true);
216 bw = new BufferedWriter(fw);
217 bw.newLine();
218 bw.write("- "+Rut+" "+nombre+" "+encriptado);
219 bw.newLine();
220 if (bw != null)
221 bw.close();
222 if (fw != null)
223 fw.close();
224 ////////////llenar archivo llaves////////////
225 fw = new FileWriter(Llaves.getAbsoluteFile(), true);
226 bw = new BufferedWriter(fw);
227 bw.newLine();
228 bw.write(Rut+" "+encriptado);
229 bw.newLine();
230 if (bw != null)
231 bw.close();
232 if (fw != null)
233 fw.close();
234 ////////////////
235 Crearcuenta qr = new Crearcuenta();
236 File f = new File("Clave privada de trabajador "+nombre+".png");
237 qr.generateQR(f, encriptado,564,564);
238 System.out.println("Código QR Generado: " + f.getAbsolutePath());
239 String qrString = qr.decoder(f);
240 System.out.println("Texto codificado en formato QR: " + qrString);
241 NCompleto.setText(null);
242 RUT.setText(null);
243 JOptionPane.showMessageDialog(null,"Tu cuenta ha sido creada");
244 JOptionPane.showMessageDialog(null, "Código QR Generado: " + f.getAbsolutePath());
245 }
246 } catch (IOException e) {
247 e.printStackTrace();
248 } catch (Exception e) {
249 e.printStackTrace();
250 }
251 }
252 });
253
254 JButton atras = new JButton("ATRAS");
255 atras.setBounds(10, 227, 89, 23);
256 contentPane.add(atras);
257
258 JLabel lblIngreseSuRut_1 = new JLabel("Ingrese su Rut sin digito verificador");
259 lblIngreseSuRut_1.setBounds(201, 112, 223, 20);
260 contentPane.add(lblIngreseSuRut_1);
261 atras.addActionListener(new ActionListener() {
262 public void actionPerformed(ActionEvent e) {
263 Inicio a = new Inicio();
264 a.setVisible(true);
265 dispose();
266 }
267 });
268 }
269}