package utils;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public final class ConnectionSafer {
private static int length = 16;
/** 指定默认存放路径 ,默认为C:ConnectionSafer */
private static String defaultPath = "C:ConnectionSafer";
/** 指定默认存放路径 ,默认为C:ConnectionSafer */
private static String namespace = "";
/** 指定用户名存放文件 名 */
private static String USERNAME = "UESRNAME";
/** 指定密码存放文件 名 */
private static String PASSWORD = "PASSWORD";
/** 指定密码存放文件 名 */
private static String Certificate = "Certificate";
private static String KEY = "KEY";
private static String key;
private static Map<String, byte[]> userCertificate = new HashMap<String, byte[]>();
private static File Certificate_FILE;
private static Random randGen = null;
private static char[] numberAndLetters = null;
/** 储存密钥对的变量 */
private static String Key_Store;
/** 储存解密的用户名的变量 */
private static byte[] Username_Decrypt_Store;
/** 储存解密的密码的变量 */
private static byte[] Password_Decrypt_Store;
private static ConnectionSafer INSTANCE;
private static String generateKey(int length) {
if (length < 1) {
return null;
}
if (randGen == null) {
randGen = new Random();
numberAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyz"
+ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
}
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = numberAndLetters[randGen.nextInt(71)];
}
return new String(randBuffer);
}
/**
*
* 存储加密的用户名密码到本地(默认为C:ConnectionSafer)
*
* @throws Exception
*/
private static void saveCertificate() throws Exception {
if (!Certificate_FILE.getParentFile().exists()) {
Certificate_FILE.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(new File(
Certificate_FILE.getPath()));
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(userCertificate);
fos.flush();
fos.close();
System.out.println("Certificate_FILE IS SAVED AT : "
+ Certificate_FILE.getPath());
}
/**
*
* 读取本地(默认为C:ConnectionSafer)的加密的密码(默认为C:ConnectionSafer)
*
* @throws Exception
*/
@SuppressWarnings("unchecked")
private static void loadCertificate() throws Exception {
FileInputStream fis = new FileInputStream(Certificate_FILE.getPath());
ObjectInputStream ois = new ObjectInputStream(fis);
userCertificate = (Map<String, byte[]>) ois.readObject();
fis.close();
ois.close();
System.out.println("Certificate_FILE IS LOADED FROM: "
+ Certificate_FILE.getPath());
}
private static boolean isinitiated() {
if (INSTANCE == null) {
Builder.builder.build();
}
return Certificate_FILE.exists();
}
private ConnectionSafer() {
defaultPath = Builder.path;
namespace = Builder.namespace;
Certificate_FILE = new File(new File(defaultPath), Certificate + "_"
+ namespace);
}
/**
*
* 删除本地(默认为C:ConnectionSafer)的加密的用户名密码(默认为C:ConnectionSafer)
*
*
*/
public static void removeCertificate() {
if (isinitiated()) {
Certificate_FILE.delete();
System.out.println("Certificate_FILE IS SUCCESSFUL DELETED FROM: "
+ Certificate_FILE.getPath());
} else {
System.out.println("Certificate_FILE IS FAIL DELETED FROM: "
+ Certificate_FILE.getPath());
}
}
/**
*
* 初始化用户名密码,产生密钥对并储存到本地(默认为C:ConnectionSafer),然后加密数据储存到本地(默认为C:
* ConnectionSafer )
*
* @throws Exception
*/
public static void initConnection() throws Exception {
isinitiated();
// 生成公钥及私钥
Key_Store = generateKey(length);
System.out.println("Input your username:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Username_Decrypt_Store = br.readLine().getBytes();
System.out.println("Input your password:");
Password_Decrypt_Store = br.readLine().getBytes();
System.out.println("Input to set your Certificate Passworld:");
key = br.readLine();
if (!key.trim().equals("")) {
key = key.trim();
userCertificate.put(KEY, Crypt.encrypt(Key_Store.getBytes(), key));
} else {
userCertificate.put(KEY, Key_Store.getBytes());
}
userCertificate.put(USERNAME,
Crypt.encrypt(Username_Decrypt_Store, Key_Store));
userCertificate.put(PASSWORD,
Crypt.encrypt(Password_Decrypt_Store, Key_Store));
saveCertificate();
}
public static Connection getConnection(String Driver, String Url)
throws Exception {
if (!isinitiated()) {
initConnection();
} else if (key==null||Key_Store==null) {
loadCertificate();
Class.forName(Driver);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Input your Certificate Pass:");
key = br.readLine();
}
if (!key.trim().equals("")) {
key = key.trim();
Key_Store = new String(Crypt.decrypt(userCertificate.get(KEY), key));
} else {
Key_Store = new String(userCertificate.get(KEY));
}
String usr = new String(Crypt.decrypt(userCertificate.get(USERNAME),
Key_Store)).trim();
String pwd = new String(Crypt.decrypt(userCertificate.get(PASSWORD),
Key_Store)).trim();
Connection con = DriverManager.getConnection(Url, usr, pwd);
return con;
}
public static enum Builder {
builder;
private static String path = "C:ConnectionSafer";
private static String namespace = "defaultname";
public Builder setPath(String path) {
if (path != null && !path.trim().equals("")) {
Builder.path = path;
}
build();
return this;
}
public Builder setNamespace(String namespace) {
if (namespace != null && !namespace.trim().equals("")) {
Builder.namespace = namespace;
}
build();
return this;
}
public void build(){
INSTANCE = new ConnectionSafer();
}
}
private static class Crypt {
private static final String CHARSET = "UTF-8";
public static byte[] decrypt(byte[] data, String key)
throws IOException {
return new AES().decrypt(data, key.getBytes(CHARSET));
}
public static byte[] encrypt(byte[] data, String key)
throws IOException {
return new AES().encrypt(data, key.getBytes(CHARSET));
}
}
private static class AES {
private final static int[] sbox = { 0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B,
0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4,
0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0, 0xB7, 0xFD, 0x93, 0x26,
0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1, 0x71, 0xD8,
0x31, 0x15, 0x04, 0xC7, 0x23, 0xC3, 0x18, 0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 0xF1, 0x1A, 0x71, 0x1D,
0x29, 0xC5, 0x89, 0x6F, 0xB7, 0x62, 0x0E, 0xAA, 0x18, 0xBE,
0x1B, 0xFC, 0x56, 0x3E, 0x4B, 0xC6, 0xD2, 0x79, 0x20, 0x9A,
0xDB, 0xC0, 0xFE, 0x78, 0xCD, 0x5A, 0xF4, 0x1F, 0xDD, 0xA8,
0x33, 0x88, 0x07, 0xC7, 0x31, 0xB1, 0x12, 0x10, 0x59, 0x27,
0x80, 0xEC, 0x5F, 0x60, 0x51, 0x7F, 0xA9, 0x19, 0xB5, 0x4A,
0x0D, 0x2D, 0xE5, 0x7A, 0x9F, 0x93, 0xC9, 0x9C, 0xEF, 0xA0,
0xE0, 0x3B, 0x4D, 0xAE, 0x2A, 0xF5, 0xB0, 0xC8, 0xEB, 0xBB,
0x3C, 0x83, 0x53, 0x99, 0x61, 0x17, 0x2B, 0x04, 0x7E, 0xBA,
0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C,
0x7D };
private final static int[] LogTable = { 0, 0, 25, 1, 50, 2, 26, 198,
75, 199, 27, 104, 51, 238, 223, 3, 100, 4, 224, 14, 52, 141,
129, 239, 76, 113, 8, 200, 248, 105, 28, 193, 125, 194, 29,
181, 249, 185, 39, 106, 77, 228, 166, 114, 154, 201, 9, 120,
101, 47, 138, 5, 33, 15, 225, 36, 18, 240, 130, 69, 53, 147,
218, 142, 150, 143, 219, 189, 54, 208, 206, 148, 19, 92, 210,
241, 64, 70, 131, 56, 102, 221, 253, 48, 191, 6, 139, 98, 179,
37, 226, 152, 34, 136, 145, 16, 126, 110, 72, 195, 163, 182,
30, 66, 58, 107, 40, 84, 250, 133, 61, 186, 43, 121, 10, 21,
155, 159, 94, 202, 78, 212, 172, 229, 243, 115, 167, 87, 175,
88, 168, 80, 244, 234, 214, 116, 79, 174, 233, 213, 231, 230,
173, 232, 44, 215, 117, 122, 235, 22, 11, 245, 89, 203, 95,
176, 156, 169, 81, 160, 127, 12, 246, 111, 23, 196, 73, 236,
216, 67, 31, 45, 164, 118, 123, 183, 204, 187, 62, 90, 251, 96,
177, 134, 59, 82, 161, 108, 170, 85, 41, 157, 151, 178, 135,
144, 97, 190, 220, 252, 188, 149, 207, 205, 55, 63, 91, 209,
83, 57, 132, 60, 65, 162, 109, 71, 20, 42, 158, 93, 86, 242,
211, 171, 68, 17, 146, 217, 35, 32, 46, 137, 180, 124, 184, 38,
119, 153, 227, 165, 103, 74, 237, 222, 197, 49, 254, 24, 13,
99, 140, 128, 192, 247, 112, 7 };
private final static int[] AlogTable = { 1, 3, 5, 15, 17, 51, 85, 255,
26, 46, 114, 150, 161, 248, 19, 53, 95, 225, 56, 72, 216, 115,
149, 164, 247, 2, 6, 10, 30, 34, 102, 170, 229, 52, 92, 228,
55, 89, 235, 38, 106, 190, 217, 112, 144, 171, 230, 49, 83,
245, 4, 12, 20, 60, 68, 204, 79, 209, 104, 184, 211, 110, 178,
205, 76, 212, 103, 169, 224, 59, 77, 215, 98, 166, 241, 8, 24,
40, 120, 136, 131, 158, 185, 208, 107, 189, 220, 127, 129, 152,
179, 206, 73, 219, 118, 154, 181, 196, 87, 249, 16, 48, 80,
240, 11, 29, 39, 105, 187, 214, 97, 163, 254, 25, 43, 125, 135,
146, 173, 236, 47, 113, 147, 174, 233, 32, 96, 160, 251, 22,
58, 78, 210, 109, 183, 194, 93, 231, 50, 86, 250, 21, 63, 65,
195, 94, 226, 61, 71, 201, 64, 192, 91, 237, 44, 116, 156, 191,
218, 117, 159, 186, 213, 100, 172, 239, 42, 126, 130, 157, 188,
223, 122, 142, 137, 128, 155, 182, 193, 88, 232, 35, 101, 175,
234, 37, 111, 177, 200, 67, 197, 84, 252, 31, 33, 99, 165, 244,
7, 9, 27, 45, 119, 153, 176, 203, 70, 202, 69, 207, 74, 222,
121, 139, 134, 145, 168, 227, 62, 66, 198, 81, 243, 14, 18, 54,
90, 238, 41, 123, 141, 140, 143, 138, 133, 148, 167, 242, 13,
23, 57, 75, 221, 124, 132, 151, 162, 253, 28, 36, 108, 180,
199, 82, 246, 1 };
private final static int[][] rcon = {
{ 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } };
private final static int NUMROUNDS = 10;
private byte[][] state;
private byte[][] keys;
private void setKey(byte[] key) {
for (int i = 0; i < keys.length; i++) {
for (int j = 0; j < keys[0].length / (NUMROUNDS + 1); j++) {
if (i * keys.length + j < key.length) {
keys[j][i] = key[i * keys.length + j];
} else {
break;
}
}
}
int rconIndex = 0;
for (int i = 4; i < 4 * (NUMROUNDS + 1); i++) {
byte[] col = getColumn(keys, i - 1);
if (i % 4 == 0) {
col = rotateWord(col);
col = subBytesKey(col);
col = xorBytes(col, getColumn(keys, i - 4));
col = xorRcon(col, rconIndex);
rconIndex++;
} else {
col = xorBytes(col, getColumn(keys, i - 4));
}
columnCopy(keys, i, col);
}
}
private byte[] getColumn(byte[][] mat, int pos) {
byte[] result = new byte[mat.length];
for (int i = 0; i < 4; i++)
result[i] = mat[i][pos];
return result;
}
private void columnCopy(byte[][] mat, int pos, byte[] col) {
for (int i = 0; i < col.length; i++) {
mat[i][pos] = col[i];
}
}
private byte[] rotateWord(byte[] arr) {
byte[] result = new byte[arr.length];
for (int i = 0; i < arr.length - 1; i++)
result[i] = arr[i + 1];
result[result.length - 1] = arr[0];
return result;
}
private byte[] subBytesKey(byte[] arr) {
byte[] result = new byte[arr.length];
for (int i = 0; i < arr.length; i++) {
int j = arr[i];
result[i] = (byte) (sbox[j & 0x000000FF] & 0xFF);
}
return result;
}
private byte[] xorBytes(byte[] m, byte[] n) {
byte[] result = new byte[m.length];
for (int i = 0; i < m.length; i++)
result[i] = (byte) (m[i] ^ n[i]);
return result;
}
private byte[] xorRcon(byte[] arr, int index) {
arr[0] = (byte) (arr[0] ^ (rcon[0][index] & 0xff));
return arr;
}
private void subBytes() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
byte b = state[i][j];
state[i][j] = (byte) (sbox[b & 0x000000FF] & 0xFF);
}
}
}
private void shiftRows() {
byte[] row = new byte[4];
int shift = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
row[j] = state[i][j];
}
for (int j = 0; j < 4; j++) {
state[i][j] = row[(j + shift) % 4];
}
shift++;
}
}
private byte mul(int a, byte b) {
int inda = (a < 0) ? (a + 256) : a;
int indb = (b < 0) ? (b + 256) : b;
if ((a != 0) && (b != 0)) {
int index = (LogTable[inda] + LogTable[indb]);
byte val = (byte) (AlogTable[index % 255]);
return val;
} else
return 0;
}
private void mixColumn(int c) {
byte a[] = new byte[4];
for (int i = 0; i < 4; i++)
a[i] = state[i][c];
state[0][c] = (byte) (mul(2, a[0]) ^ a[2] ^ a[3] ^ mul(3, a[1]));
state[1][c] = (byte) (mul(2, a[1]) ^ a[3] ^ a[0] ^ mul(3, a[2]));
state[2][c] = (byte) (mul(2, a[2]) ^ a[0] ^ a[1] ^ mul(3, a[3]));
state[3][c] = (byte) (mul(2, a[3]) ^ a[1] ^ a[2] ^ mul(3, a[0]));
}
private void mixColumnInverse(int c) {
byte a[] = new byte[4];
for (int i = 0; i < 4; i++)
a[i] = state[i][c];
state[0][c] = (byte) (mul(0xE, a[0]) ^ mul(0xB, a[1])
^ mul(0xD, a[2]) ^ mul(0x9, a[3]));
state[1][c] = (byte) (mul(0xE, a[1]) ^ mul(0xB, a[2])
^ mul(0xD, a[3]) ^ mul(0x9, a[0]));
state[2][c] = (byte) (mul(0xE, a[2]) ^ mul(0xB, a[3])
^ mul(0xD, a[0]) ^ mul(0x9, a[1]));
state[3][c] = (byte) (mul(0xE, a[3]) ^ mul(0xB, a[0])
^ mul(0xD, a[1]) ^ mul(0x9, a[2]));
}
private void mixColumns() {
for (int i = 0; i < 4; i++)
mixColumn(i);
}
private void mixColumnsInverse() {
for (int i = 0; i < 4; i++)
mixColumnInverse(i);
}
private void addRoundKey(int currentRound) {
byte[] col;
int j = 0;
for (int i = 0; i < 4; i++) {
col = xorBytes(getColumn(state, i),
getColumn(keys, currentRound * 4 + j));
columnCopy(state, i, col);
j++;
}
}
private void subBytesInverse() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
byte b = state[i][j];
state[i][j] = (byte) (isbox[b & 0x000000FF] & 0xFF);
}
}
}
private void shiftRowsInverse() {
byte[] row = new byte[4];
int shift = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
row[(j + shift) % 4] = state[i][j];
}
for (int j = 0; j < 4; j++) {
state[i][j] = row[j];
}
shift++;
}
}
public AES() {
state = new byte[4][4];
keys = new byte[4][4 * (NUMROUNDS + 1)];
}
public byte[] encrypt(byte[] data, byte[] key) {
this.setKey(key);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int n = (int) Math.ceil(data.length / 16.0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
if (i * 16 + j * 4 + k < data.length) {
state[k][j] = data[i * 16 + j * 4 + k];
} else {
state[k][j] = (byte) 0;
}
}
}
addRoundKey(0);
for (int j = 1; j < NUMROUNDS + 1; j++) {
subBytes();
shiftRows();
if (j != NUMROUNDS)
mixColumns();
addRoundKey(j);
}
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
bout.write(state[k][j]);
}
}
}
return bout.toByteArray();
}
public byte[] decrypt(byte[] data, byte[] key) {
this.setKey(key);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int n = (int) Math.ceil(data.length / 16.0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
if (i * 16 + j * 4 + k < data.length) {
state[k][j] = data[i * 16 + j * 4 + k];
} else {
state[k][j] = (byte) 0;
}
}
}
addRoundKey(NUMROUNDS);
for (int j = NUMROUNDS - 1; j > -1; j--) {
subBytesInverse();
shiftRowsInverse();
addRoundKey(j);
if (j != 0) // last round has no mixColumns
mixColumnsInverse();
}
for (int j = 0; j < 4; j++) {
for (int k = 0; k < 4; k++) {
bout.write(state[k][j]);
}
}
}
return bout.toByteArray();
}
}
}
加密的算法是从httpdb这个开源项目提取出来的,其他代码块均为原创。
调用方法如下:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class test {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String path = "C:/Users/Administrator/Desktop/safeConnection";
// sl.removeCertificate();
String url = "jdbc:sqlserver://localhost:1433;DatabaseName=goods";//根据实际情况改
String sqlStr = "select * from t_book";//根据实际情况改
String SDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
try {
ConnectionSafer.Builder.builder.setPath(path);
ConnectionSafer.Builder.builder.setNamespace("jiami");
ConnectionSafer.removeCertificate();
ConnectionSafer.initConnection();
Connection con =ConnectionSafer.getConnection(SDriver, url);
Statement st = con.createStatement();
ResultSet rs = st.executeQuery(sqlStr);
// 输出所有查询结果
ResultSetMetaData rsmd = rs.getMetaData();
int col = rsmd.getColumnCount();
while (rs.next()) {
int i = 1;
String str = "";
while (i <= col) {
str = str + rs.getString(i) + "\t\t";
i++;
}
System.out.println(str);
}
rs.close();
st.close();
con.close();
} catch (Exception err) {
err.printStackTrace(System.out);
}
}
}