当前位置:   article > 正文

简易数据库加密系统

数据库加密系统

总述

设计实现一个简易的数据库加密系统,实现数据库加密,数据库查询,密钥管理等模块。前期想法是准备写一个数据管理系统,实现对数据库的增删查改操作,并在其中使用密钥实现对数据库的加密。流程图如下,在进行增删查改等操作的过程中需要输入密钥来对数据经行加密和解密。本系统基于phpstudy,建立在本地上。

1,实现用户登陆

效果图

login.php代码:

  1. <?php
  2. include "./connect.php";
  3. //接收数据
  4. if(isset($_POST['userid']) && isset($_POST['password'])){
  5. //从数据库里查找用户名是否存在
  6. $_sql = "SELECT user_id,passwd FROM people WHERE user_id='{$_POST['userid']}'";
  7. $result = _fetch_array($_sql);
  8. if(!empty($result[0])){
  9. if($_POST['password']==$result[0]['passwd']){
  10. if($_POST['userid'] == "admin")
  11. {
  12. _location('','admin.php');
  13. }
  14. else{
  15. _location('','hello.php'); //示例网站
  16. }
  17. }else if($_POST['password']==''){
  18. _alert('密码为空,请输入密码');
  19. }
  20. else{
  21. _alert('密码错误');
  22. }
  23. }else if($_POST['userid']=='' && $_POST['password']==''){
  24. _alert('用户名和密码为空,请输入用户名和密码');
  25. }else if($_POST['userid']==''){
  26. _alert('用户名为空,请输入用户名');
  27. }else {
  28. _alert('用户名不存在');
  29. }
  30. _close();
  31. exit;
  32. }
  33. ?>
  34. <!DOCTYPE html>
  35. <head>
  36. <meta charset="UTF-8">
  37. <title>Login</title>
  38. <link rel="stylesheet" href="login.css" >
  39. </head>
  40. <body>
  41. <div id="main">
  42. <div id="avatar">
  43. </div>
  44. <form action="login.php" method="post">
  45. 用户名:<input type="text" name="userid" style="height: 40px;width: calc(100% - 10px);border: none;outline: none;padding: 0 5px;background: rgba(0,0,0,0.5);color: #ffcae5;font-size: 16px;">
  46. 密码:<input type="password" name="password" style="height: 40px;width: calc(100% - 10px);border: none;outline: none;padding: 0 5px;background: rgba(0,0,0,0.5);color: #ffcae5;font-size: 16px;">
  47. <input type="submit" value="提交" style="width: 75%;height: 40px;display: block;margin: 30px auto;background:#ffc5d1 ;border: none;outline: none;color: #fff;font-size: 16px;cursor: pointer;">
  48. </form>
  49. <div id="footer">
  50. <a href="#">Forget Password?</a>
  51. </div>
  52. </div>
  53. </body>

 login.css

  1. @charset "utf-8";
  2. /*统一设置*/
  3. *{
  4. margin: 0;
  5. padding: 0;
  6. }
  7. /*网页背景图片*/
  8. body{
  9. background: url("picture/background.jpg") no-repeat ;
  10. background-size: cover;
  11. }
  12. /*登陆区主体*/
  13. #main{
  14. width: 350px;
  15. height: 600px;
  16. background: rgba(0,0,0,0.5);
  17. margin: 40px auto;
  18. border-top: 8px solid #ffc5d100;
  19. position: relative;
  20. }
  21. /*头像区*/
  22. #avatar{
  23. width: 184px;
  24. height: 184px;
  25. background: url("picture/avatar.jpg") no-repeat;
  26. background-size: cover;
  27. margin: 50px auto;
  28. border-radius: 50%;
  29. }
  30. /*登陆区底部*/
  31. #footer{
  32. height: 50px;
  33. text-align: center;
  34. line-height: 50px;
  35. position: absolute;
  36. bottom: 0;
  37. width: 100%;
  38. border-top: 1px solid #ccc;
  39. }
  40. #footer a{
  41. color: #ccc;
  42. text-decoration: none;
  43. }
  44. #footer a:hover{
  45. color: red;
  46. }

