· 7 years ago · Mar 15, 2018, 03:04 PM
1package com.encript;
2import java.security.InvalidKeyException;
3import java.security.MessageDigest;
4import java.security.NoSuchAlgorithmException;
5import java.sql.CallableStatement;
6import java.sql.Connection;
7import java.sql.DriverManager;
8import java.sql.ResultSet;
9import java.text.SimpleDateFormat;
10import java.util.ArrayList;
11import java.util.List;
12import java.sql.Date;
13
14import javax.crypto.BadPaddingException;
15import javax.crypto.Cipher;
16import javax.crypto.IllegalBlockSizeException;
17import javax.crypto.NoSuchPaddingException;
18import javax.crypto.spec.SecretKeySpec;
19
20import oracle.jdbc.OracleTypes;
21
22import org.apache.commons.codec.binary.Base64;
23
24
25
26
27
28
29public class Encript {
30
31 public static void main(String[] args) {
32
33 List<String > test = null;
34
35
36 for(String s:test){
37
38 }
39 callDatabase();
40 System.out.println(generateDigest("migracion", EDigestAlgorithm.MD5));
41 }
42
43 public static void callDatabase(){
44
45 try {
46 Class.forName("oracle.jdbc.driver.OracleDriver");
47
48 Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@172.20.11.103:1521:PRUPLATI", "MIGRACION", "migracion1");
49
50 CallableStatement cs = conn.prepareCall("call PR_PERMANENCIA_VIAJERO.GET_CALCULAR_PERMANENCIA(?, ?, ?, ?, ?, ?, ?)");
51 cs.setLong(1, 3);
52 cs.setString(2, "1094886");
53 cs.setLong(3, 603);
54
55
56 SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy/MM/dd");
57 java.util.Date date=DATE_FORMAT.parse("1986/08/26");
58
59 Date pFechaNacimient= new Date(date.getTime());
60
61 System.out.println(pFechaNacimient);
62 cs.setDate(4, pFechaNacimient);
63 cs.setString(5, "I");
64 cs.setString(6, "PIP5");
65 cs.registerOutParameter(7, OracleTypes.CURSOR);
66 cs.executeUpdate();
67 ResultSet rs = (ResultSet) cs.getObject(7);
68
69 if (rs != null) {
70 while (rs.next()) {
71 System.out.println(rs.getLong(":B4"));
72 System.out.println(rs.getLong(":B3"));
73 System.out.println(rs.getLong(":B2"));
74 System.out.println(rs.getLong(":B1"));
75 }
76 }
77
78 } catch (Exception e) {
79 // TODO Auto-generated catch block
80 e.printStackTrace();
81 }
82 }
83
84 public static String generateDigest(String text, EDigestAlgorithm algorithm) {
85 try {
86 MessageDigest digest = MessageDigest.getInstance(algorithm.getName());
87 byte[] hashBytes = digest.digest(text.getBytes());
88 byte[] encodedBytes = Base64.encodeBase64(hashBytes);
89 String encodedString = new String(encodedBytes);
90 return encodedString;
91
92 } catch (NoSuchAlgorithmException e) {
93 e.printStackTrace();
94 }
95 return null;
96 }
97
98
99 public static String decrypt(String text,EDigestAlgorithm algorithm) {
100 try {
101 // Obtiene el key specs de la llave de crifrado
102 SecretKeySpec skeySpec = getSecretKeySpec(algorithm);
103
104 // Instancia el cipher
105 Cipher cipher = Cipher.getInstance(algorithm.getName());
106 cipher.init(Cipher.DECRYPT_MODE, skeySpec);
107
108 // Descrifra el texto
109 byte[] decodedBytes = Base64.decodeBase64(text.getBytes());
110 byte[] decryptedBytes = cipher.doFinal(decodedBytes);
111 String originalString = new String(decryptedBytes);
112 return originalString;
113
114 } catch (NoSuchAlgorithmException e) {
115 e.printStackTrace();
116 } catch (NoSuchPaddingException e) {
117 e.printStackTrace();
118 } catch (InvalidKeyException e) {
119 e.printStackTrace();
120 } catch (BadPaddingException e) {
121 e.printStackTrace();
122 } catch (IllegalBlockSizeException e) {
123 e.printStackTrace();
124 }
125 return null;
126 }
127 private static SecretKeySpec getSecretKeySpec(EDigestAlgorithm algorithm) throws NoSuchAlgorithmException {
128 byte[] secretKey = Base64.decodeBase64(SECRET_KEY.getBytes());
129 SecretKeySpec skeySpec = new SecretKeySpec(secretKey, algorithm.getName());
130 return skeySpec;
131 }
132 private static final String SECRET_KEY = "TXz3JoI86CCn5WA/FOkTRQ==";
133}