1、获取固定秘钥:8位的
/**
* 固定key
*
* @return
*/
public Key getKey() {
Key deskey = null;
try {
// 固定密钥
byte[] buffer = new byte[] { 0x47, 0x33, 0x43, 0x4D, 0x4F, 0x50,0x31, 0x32 };
deskey = (Key) new SecretKeySpec(buffer, "DES");
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
}
return deskey;
}
2、加密方法:
/**
* 加密01
* @param src
* @param deskey
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public byte[] Encrytor(byte[] src, Key deskey)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException {
Cipher c = Cipher.getInstance("DES/ECB/NoPadding");
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, deskey);
byte[] doFinal = c.doFinal(src);
return doFinal;
}
/**
* 加密方法02
* @param is
* @param deskey
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
*/
public CipherInputStream Encrytor02(InputStream is, Key deskey)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException {
Cipher c = Cipher.getInstance("DES/ECB/NoPadding");
// 根据密钥,对Cipher对象进行初始化,ENCRYPT_MODE表示加密模式
c.init(Cipher.ENCRYPT_MODE, deskey);
CipherInputStream cis = new CipherInputStream(is, c);
return cis;
}
3、解密方法
/**
* 对字符串解密
*
* @param buff
* @return
* @throws InvalidKeyException
* @throws IllegalBlockSizeException
* @throws BadPaddingException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
*/
public byte[] Decryptor(byte[] buff, Key deskey)
throws InvalidKeyException, IllegalBlockSizeException,
BadPaddingException, NoSuchAlgorithmException,
NoSuchPaddingException {
Cipher c = Cipher.getInstance("DES/ECB/NoPadding");
// 根据密钥,对Cipher对象进行初始化,DECRYPT_MODE表示加密模式
c.init(Cipher.DECRYPT_MODE, deskey);
byte[] doFinal = c.doFinal(buff);
return doFinal;
}
4、InputStream 转 byte[]
/**
* InputStream 转 byte[]
* @param iStrm
* @return
* @throws IOException
*/
public byte[] InputStreamToByte(InputStream iStrm) throws IOException {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
byte[] buf = new byte[1024];
while ((ch = iStrm.read()) != -1)
{
bytestream.write(buf, 0, ch);
}
byte imgdata[]=bytestream.toByteArray();
bytestream.close();
return imgdata;
}
5、加密文件导入数据库
EncrypDes encrypDes = new EncrypDes();
Key key = encrypDes.getKey();
System.out.println(key);
long sqlstart = System.currentTimeMillis();// 开始时间
File file = new File("D:/Icon.txt");
FileInputStream in = new FileInputStream(file);
CipherInputStream cin = encrypDes.Encrytor02(in, key);
Connection con = null;// 创建一个数据库连接
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
ResultSet result = null;// 创建一个结果集对象
// /*
try {
con = JdbcOracleUtil.getConn02("");
System.out.println(con != null ? "成功" : "失败" + "连接成功!");
String sql = "Insert INTO FI_FILE (ID,FILECONTENT) values ( FIFILE_SEQ.nextVal ,? ) ";
pre = con.prepareStatement(sql);
ByteArrayOutputStream bAOutputStream = new ByteArrayOutputStream();
int ch;
while ((ch = cin.read()) != -1) {
bAOutputStream.write(ch);
}
byte data[] = bAOutputStream.toByteArray();
System.out.println(data);
bAOutputStream.close();
pre.setBytes(1, data);
// 4.执行语句
int i = pre.executeUpdate();
System.out.println(i);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭!");
} catch (Exception e) {
e.printStackTrace();
}
}
long sqlend = System.currentTimeMillis(); // 结束时间
System.out.println("上传文件用时:" + (sqlend - sqlstart) / 1000 + " s");
System.out.println("上传文件用时:" + (sqlend - sqlstart) + " ms ");
6、解密文件展示
EncrypDes encrypDes = new EncrypDes();
Key key = encrypDes.getKey();
System.out.println(key);
long sqlstart = System.currentTimeMillis();// 开始时间
File file = new File("D:/Icon.txt");
FileInputStream in = new FileInputStream(file);
CipherInputStream cin = encrypDes.Encrytor02(in, key);
Connection con = null;// 创建一个数据库连接
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
ResultSet result = null;// 创建一个结果集对象
try {
con = JdbcOracleUtil.getConn02("");
System.out.println(con != null ? "成功" : "失败" + "连接成功!");
String sql = "select * from Fi_File where ID=16";
// String sql="select * from Fi_Fileinfo where fileid=97 ";
pre = con.prepareStatement(sql);
// 4.执行语句
ResultSet reset = pre.executeQuery();
byte[] bis = null;
while (reset.next()) {
bis = reset.getBytes("FILECONTENT");
}
byte[] doFinal = encrypDes.Decryptor(bis, key);
// Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
// cipher.init(Cipher.DECRYPT_MODE, key);
// byte[] doFinal = cipher.doFinal(bis);
System.out.println(doFinal);
System.out.println(new String(doFinal, "GB2312"));
} catch (Exception e) {
System.out.println(e);
} finally {
try {
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
// 注意关闭的顺序,最后使用的最先关闭
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
System.out.println("数据库连接已关闭!");
} catch (Exception e) {
e.printStackTrace();
}
}
long sqlend02 = System.currentTimeMillis(); // 结束时间
System.out.println("上传文件用时:" + (sqlend02 - sqlstart) / 1000 + " s");
System.out.println("上传文件用时:" + (sqlend02 - sqlstart) + " ms ");