• Python 爬取 热词并进行分类数据分析-[安全性改造]


    日期:2020.03.01

    博客期:159

    星期日

      【本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)】

        所有相关跳转:

        a.【简单准备

        b.【云图制作+数据导入

        c.【拓扑数据

        d.【数据修复

        e.【解释修复+热词引用

         f.【JSP演示+页面跳转

        g.【热词分类+目录生成

        h.【热词关系图+报告生成

        i . 【App制作

        j . 【安全性改造】(本期博客)


       我打算从五方面去实现安全性的保障!

      1、访问的数据库权限

        因为毕竟在做项目的时候,只是需要从数据库中抽取数据,并不需要对其进行增加、删除或修改。所以,我们 DBLink 需要使用仅支持复合查询或者简单查询的用户来访问数据库!这样,就不怕代码被恶意篡改了,反正你这个用户只能“读”!

      

      2、访问的语句要实现完整!

        这个是怎么一回事呢?就是我们平常使用的 sql 查询语句结构可以是 Select * From ... 这样的结构很容易被推算出我们数据表的表结构!这样也不好!不安全!

        本片博客会以本人的大二的一个《石家庄地铁线路》的项目为例子(热词分析的部分已经改造好了,但是要等到开学我再公布),那个项目如果说仅仅从实现的角度来说的话,我觉得已经比较完美了,所有现在咱们来找一下它的其他方面的问题。

        地铁线路表的结构如下:

         我们对上面的 line 表进行查询的时候,写 Select 要写全, like this: select code as code ,name as name ,line1 as line1 ,seat1 as seat1 ,line2 as line2, seat2 as seat2 from line

        不可以使用 Select * From line ! 这样相当于直接给数据看!

      3、输入查询数据的内部监听

        我们要到我们的 Servlet 层来做数据的错误检测,因为 js 层可以在浏览器上篡改!

        对的,我要提醒大家 html 文件和 jsp 文件最好不要写 javascript 的内容,有需要的话把它写到 js 文件里,然后内部调用就好了!我们使用浏览器打开有一个程序员调试模式,打开以后可以直接性的修改我们的内容。如果你的健壮性与页面上的存储内容有关,那么用户的 “瞎操作” 就极有可能对整个项目的运转产生不可磨灭的影响。

        对于调用 Servlet 传参数是使用 doPost 好,还是 doGet 好呢?处于安全性的考虑,我给你的建议是 doPost 。

      4、数据的加密(如果没有涉及密码,就没有做的必要了)

        不过,我会简单介绍一下,想不想制造一个密码管理器?在保存密码的时候以自定义的方式将密码加密,连同加密方法,一并保存到文件或数据库中!我现在一般都是使用 DES 的加密算法IDEA加密算法怎么说呢,加密好用,解密不好用。

        我之前有过一期博客是专门讲加密的,而且附有封装的 Java 包。

        所以,这里就简单给大家看一下:(测试用例就在类中)

     1 package test;
     2 
     3 import javax.crypto.*;
     4 import javax.crypto.spec.SecretKeySpec;
     5 
     6 import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
     7 import com.sun.org.apache.xml.internal.security.utils.Base64;
     8 
     9 import java.security.InvalidKeyException;
    10 import java.security.Key;
    11 import java.security.NoSuchAlgorithmException;
    12 
    13 @SuppressWarnings("unused")
    14 public class DESDemo {
    15     public static void main(String[] args) {
    16         String clearText ="hello";
    17         String originKey ="12345678";
    18         try {
    19             String cipherText = desEncrpt(clearText,originKey);
    20             System.out.println(cipherText);
    21             String clearText2 = desDecrpt(cipherText,originKey);
    22             System.out.println(clearText2);
    23         } catch (NoSuchPaddingException e) {
    24             e.printStackTrace();
    25         } catch (NoSuchAlgorithmException e) {
    26             e.printStackTrace();
    27         } catch (InvalidKeyException e) {
    28             e.printStackTrace();
    29         } catch (BadPaddingException e) {
    30             e.printStackTrace();
    31         } catch (IllegalBlockSizeException e) {
    32             e.printStackTrace();
    33         }
    34     }
    35     //ÓÃDESËã·¨½øÐнâÃÜ
    36     public static String desDecrpt(String cipherText, String originKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    37         //»ñÈ¡¼ÓÃÜÀà¶ÔÏó
    38         Cipher cipher =Cipher.getInstance("DES");
    39         SecretKey key =getKey(originKey);
    40         //¶Ô¼ÓÃÜÀà¶ÔÏó½øÐгõʼ»¯
    41         //mode:¼ÓÃÜ/½âÃÜ
    42         //key:¶ÔԭʼÃØÔ¿´¦ÀíÖ®ºóµÄÃØÔ¿
    43         cipher.init(Cipher.DECRYPT_MODE,key);
    44         byte[] decodebytes = null;
    45         try {
    46             decodebytes = Base64.decode(cipherText);
    47         } catch (Base64DecodingException e) {
    48             System.out.println("Ò¯Ò¯£¡ÄúµÄ³ÌÐò±¨´íÀ²£¡³öÏÖ Base64DecodingException À²£¡");
    49         }
    50         //ʹÓüÓÃܹ¤¾ßÀà¶ÔÏó¶ÔÃ÷ÎĽøÐнâÃÜ ±ä³ÉÃ÷ÎÄ
    51         byte[] doFinal= cipher.doFinal(decodebytes);
    52        // String encodetext =Base64.encode(doFinal);
    53         return new String(doFinal);
    54     }
    55     
    56     //ÓÃDESËã·¨½øÐмÓÃÜ£ºÍ¨¹ý¶Ô±ÈÌØΪ½øÐÐÊýѧÔËËã
    57     public static String desEncrpt(String clearText, String originKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    58         //»ñÈ¡¼ÓÃÜÀà¶ÔÏó
    59         Cipher cipher = Cipher.getInstance("DES");
    60         SecretKey key = getKey(originKey);
    61         //¶Ô¼ÓÃÜÀà¶ÔÏó½øÐгõʼ»¯
    62         //mode:¼ÓÃÜ/½âÃÜ
    63         //key:¶ÔԭʼÃØÔ¿´¦ÀíÖ®ºóµÄÃØÔ¿
    64         cipher.init(Cipher.ENCRYPT_MODE,key);
    65         //ʹÓüÓÃܹ¤¾ßÀà¶ÔÏó¶ÔÃ÷ÎĽøÐмÓÃÜ ±ä³ÉÃÜÎÄ
    66         byte[] doFinal= cipher.doFinal(clearText.getBytes());
    67         //System.out.println(doFinal);
    68            String encode = Base64.encode(doFinal);
    69         return encode;
    70     }
    71     //»ñÈ¡SecretKey
    72     private static SecretKey getKey(String originKey) {
    73         //²»¹» ³õʼֵΪ0
    74         byte[]buffer = new byte[8];
    75         //»ñÈ¡Óû§ÌṩµÄԭʼÃÜÔ¿×Ö½ÚÊý×é
    76         byte[] originBytes = originKey.getBytes();
    77         for(int i=0;i<8&&i<originBytes.length;i++){
    78             buffer[i]= originBytes[i];
    79         }
    80         SecretKeySpec key= new SecretKeySpec(buffer,"DES");
    81         return key;
    82     }
    83 }
    DESDemo.java
      1 package test;
      2 
      3 public class IDEADemo {
      4 
      5     private byte[] bytekey;
      6 
      7     public byte[] getKey(String key) {
      8 
      9         int len1 = key.length();
     10 
     11         if (len1 >= 16) {
     12 
     13              key = key.substring(0,16);
     14 
     15         } else {
     16 
     17              for (int i = 0; i < 16 - len1; i++) {
     18 
     19                   key = key.concat("0");
     20 
     21              }
     22 
     23         }
     24 
     25         bytekey = key.getBytes();
     26 
     27         return bytekey;
     28 
     29     }
     30     
     31     public String killNotChar(String agr){
     32         String str = "";
     33         
     34         char [] args = agr.toCharArray();
     35         
     36         int leng = args.length;
     37         
     38         for(int i=0;i<leng;++i)
     39         {
     40             char c = args[i];
     41             if(!((c<='Z'&c>='A')||(c<='z'&c>='a')||(c<='9'&c>='0')))
     42             {
     43                 args[i] = '';
     44                 break;
     45             }
     46             str = str + c;
     47         }
     48         
     49         return str;
     50     }
     51 
     52    /** ¼ÓÃÜStringÃ÷ÎÄÊäÈë,StringÃÜÎÄÊä³ö */
     53 
     54     @SuppressWarnings("unused")
     55      public String getEncString(String strMing) {
     56 
     57         byte[] byteMi = null;
     58 
     59         byte[] byteMing = null;
     60 
     61         String strMi = "";
     62 
     63         try {
     64 
     65              return byte2hex(IdeaEncrypt(bytekey, strMing.getBytes(), true));
     66 
     67         } catch (Exception e) {
     68 
     69              e.printStackTrace();
     70 
     71         } finally {
     72 
     73              byteMing = null;
     74 
     75              byteMi = null;
     76 
     77         }
     78 
     79         return strMi;
     80 
     81     }
     82 
     83     /** ½âÃÜ ÒÔStringÃÜÎÄÊäÈë,StringÃ÷ÎÄÊä³ö*/    
     84 
     85     @SuppressWarnings("unused")
     86     public String getDesString(String strMi) {
     87 
     88         byte[] byteMing = null;
     89 
     90         byte[] byteMi = null;
     91 
     92         String strMing = "";
     93 
     94         try {
     95 
     96              String tmp = new String(IdeaEncrypt(bytekey, hex2byte(strMi
     97 
     98                       .getBytes()), false));
     99 
    100              int len1 = tmp.length();
    101              
    102              String str_new = tmp.substring(0, len1 - 6 );
    103              
    104              //System.out.println(len1-6);
    105              //System.out.println(bytekey.length);
    106              //System.out.println(strMi.length());
    107              
    108              //str_new = killNotChar(str_new);
    109              
    110              return str_new;
    111 
    112         } catch (Exception e) {
    113 
    114              e.printStackTrace();
    115 
    116         } finally {
    117 
    118              byteMing = null;
    119 
    120              byteMi = null;
    121 
    122         }
    123 
    124         return strMing;
    125     }
    126 
    127     private byte[] Encrypt(byte[] bytekey, byte[] inputBytes, boolean flag) {
    128 
    129         byte[] encryptCode = new byte[8];
    130 
    131         int[] key = get_subkey(flag, bytekey);// ·Ö½â×ÓÃÜÔ¿
    132 
    133         encrypt(key, inputBytes, encryptCode);// ½øÐмÓÃܲÙ×÷
    134 
    135         return encryptCode;// ·µ»Ø¼ÓÃÜÊý¾Ý
    136 
    137     }
    138 
    139     private int bytesToInt(byte[] inBytes, int startPos) {
    140 
    141         return ((inBytes[startPos] << 8) & 0xff00)
    142 
    143                   + (inBytes[startPos + 1] & 0xff);
    144 
    145     }
    146 
    147     private void intToBytes(int inputInt, byte[] outBytes, int startPos) {
    148 
    149         outBytes[startPos] = (byte) (inputInt >>> 8);
    150 
    151         outBytes[startPos + 1] = (byte) inputInt;
    152 
    153     }
    154 
    155     private int x_multiply_y(int x, int y) {
    156 
    157         if (x == 0) {
    158 
    159              x = 0x10001 - y;
    160 
    161         } else if (y == 0) {
    162 
    163              x = 0x10001 - x;
    164 
    165         } else {
    166 
    167              int tmp = x * y;
    168 
    169              y = tmp & 0xffff;
    170 
    171              x = tmp >>> 16;
    172 
    173              x = (y - x) + ((y < x) ? 1 : 0);
    174 
    175         }
    176 
    177         return x & 0xffff;
    178 
    179     }
    180 
    181     /** ¼ÓÃܺ¯Êý*/
    182 
    183     private void encrypt(int[] key, byte[] inbytes, byte[] outbytes) {
    184 
    185         int k = 0;
    186 
    187         int a = bytesToInt(inbytes, 0);
    188 
    189         int b = bytesToInt(inbytes, 2);
    190 
    191         int c = bytesToInt(inbytes, 4);
    192 
    193         int d = bytesToInt(inbytes, 6);
    194 
    195         for (int i = 0; i < 8; i++) {
    196 
    197              a = x_multiply_y(a, key[k++]);
    198 
    199              b += key[k++];
    200 
    201              b &= 0xffff;
    202 
    203              c += key[k++];
    204 
    205              c &= 0xffff;
    206 
    207              d = x_multiply_y(d, key[k++]);
    208 
    209              int tmp1 = b;
    210 
    211              int tmp2 = c;
    212 
    213              c ^= a;
    214 
    215              b ^= d;
    216 
    217              c = x_multiply_y(c, key[k++]);
    218 
    219              b += c;
    220 
    221              b &= 0xffff;
    222 
    223              b = x_multiply_y(b, key[k++]);
    224 
    225              c += b;
    226 
    227              c &= 0xffff;
    228 
    229              a ^= b;
    230 
    231              d ^= c;
    232 
    233              b ^= tmp2;
    234 
    235              c ^= tmp1;
    236 
    237         }
    238 
    239         intToBytes(x_multiply_y(a, key[k++]), outbytes, 0);
    240 
    241         intToBytes(c + key[k++], outbytes, 2);
    242 
    243         intToBytes(b + key[k++], outbytes, 4);
    244 
    245         intToBytes(x_multiply_y(d, key[k]), outbytes, 6);
    246 
    247     }
    248 
    249     private int[] encrypt_subkey(byte[] byteKey) {
    250 
    251         int[] key = new int[52];
    252 
    253         if (byteKey.length < 16) {
    254 
    255              byte[] tmpkey = new byte[16];
    256 
    257              System.arraycopy(byteKey, 0, tmpkey,
    258 
    259                       tmpkey.length - byteKey.length, byteKey.length);
    260 
    261              byteKey = tmpkey;
    262 
    263         }
    264 
    265         for (int i = 0; i < 8; i++) {
    266 
    267              key[i] = bytesToInt(byteKey, i * 2);
    268 
    269         }
    270 
    271         for (int j = 8; j < 52; j++) {
    272 
    273              if ((j & 0x7) < 6) {
    274 
    275                   key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 6] >> 7)) & 0xffff;
    276 
    277              } else if ((j & 0x7) == 6) {
    278 
    279                   key[j] = (((key[j - 7] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff;
    280 
    281              } else {
    282 
    283                   key[j] = (((key[j - 15] & 0x7f) << 9) | (key[j - 14] >> 7)) & 0xffff;
    284 
    285              }
    286 
    287         }
    288 
    289         return key;
    290 
    291     }
    292 
    293     private int fun_a(int a) {
    294 
    295         if (a < 2) {
    296 
    297              return a;
    298 
    299         }
    300 
    301         int b = 1;
    302 
    303         int c = 0x10001 / a;
    304 
    305         for (int i = 0x10001 % a; i != 1;) {
    306 
    307              int d = a / i;
    308 
    309              a %= i;
    310 
    311              b = (b + (c * d)) & 0xffff;
    312 
    313              if (a == 1) {
    314 
    315                   return b;
    316 
    317              }
    318 
    319              d = i / a;
    320 
    321              i %= a;
    322 
    323              c = (c + (b * d)) & 0xffff;
    324 
    325         }
    326 
    327         return (1 - c) & 0xffff;
    328 
    329     }
    330 
    331     private int fun_b(int b) {
    332 
    333         return (0 - b) & 0xffff;
    334 
    335     }
    336 
    337     private int[] uncrypt_subkey(int[] key) {
    338 
    339         int dec = 52;
    340 
    341         int asc = 0;
    342 
    343         int[] unkey = new int[52];
    344 
    345         int aa = fun_a(key[asc++]);
    346 
    347         int bb = fun_b(key[asc++]);
    348 
    349         int cc = fun_b(key[asc++]);
    350 
    351         int dd = fun_a(key[asc++]);
    352 
    353         unkey[--dec] = dd;
    354 
    355         unkey[--dec] = cc;
    356 
    357         unkey[--dec] = bb;
    358 
    359         unkey[--dec] = aa;
    360 
    361         for (int k1 = 1; k1 < 8; k1++) {
    362 
    363              aa = key[asc++];
    364 
    365              bb = key[asc++];
    366 
    367              unkey[--dec] = bb;
    368 
    369              unkey[--dec] = aa;
    370 
    371              aa = fun_a(key[asc++]);
    372 
    373              bb = fun_b(key[asc++]);
    374 
    375              cc = fun_b(key[asc++]);
    376 
    377              dd = fun_a(key[asc++]);
    378 
    379              unkey[--dec] = dd;
    380 
    381              unkey[--dec] = bb;
    382 
    383              unkey[--dec] = cc;
    384 
    385              unkey[--dec] = aa;
    386 
    387         }
    388 
    389         aa = key[asc++];
    390 
    391         bb = key[asc++];
    392 
    393         unkey[--dec] = bb;
    394 
    395         unkey[--dec] = aa;
    396 
    397         aa = fun_a(key[asc++]);
    398 
    399         bb = fun_b(key[asc++]);
    400 
    401         cc = fun_b(key[asc++]);
    402 
    403         dd = fun_a(key[asc]);
    404 
    405         unkey[--dec] = dd;
    406 
    407         unkey[--dec] = cc;
    408 
    409         unkey[--dec] = bb;
    410 
    411         unkey[--dec] = aa;
    412 
    413         return unkey;
    414 
    415     }
    416 
    417     private int[] get_subkey(boolean flag, byte[] bytekey) {
    418 
    419         if (flag) {
    420 
    421              return encrypt_subkey(bytekey);
    422 
    423         } else {
    424 
    425              return uncrypt_subkey(encrypt_subkey(bytekey));
    426 
    427         }
    428 
    429     }
    430 
    431     private byte[] ByteDataFormat(byte[] data, int unit) {
    432 
    433         int len = data.length;
    434 
    435         int padlen = unit - (len % unit);
    436 
    437         int newlen = len + padlen;
    438 
    439         byte[] newdata = new byte[newlen];
    440 
    441         System.arraycopy(data, 0, newdata, 0, len);
    442 
    443 
    444 
    445         for (int i = len; i < newlen; i++)
    446 
    447              newdata[i] = (byte) padlen;
    448 
    449         return newdata;
    450 
    451     }
    452 
    453     public byte[] IdeaEncrypt(byte[] idea_key, byte[] idea_data, boolean flag) {
    454 
    455         byte[] format_key = ByteDataFormat(idea_key, 16);
    456 
    457         byte[] format_data = ByteDataFormat(idea_data, 8);
    458 
    459         int datalen = format_data.length;
    460 
    461         int unitcount = datalen / 8;
    462 
    463         byte[] result_data = new byte[datalen];
    464 
    465         for (int i = 0; i < unitcount; i++) {
    466 
    467              byte[] tmpkey = new byte[16];
    468 
    469              byte[] tmpdata = new byte[8];
    470 
    471              System.arraycopy(format_key, 0, tmpkey, 0, 16);
    472 
    473              System.arraycopy(format_data, i * 8, tmpdata, 0, 8);
    474 
    475              byte[] tmpresult = Encrypt(tmpkey, tmpdata, flag);
    476 
    477              System.arraycopy(tmpresult, 0, result_data, i * 8, 8);
    478 
    479         }
    480 
    481         return result_data;
    482 
    483     }
    484 
    485     /**¶þÐÐÖÆת×Ö·û´®*/
    486 
    487     public static String byte2hex(byte[] b) { // Ò»¸ö×Ö½ÚµÄÊý£¬
    488 
    489         // ת³É16½øÖÆ×Ö·û´®
    490 
    491         String hs = "";
    492 
    493         String stmp = "";
    494 
    495         for (int n = 0; n < b.length; n++) {
    496 
    497              stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
    498 
    499              // ÕûÊýת³ÉÊ®Áù½øÖƱíʾ
    500 
    501              if (stmp.length() == 1)
    502 
    503                   hs = hs + "0" + stmp;
    504 
    505              else
    506 
    507                   hs = hs + stmp;
    508 
    509         }
    510 
    511         return hs.toUpperCase(); // ת³É´óд
    512 
    513     }
    514 
    515     public static byte[] hex2byte(byte[] b) {
    516 
    517         if ((b.length % 2) != 0)
    518 
    519              throw new IllegalArgumentException("³¤¶È²»ÊÇżÊý");
    520 
    521         byte[] b2 = new byte[b.length / 2];
    522 
    523         for (int n = 0; n < b.length; n += 2) {
    524 
    525              String item = new String(b, n, 2);
    526 
    527              // Á½Î»Ò»×飬±íʾһ¸ö×Ö½Ú,°ÑÕâÑù±íʾµÄ16½øÖÆ×Ö·û´®£¬»¹Ô­³ÉÒ»¸ö½øÖÆ×Ö½Ú
    528 
    529              b2[n / 2] = (byte) Integer.parseInt(item, 16);
    530 
    531         }
    532 
    533         return b2;
    534 
    535     }
    536 
    537     public static void main(String[] args) {
    538 
    539         IDEADemo idea = new IDEADemo();
    540 
    541         idea.getKey("1234");//Éú³ÉÃܳ×
    542         
    543         String str_code = "321323313";
    544         
    545         int len_str = str_code.length();
    546 
    547         String strEnc = idea.getEncString(str_code);//¼ÓÃÜ×Ö·û´®,·µ»ØStringµÄÃÜÎÄ
    548 
    549         System.out.println("ÃÜÎÄΪ£º");
    550 
    551         System.out.println(strEnc);
    552 
    553         String strDes = idea.getDesString(strEnc);//°ÑString ÀàÐ͵ÄÃÜÎĽâÃÜ
    554 
    555         System.out.println("Ã÷ÎÄΪ£º");
    556 
    557         System.out.println(new String(strDes.toCharArray()).substring(0,len_str));
    558         
    559         // 1    ------- 6 -------- 13
    560         // 2       ------- 5 -------- 13
    561         // 3     ------- 4 -------- 13
    562         // 4    ------- 3 -------- 13
    563         // 5    ------- 2 -------- 13
    564         // 6    ------- 1 -------- 13
    565         // 7    ------- 0 -------- 13
    566         // 8    ------- 7 -------- 21
    567         // 9    ------- 6 -------- 21
    568         // 10    ------- 5 -------- 21
    569         // 
    570    }
    571 
    572 }
    IDEADemo.java

        这两个类分别对应 DES 和 IDEA 的加密或解密操作,里面有已经包装好的方法,经过自己的参数设定可以得到密文,用于数据保存真的是不要太合适!

      5、视图的包装改造

        我们在实际项目中应该调用的不是数据库的数据表,因为这个表结构是不应该暴露在外面的,应该使用建立视图的方法,将原数据表的结构进行隐秘改造。

        就拿之前的数据表 line 不能直接进行增删改查!必须要使用视图来隐蔽原数据表(哪怕你就改个名字呢)

  • 相关阅读:
    百度网盘提交提取密码:根据cookies获取loginId 的js
    javaScript 中的私有,共有,特权属性和方法
    centos7 设置时区和时间
    centos 7 su jenkins 切换不过去
    在宿主机查看docker使用cpu、内存、网络、io情况
    Centos6.8通过yum安装mysql5.7 centos7.5适用
    docker-compose 安装redis sentinel,共享主机网络模式
    spring boot通过Interceptor和HandlerMethodReturnValueHandler实现统一处理为controller返回对象统计处理时间
    centos 7.5安装docker-CE 18
    centos查看系统版本信息
  • 原文地址:https://www.cnblogs.com/onepersonwholive/p/12393118.html
Copyright © 2020-2023  润新知