connect.php(数据库连接代码)

  1. <?php
  2. $_conn=mysqli_connect('localhost','root','root'); //主机地址 用户名 密码 如果你的跟我一样就不用改
  3. if (!$_conn) {
  4. exit('数据库连接失败:'.mysqli_error($_conn));
  5. }
  6. mysqli_select_db($_conn,'user')or die('找不到数据库:'.mysqli_error($_conn).mysqli_errno($_conn));
  7. mysqli_query($_conn,"SET NAMES UTF8");
  8. // var_dump($_conn);
  9. include "sql.func.php";
  10. ?>

 sql.func.php (底层封装文件 不是很懂)

  1. <?php
  2. /**
  3. *弹框
  4. */
  5. function _alert($_info){
  6. echo "<script type='text/javascript'>alert('$_info');history.back();</script>";
  7. exit;
  8. }
  9. /**
  10. * _location():弹出一个对话框并且转跳到另一个界面
  11. */
  12. function _location($_info,$_url){
  13. if($_info==null){
  14. header('Location:'.$_url);
  15. }else{
  16. echo "<script type='text/javascript'>alert('$_info');location.href='$_url';</script>";
  17. exit;
  18. }
  19. }
  20. /**
  21. * _connect():连接数据库
  22. */
  23. function _connect()
  24. {
  25. //定义全局变量$_conn,在函数外部也能调用
  26. global $_conn;
  27. $_conn=mysqli_connect(DB_HOST, DB_USER,DB_PWD);
  28. if (!$_conn) {
  29. exit('数据库连接失败:'.mysqli_error($_conn));
  30. }
  31. }
  32. /**
  33. * _select_db():选择数据库
  34. */
  35. function _select_db(){
  36. global $_conn;
  37. if(!mysqli_select_db($_conn,DB_NAME)){
  38. exit('找不到数据库'.mysqli_error($_conn));
  39. }
  40. }
  41. /**
  42. * _set_names():设置字符编码
  43. */
  44. function _set_names(){
  45. global $_conn;
  46. if(!mysqli_query($_conn,'SET NAMES UTF8')){
  47. exit('字符编码错误'.mysqli_error($_conn));
  48. }
  49. }
  50. /**
  51. * _query():执行sql语句
  52. * @param string $_sql sql操作语句
  53. * @return string 返回结果集
  54. */
  55. function _query($_sql){
  56. global $_conn;
  57. if(!$result=mysqli_query($_conn,$_sql)){
  58. exit('SQL执行失败'.mysqli_error($_conn).mysqli_errno($_conn));
  59. }
  60. return $result;
  61. }
  62. /**
  63. * _fetch_array():根据sql语句遍历数据库。返回一个数组,键名是数据库的表单结构名
  64. * @param string $_sql sql操作语句
  65. * @return array|null
  66. */
  67. function _fetch_array($_sql){
  68. return mysqli_fetch_all(_query($_sql),MYSQLI_ASSOC);
  69. }
  70. /**
  71. * _num_rows():返回数据库中查找条件的数据个数
  72. * @param string $_sql sql操作语句
  73. * @return int 返回数据个数
  74. */
  75. function _num_rows($_sql){
  76. return mysqli_num_rows(_query($_sql));
  77. }
  78. /**
  79. * _affected_rows():返回数据库里被影响到的数据条数
  80. * @return int 返回影响到的记录数
  81. */
  82. function _affected_rows(){
  83. global $_conn;
  84. return mysqli_affected_rows($_conn);
  85. }
  86. /**
  87. * _is_repeat():判断数据在数据库里是否已经存在
  88. * @param string $_sql sql操作语句
  89. * @param string $_info 弹窗上显示的文字
  90. */
  91. function _is_repeat($_sql,$_info){
  92. if(_fetch_array($_sql)){
  93. _alert_back($_info);
  94. }
  95. }
  96. /**
  97. * _close():关闭数据库
  98. */
  99. function _close(){
  100. global $_conn;
  101. if(!mysqli_close($_conn)){
  102. exit('数据库关闭异常'.mysqli_error($_conn));
  103. }
  104. }
  105. ?>

index.php 文件

  1. <?php
  2. session_start();//开启Session功能。
  3. include "php/connect.php";
  4. //接收数据
  5. if(isset($_POST['user_id']) && isset($_POST['password'])){
  6. //从数据库里查找用户名是否存在
  7. $mdpw=substr(md5($_POST['password']),8,16);//对用户输入密码进行16位MD5加密。
  8. $_sql = "SELECT passwd FROM people WHERE user_id='{$_POST['user_id']}'";
  9. $result = _fetch_array($_sql);
  10. if(!empty($result[0])){
  11. if($mdpw==$result[0]['passwd']){
  12. if($_POST['user_id'] == "admin")
  13. {
  14. $_SESSION['user_id']="{$_POST['user_id']}";
  15. _location('','php/admin.php');//管理员界面
  16. }
  17. else{
  18. $_SESSION['user_id']="{$_POST['user_id']}";
  19. _location('','php/user.php'); //用户界面
  20. }
  21. }else if($_POST['password']==''){
  22. _alert('密码为空,请输入密码');
  23. }
  24. else{
  25. _alert('密码错误');
  26. }
  27. }else if($_POST['userid']=='' && $_POST['password']==''){
  28. _alert('用户名和密码为空,请输入用户名和密码');
  29. }else if($_POST['userid']==''){
  30. _alert('用户名为空,请输入用户名');
  31. }else {
  32. _alert('用户名不存在');
  33. }
  34. _close();
  35. exit;
  36. }
  37. ?>
  38. <!DOCTYPE html>
  39. <head>
  40. <meta charset="UTF-8">
  41. <title>Login</title>
  42. <link rel="stylesheet" href="css/login.css" >
  43. </head>
  44. <body>
  45. <div id="main">
  46. <div id="avatar">
  47. </div>
  48. <form action="index.php" method="post">
  49. 用户名:<input type="text" name="user_id" style="height: 40px;width: calc(100% - 10px);border: none;outline: none;padding: 0 5px;background: rgba(0,0,0,0.5);color: #ffcae5;font-size: 16px;">
  50. 密码:<input type="password" name="password" style="height: 40px;width: calc(100% - 10px);border: none;outline: none;padding: 0 5px;background: rgba(0,0,0,0.5);color: #ffcae5;font-size: 16px;">
  51. <input type="submit" value="提交" style="width: 75%;height: 40px;display: block;margin: 30px auto;background:#ffc5d1 ;border: none;outline: none;color: #fff;font-size: 16px;cursor: pointer;">
  52. </form>
  53. <div id="footer">
  54. <a href="#">Forget Password?</a>
  55. </div>
  56. </div>
  57. </body>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/900951
推荐阅读
相关标签
  

闽ICP备14008679号