import
java.security.NoSuchAlgorithmException;
import
java.sql.Connection;
import
java.sql.DriverManager;
import
java.sql.PreparedStatement;
import
java.sql.ResultSet;
import
java.sql.SQLException;
import
java.sql.Statement;
import
java.util.logging.Level;
import
java.util.logging.Logger;
import
javax.crypto.Cipher;
import
javax.crypto.KeyGenerator;
import
javax.crypto.SecretKey;
import
javax.crypto.spec.SecretKeySpec;
public
class
EncryptAndStorePassword {
Connection conn =
null
;
public
static
final
String AES =
"AES"
;
String key =
null
;
public
Connection getConnection()
{
try
{
Class.forName(
"com.mysql.cj.jdbc.Driver"
)
.newInstance();
conn = DriverManager.getConnection(
"root"
,
"admin"
);
Statement st;
st = conn.createStatement();
ResultSet rs = st.executeQuery(
"select * from login_key"
);
while
(rs.next()) {
key = rs.getString(
"keyValue"
);
}
System.out.println(
"key="
+ key);
}
catch
(ClassNotFoundException ex) {
Logger
.getLogger(
EncryptAndStorePassword.
class
.getName())
.log(Level.SEVERE,
null
, ex);
}
catch
(SQLException ex) {
System.out.println(
"Exception.."
+ ex.getMessage());
}
catch
(InstantiationException e) {
e.printStackTrace();
}
catch
(IllegalAccessException e) {
e.printStackTrace();
}
return
conn;
}
private
void
closeConnection()
{
try
{
conn.close();
conn =
null
;
}
catch
(SQLException e) {
e.printStackTrace();
}
}
private
static
String byteArrayToHexString(
byte
[] b)
{
StringBuffer sb =
new
StringBuffer(b.length *
2
);
for
(
int
i =
0
; i < b.length; i++) {
int
v = b[i] &
0xff
;
if
(v <
16
) {
sb.append(
'0'
);
}
sb.append(Integer.toHexString(v));
}
return
sb.toString().toUpperCase();
}
private
static
byte
[] hexStringToByteArray(String s)
{
byte
[] b =
new
byte
[s.length() /
2
];
for
(
int
i =
0
; i < b.length; i++) {
int
index = i *
2
;
int
v = Integer.parseInt(
s.substring(index, index +
2
),
16
);
b[i] = (
byte
)v;
}
return
b;
}
public
EncryptAndStorePassword(String tableName)
{
getConnection();
doSelectAndUpdate(tableName);
closeConnection();
}
private
void
doSelectAndUpdate(String tableName)
{
doSelect(
"GEEKPORTALLOGIN"
);
}
private
void
getAndStoreLoginKey(String key)
{
try
{
String sql
=
"INSERT INTO login_key(keyValue) VALUES(?)"
;
PreparedStatement pst
= conn.prepareStatement(sql);
pst.setString(
1
, key);
pst.executeUpdate();
}
catch
(SQLException ex) {
System.out.println(
"SQLException.."
+ ex.getMessage());
}
}
private
void
oneTimeKeyGeneration()
{
try
{
KeyGenerator keyGen
= KeyGenerator.getInstance(AES);
keyGen.init(
128
);
SecretKey sk = keyGen.generateKey();
String key
= byteArrayToHexString(sk.getEncoded());
System.out.println(
"key:"
+ key);
getAndStoreLoginKey(key);
}
catch
(Exception ex) {
System.out.println(ex.getMessage());
}
}
private
void
doSelect(String tableName)
{
Statement st;
String query =
null
, password =
null
,
userName =
null
;
query =
"SELECT * FROM GEEKPORTALLOGIN"
;
try
{
st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
while
(rs.next()) {
userName = rs.getString(
"loginName"
);
password = rs.getString(
"password"
);
byte
[] passwordByte
= hexStringToByteArray(key);
System.out.println(
"keyValue.."
+ key);
SecretKeySpec sks =
new
SecretKeySpec(
passwordByte,
EncryptAndStorePassword.AES);
Cipher cipher = Cipher.getInstance(
EncryptAndStorePassword.AES);
cipher.init(Cipher.ENCRYPT_MODE, sks,
cipher.getParameters());
byte
[] encrypted
= cipher.doFinal(password.getBytes());
String encryptedpwd
= byteArrayToHexString(encrypted);
System.out.println(encryptedpwd);
doUpdate(encryptedpwd, userName,
"GEEKPORTALLOGIN"
);
}
}
catch
(SQLException ex) {
Logger
.getLogger(
EncryptAndStorePassword.
class
.getName())
.log(Level.SEVERE,
null
, ex);
}
catch
(Exception ex) {
System.err.println(ex.getMessage());
}
}
private
void
doUpdate(String password, String userName,
String tableName)
{
System.out.print(
"\n[Performing UPDATE] ... "
);
try
{
Statement st = conn.createStatement();
String sqlUpdate =
null
;
sqlUpdate =
"UPDATE GEEKPORTALLOGIN "
+
"SET password = ? "
+
"WHERE loginName = ?"
;
PreparedStatement pstmt
= conn.prepareStatement(sqlUpdate);
pstmt.setString(
1
, password);
pstmt.setString(
2
, userName);
int
rowAffected = pstmt.executeUpdate();
System.out.println(String.format(
"Row affected %d"
, rowAffected));
}
catch
(SQLException ex) {
System.err.println(ex.getMessage());
}
}
public
static
void
main(String args[])
throws
NoSuchAlgorithmException
{
EncryptAndStorePassword encryptAndStorePassword
=
null
;
try
{
encryptAndStorePassword
=
new
EncryptAndStorePassword(
"GEEKPORTALLOGIN"
);
}
finally
{
}
}
}