Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 求高手帮忙看一下,关于数据库加密的问题
爱猫的青蛙





发贴: 18
积分: 0
于 2006-06-09 09:22 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list

import java.io.*;
import java.net.*;
import java.util.*;
import java.net.URL;
import java.sql.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DataBaseManager
{
Connection con;
ResultSet rs;
Statement stmt;

//String search_str=subString(s);

/*为数据库管理器的构造函数,进行数据库链接*/
public DataBaseManager(String l,String user,String password)
{

try
{
try
{
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );/*加载指定的驱动程序*/
}
catch(java.lang.ClassNotFoundException e)
{
System.err.print("ClassNOtFoundException:");
System.err.println(e.getMessage());
System.err.println("Failed to load JDBC/ODBC driver." );
}
con=DriverManager.getConnection(l,user,password);
DatabaseMetaData dma=con.getMetaData();
System.out.println("连接的数据库:"+dma.getURL());
System.out.println("Driver :"+dma.getDriverName());
System.out.println("Version :"+dma.getDriverVersion());
stmt=con.createStatement();
}

catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("数据库异常被捕获了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
}
}

}



/*查找函数*/
public String querySql(String buf,String table,String sbuf,String s)
{
Des des=new Des("12345678");
System.out.println("加密前的字符串"+s);
String search_str=des.enc(s,s.length());
byte[] tempstr=search_str.getBytes();
String q_Str=byte2hex(tempstr);
System.out.println("加密后的二进制字符串"+q_Str);
String s_buf=sbuf;
String tab=table;
String strSQL="select * from "+tab+" where "+s_buf+"="+"\'"+q_Str+"\'";
//String temp="%"+search_str+"%";
//String strSQL="select * from titles where pub_id like '"+temp+"'";
System.out.println(strSQL);
try
{
rs=stmt.executeQuery(strSQL);
rs.next();
String tempre=rs.getString(buf).trim();
System.out.println("找到的二进制串"+tempre+"iddd");
byte[] re=hex2byte(tempre);
String re1=new String(re);
System.out.println(re1);
String result=des.dec(re1,re1.length());
System.out.println("解密后的字符串"+result);
return result;
//System.out.println("This is content "+rs.getString(buf));
}
catch(SQLException ex)
{

while(ex!=null)
{
System.out.println("数据库异常被捕获了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
System.out.println("未找到指定记录");
}
return null;
}

}

public boolean updateSql(String table,String buf,String s,String ubuf,String us)
{
Des des=new Des("12345678");
System.out.println("加密前要查找的字符串"+s);
System.out.println("加密前要写入的字符串"+us);
String search_str=des.enc(s,s.length());
String search_ustr=des.enc(us,us.length());
System.out.println("wu的加密字符"+search_ustr);
System.out.println("wu的加密字符"+des.dec(search_ustr,search_ustr.length()));
byte[] tempstr=search_str.getBytes();
byte[] tempustr=search_ustr.getBytes();
String q_Str=byte2hex(tempstr);
System.out.println("加密后的要查找的二进制字符串"+q_Str);
String u_Str=byte2hex(tempustr);
System.out.println("加密后的要写入的二进制字符串"+u_Str+"i am is girl");
String tab=table;
String temp=search_str;
String strSQL="update "+tab+" set "+ubuf+"="+"\'"+u_Str+"\'"+" where "+buf+"="+"\'"+q_Str+"\'";
//String strSQL="update "+tab+" set "+ubuf+"="+"\'"+us+"\'"+" where "+buf+"="+"\'"+temp+"\'";
System.out.println(strSQL);
try{
stmt.executeUpdate(strSQL);
con.commit();
System.out.println("写入成功");
return true;

}
catch(SQLException sqle)
{
System.out.println(sqle.toString());
return false;
}

}
// public Vector additems

public boolean insertSql(String table,String buf,String s)
{
Des des=new Des("12345678");
System.out.println("加密前的二进制串"+s);
String write_Str=des.enc(s,s.length());
byte[] str=write_Str.getBytes();
String w_Str=byte2hex(str);
System.out.println("加密后的二进制串"+w_Str);
String tab=table;
String write_buf=buf;
String strSQL="INSERT INTO "+tab+" ("+write_buf+") "+" values "+" ("+"\'"+w_Str+"\'"+")";
//System.out.println(strSQL);
try
{
stmt.executeUpdate(strSQL);
System.out.println("新增索引项成功");
return true;
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("数据库异常被捕获了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
//System.out.println(ex.toString());
}

return false;
}

}


/**
* 二进制转字符串。
*
* @param b
* @return
*/
private String byte2hex(byte[] b)
{
String hs = "";
String stmp = "";
for (int n = 0; n < b.length; n++)
{
stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
if (n < b.length - 1)
hs = hs + ":";
}
return hs.toUpperCase();
}

/**
* 字符串转二进制。
*
* @param hex
* @return
*/
private byte[] hex2byte(String hex)
{
String[] h = hex.split(":");
byte[] b = new byte[h.length];
for (int i = 0; i < h.length; i++)
{
int byteint = Integer.parseInt(h[i], 16) & 0xFF;
b[i] = new Integer(byteint).byteValue();
}
return b;
}

/**
* @return Returns the algorithm.
*/

/**
* @param algorithm
* The algorithm to set.

/*断开与数据库的链接*/
public void closeConnection()
{
try
{
con.close();
System.out.println("已退出数据库");
}
catch(SQLException ex)
{
while(ex!=null)
{
System.out.println("数据库异常被捕获了");
System.out.println(ex.getSQLState());
System.out.println(ex.getMessage());
System.out.println(ex.getErrorCode());
ex=ex.getNextException();
}

}
}


public static void main(String args[])
{
String l="jdbc:odbc:fangwei";
String s="20";
DataBaseManager db=new DataBaseManager(l,"sa","kelly");
String buf="Product_sn";
boolean x=db.insertSql("server1","Product_sn","20");
boolean y=db.updateSql("server1","Product_sn","20","Product_area","wu");
String a=db.querySql("Product_area","server1","Product_sn",s);

System.out.println("找到的"+a);
//boolean y=db.updateSql("server","Product_sn","?abcd","Product_area","wu");
//a=db.querySql(buf,"server","Product_sn",s);

// System.out.println(a);
db.closeConnection();


}

}

这个程序运行时
在写入前加解密是正常的
des.enc()为加密
des.dec()为解密
Des的类在附件中,大家可以试运行一下
但是为什么存入后再取出解密就出问题了

Des.class (12.25k)


爱猫的青蛙 edited on 2006-06-09 13:28

作者 Re:求高手帮忙看一下,关于数据库加密的问题 [Re:爱猫的青蛙]
爱猫的青蛙





发贴: 18
积分: 0
于 2006-06-09 13:30 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
另外一个用到的类

求大家棒棒忙

我实在是不知道怎么回事了

是编码转换的问题么?

SubKey.java (5.71k)



作者 Re:求高手帮忙看一下,关于数据库加密的问题 [Re:爱猫的青蛙]
爱猫的青蛙





发贴: 18
积分: 0
于 2006-06-09 13:32 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
程序的源代码

DataBaseManager.java (7.65k)




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923