Advertisement
Guest User

Untitled

a guest
Jun 15th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.11 KB | None | 0 0
  1. package encrypt.encrypt;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7.  
  8. import javax.crypto.Cipher;
  9. import javax.crypto.spec.IvParameterSpec;
  10. import javax.crypto.spec.SecretKeySpec;
  11. import javax.sql.DataSource;
  12. import javax.xml.bind.DatatypeConverter;
  13.  
  14. import org.springframework.jdbc.datasource.SimpleDriverDataSource;
  15.  
  16. import com.mysql.jdbc.Driver;
  17. import com.mysql.jdbc.Statement;
  18.  
  19. public class App {
  20. public static void main(String[] args) {
  21. Connection conn = null;
  22. DataSource ds = null;
  23. ResultSet resultSet = null;
  24. Statement statement = null;
  25.  
  26. try {
  27.  
  28. Driver driver = new com.mysql.jdbc.Driver();
  29. String url = "jdbc:mysql://localhost:5555/nktrans_test_pst";
  30. String username = "test";
  31. String password = "test";
  32. ds = new SimpleDriverDataSource(driver, url, username, password);
  33. conn = ds.getConnection();
  34.  
  35. } catch (SQLException e1) {
  36. e1.printStackTrace();
  37. }
  38. // persons columns for update - NID, address, email, faxNumber,
  39. // firstName,homePhone, lastName, middleName, mobilePhone, driverLicense
  40. // appuser columns for update - firstName, lastName
  41. try {
  42. statement = (Statement) conn.createStatement();
  43.  
  44. // resultSet = statement.executeQuery("select id, firstName, lastName from
  45. // appuser where encrypted = 0");
  46.  
  47. resultSet = statement.executeQuery(
  48. "select id,NID, address, email, faxNumber, firstName,homePhone, lastName, middleName, mobilePhone, driverLicense,encrypted from persons where encrypted=0");
  49.  
  50. while (resultSet.next()) {
  51.  
  52. byte[] key = new byte[] { 113, 35, 83, -69, -115, 53, -75, -2, -4, -111, 29, -62, 77, -63, -10, 5 };
  53.  
  54. // appuser
  55. // String f = resultSet.getString("firstName");
  56. // String l = resultSet.getString("lastName");
  57. // String id = resultSet.getString("id");
  58. // try {
  59. // if (f != null) {
  60. // f = encrypt(f, key, "Z0roCryptNKTechY");
  61. // }
  62. //
  63. // if (l != null) {
  64. // l = encrypt(l, key, "Z0roCryptNKTechY");
  65. // }
  66. //
  67. // String UPDATE_SQL = "UPDATE appuser SET firstName = ?,lastName = ? where
  68. // id=?";
  69. //
  70. // PreparedStatement ps = conn.prepareStatement(UPDATE_SQL,
  71. // Statement.RETURN_GENERATED_KEYS);
  72. // ps.setString(1, f);
  73. // ps.setString(2, l);
  74. // ps.setString(3, id);
  75. // ps.executeUpdate();
  76. //
  77. // ResultSet rs = ps.getGeneratedKeys();
  78. //
  79. // ps.close();
  80.  
  81. // persons
  82. String a = resultSet.getString("NID");
  83. String b = resultSet.getString("address");
  84. String c = resultSet.getString("email");
  85. String d = resultSet.getString("faxNumber");
  86. String f = resultSet.getString("firstName");
  87. String e = resultSet.getString("homePhone");
  88. String l = resultSet.getString("lastName");
  89. String g = resultSet.getString("middleName");
  90. String h = resultSet.getString("mobilePhone");
  91. String i = resultSet.getString("driverLicense");
  92. String id = resultSet.getString("id");
  93. try {
  94. if (a != null) {
  95. a = encrypt(a, key, "Z0roCryptNKTechY");
  96. }
  97. if (b != null) {
  98. b = encrypt(b, key, "Z0roCryptNKTechY");
  99. }
  100. if (c != null) {
  101. c = encrypt(c, key, "Z0roCryptNKTechY");
  102. }
  103. if (d != null) {
  104. d = encrypt(d, key, "Z0roCryptNKTechY");
  105. }
  106. if (f != null) {
  107. f = encrypt(f, key, "Z0roCryptNKTechY");
  108. }
  109. if (e != null) {
  110. e = encrypt(e, key, "Z0roCryptNKTechY");
  111. }
  112. if (l != null) {
  113. l = encrypt(l, key, "Z0roCryptNKTechY");
  114. }
  115. if (g != null) {
  116. g = encrypt(g, key, "Z0roCryptNKTechY");
  117. }
  118. if (h != null) {
  119. h = encrypt(h, key, "Z0roCryptNKTechY");
  120. }
  121. if (i != null) {
  122. i = encrypt(i, key, "Z0roCryptNKTechY");
  123. }
  124. String UPDATE_SQL = "UPDATE persons SET NID = ?, address = ?, email = ?, faxNumber = ?, firstName = ?,homePhone = ?, lastName = ?, middleName = ?, mobilePhone = ?, driverLicense = ?,encrypted=? where id=? AND encrypted = 0";
  125.  
  126. PreparedStatement ps = conn.prepareStatement(UPDATE_SQL, Statement.RETURN_GENERATED_KEYS);
  127. ps.setString(1, a);
  128. ps.setString(2, b);
  129. ps.setString(3, c);
  130. ps.setString(4, d);
  131. ps.setString(5, f);
  132. ps.setString(6, e);
  133. ps.setString(7, l);
  134. ps.setString(8, g);
  135. ps.setString(9, h);
  136. ps.setString(10, i);
  137. ps.setString(11, "1");
  138. ps.setString(12, id);
  139. ps.executeUpdate();
  140.  
  141. // ResultSet rs = ps.getGeneratedKeys();
  142.  
  143. ps.close();
  144. // resultSet.updateString( "firstName", encrypt(f, key, "Z0roCryptNKTechY"));
  145. // resultSet.updateRow();
  146. } catch (Exception q) {
  147. // TODO Auto-generated catch block
  148. q.printStackTrace();
  149. }
  150.  
  151. }
  152. } catch (
  153.  
  154. SQLException e) {
  155. // TODO Auto-generated catch block
  156. e.printStackTrace();
  157. }
  158.  
  159. }
  160.  
  161. public static String encrypt(String strClearText, byte[] key, String strKey) throws Exception {
  162. String strData = "";
  163.  
  164. try {
  165. IvParameterSpec iv = new IvParameterSpec(strKey.getBytes("UTF-8"));
  166. SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  167.  
  168. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  169. cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
  170.  
  171. byte[] encrypted = cipher.doFinal(strClearText.getBytes());
  172.  
  173. strData = DatatypeConverter.printBase64Binary(encrypted);
  174.  
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. throw new Exception(e);
  178. }
  179. return strData;
  180. }
  181. //decrypt method
  182. // public static String decrypt(String strEncrypted, byte[] key, String strKey) throws Exception {
  183. // String strData = "";
  184. //
  185. // try {
  186. //
  187. // IvParameterSpec iv = new IvParameterSpec(strKey.getBytes("UTF-8"));
  188. // SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  189. //
  190. // Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
  191. // cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
  192. //
  193. // byte[] original = cipher.doFinal(DatatypeConverter.parseBase64Binary(strEncrypted));
  194. //
  195. // strData = new String(original);
  196. //
  197. // } catch (Exception e) {
  198. // e.printStackTrace();
  199. // throw new Exception(e);
  200. // }
  201. // return strData;
  202. // }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement