Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package encrypt.encrypt;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.crypto.Cipher;
- import javax.crypto.spec.IvParameterSpec;
- import javax.crypto.spec.SecretKeySpec;
- import javax.sql.DataSource;
- import javax.xml.bind.DatatypeConverter;
- import org.springframework.jdbc.datasource.SimpleDriverDataSource;
- import com.mysql.jdbc.Driver;
- import com.mysql.jdbc.Statement;
- public class App {
- public static void main(String[] args) {
- Connection conn = null;
- DataSource ds = null;
- ResultSet resultSet = null;
- Statement statement = null;
- try {
- Driver driver = new com.mysql.jdbc.Driver();
- String url = "jdbc:mysql://localhost:5555/nktrans_test_pst";
- String username = "test";
- String password = "test";
- ds = new SimpleDriverDataSource(driver, url, username, password);
- conn = ds.getConnection();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- // persons columns for update - NID, address, email, faxNumber,
- // firstName,homePhone, lastName, middleName, mobilePhone, driverLicense
- // appuser columns for update - firstName, lastName
- try {
- statement = (Statement) conn.createStatement();
- // resultSet = statement.executeQuery("select id, firstName, lastName from
- // appuser where encrypted = 0");
- resultSet = statement.executeQuery(
- "select id,NID, address, email, faxNumber, firstName,homePhone, lastName, middleName, mobilePhone, driverLicense,encrypted from persons where encrypted=0");
- while (resultSet.next()) {
- byte[] key = new byte[] { 113, 35, 83, -69, -115, 53, -75, -2, -4, -111, 29, -62, 77, -63, -10, 5 };
- // appuser
- // String f = resultSet.getString("firstName");
- // String l = resultSet.getString("lastName");
- // String id = resultSet.getString("id");
- // try {
- // if (f != null) {
- // f = encrypt(f, key, "Z0roCryptNKTechY");
- // }
- //
- // if (l != null) {
- // l = encrypt(l, key, "Z0roCryptNKTechY");
- // }
- //
- // String UPDATE_SQL = "UPDATE appuser SET firstName = ?,lastName = ? where
- // id=?";
- //
- // PreparedStatement ps = conn.prepareStatement(UPDATE_SQL,
- // Statement.RETURN_GENERATED_KEYS);
- // ps.setString(1, f);
- // ps.setString(2, l);
- // ps.setString(3, id);
- // ps.executeUpdate();
- //
- // ResultSet rs = ps.getGeneratedKeys();
- //
- // ps.close();
- // persons
- String a = resultSet.getString("NID");
- String b = resultSet.getString("address");
- String c = resultSet.getString("email");
- String d = resultSet.getString("faxNumber");
- String f = resultSet.getString("firstName");
- String e = resultSet.getString("homePhone");
- String l = resultSet.getString("lastName");
- String g = resultSet.getString("middleName");
- String h = resultSet.getString("mobilePhone");
- String i = resultSet.getString("driverLicense");
- String id = resultSet.getString("id");
- try {
- if (a != null) {
- a = encrypt(a, key, "Z0roCryptNKTechY");
- }
- if (b != null) {
- b = encrypt(b, key, "Z0roCryptNKTechY");
- }
- if (c != null) {
- c = encrypt(c, key, "Z0roCryptNKTechY");
- }
- if (d != null) {
- d = encrypt(d, key, "Z0roCryptNKTechY");
- }
- if (f != null) {
- f = encrypt(f, key, "Z0roCryptNKTechY");
- }
- if (e != null) {
- e = encrypt(e, key, "Z0roCryptNKTechY");
- }
- if (l != null) {
- l = encrypt(l, key, "Z0roCryptNKTechY");
- }
- if (g != null) {
- g = encrypt(g, key, "Z0roCryptNKTechY");
- }
- if (h != null) {
- h = encrypt(h, key, "Z0roCryptNKTechY");
- }
- if (i != null) {
- i = encrypt(i, key, "Z0roCryptNKTechY");
- }
- String UPDATE_SQL = "UPDATE persons SET NID = ?, address = ?, email = ?, faxNumber = ?, firstName = ?,homePhone = ?, lastName = ?, middleName = ?, mobilePhone = ?, driverLicense = ?,encrypted=? where id=? AND encrypted = 0";
- PreparedStatement ps = conn.prepareStatement(UPDATE_SQL, Statement.RETURN_GENERATED_KEYS);
- ps.setString(1, a);
- ps.setString(2, b);
- ps.setString(3, c);
- ps.setString(4, d);
- ps.setString(5, f);
- ps.setString(6, e);
- ps.setString(7, l);
- ps.setString(8, g);
- ps.setString(9, h);
- ps.setString(10, i);
- ps.setString(11, "1");
- ps.setString(12, id);
- ps.executeUpdate();
- // ResultSet rs = ps.getGeneratedKeys();
- ps.close();
- // resultSet.updateString( "firstName", encrypt(f, key, "Z0roCryptNKTechY"));
- // resultSet.updateRow();
- } catch (Exception q) {
- // TODO Auto-generated catch block
- q.printStackTrace();
- }
- }
- } catch (
- SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static String encrypt(String strClearText, byte[] key, String strKey) throws Exception {
- String strData = "";
- try {
- IvParameterSpec iv = new IvParameterSpec(strKey.getBytes("UTF-8"));
- SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
- Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
- cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
- byte[] encrypted = cipher.doFinal(strClearText.getBytes());
- strData = DatatypeConverter.printBase64Binary(encrypted);
- } catch (Exception e) {
- e.printStackTrace();
- throw new Exception(e);
- }
- return strData;
- }
- //decrypt method
- // public static String decrypt(String strEncrypted, byte[] key, String strKey) throws Exception {
- // String strData = "";
- //
- // try {
- //
- // IvParameterSpec iv = new IvParameterSpec(strKey.getBytes("UTF-8"));
- // SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
- //
- // Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
- // cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
- //
- // byte[] original = cipher.doFinal(DatatypeConverter.parseBase64Binary(strEncrypted));
- //
- // strData = new String(original);
- //
- // } catch (Exception e) {
- // e.printStackTrace();
- // throw new Exception(e);
- // }
- // return strData;
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement