數據加密傳輸與解密

數據加密傳輸與解密部分代碼

1 網頁js加密算法

function base64Decode(input) {
    _keyStr = "ABCDEFHHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789+/=";
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    input = input.replace(/[^A-Za-z0-9\+/\=]/g, "");
    while (i < input.length) {
        enc1 = _keyStr.indexOf(input.charAt(i++));
        enc2 = _keyStr.indexOf(input.charAt(i++));
        enc3 = _keyStr.indexOf(input.charAt(i++));
        enc4 = _keyStr.indexOf(input.charAt(i++));
        chr1 = (enc1 << 2) | (enc2 >> 4);
        chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
        chr3 = ((enc3 & 3) << 6) | enc4;
        output = output + String.fromCharCode(chr1);
        if (enc3 != 64) {
            output = output + String.fromCharCode(chr3);
        }
        if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
        }
    }
    _utf8_decode = function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while (i < utftext.length) {
            c = utftext.charCodeAt(i);
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            } else if ((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i + 1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            } else {
                c2 = utftext.charCodeAt(i + 1);
                c3 = utftext.charCodeAt(i + 2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string
    };
    output = _utf8_decode(output);
    return output;
}

function showDeHtml(J1) {
    return base64Decode(
        (base64Decode(J1)["\x72\x65\x70\x6c\x63\x65"]('\x43\x48\x4b\x61\x32\x47\x46\x4c\x31\x74\x77\x68\x4d\x44\x68\x45\x5a\x56\x66\x44\x66\x55\x32\x44\x6f\x5a\x48\x43\x4c\x5a\x6b',''))
        ["\x72\x65\x70\x6c\x63\x65"]('\x71\x4f\x71\x33\x6b\x52\x49\x78\x73\x32\x36\x72\x6d\x52\x74\x73\x55\x54\x4a\x76\x42\x6e\x39\x5a', '')
    )
}

2 模板代碼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>NewsList</title>
    <script type="text/javascript">
        document.write('<script type="text/javascript" src="getshow/?t='+Date.parse(new_Date())+'"><\/script>');
    </script>
</head>
</html>

3 視圖函數代碼

# -*- coding: utf-8 -*-
__author__='Tinlok' 
import base64
import os
import random
import time
from flask import Blueprint, render_template, request
from App.ext import db
from App.models import News
import requests
from App.settings import BASE_DIR

blue = Blueprint('blue', __name__)

@blue.route('/getnews/')
def get_news():
    # 展示新聞
    news_list = News.query.all()
    news_content = render_template("news_content.html", news_list=news_list)
    # 第一次解碼獲取到的二進制轉爲字符串
    encode_content = base64.standard_b64encode(news_content.encode("utf-8")).decode("utf-8")
    print(encode_content)
    # 拼接字符串
    add_content_encode_content = "CHKa2GFL1twhMDhEZVfDfU2DoZHCLZk" + encode_content + "qOq3kRIxs26rmRtsUTJvBn9Z"
    print(add_content_encode_content)
    # 第二次解碼字符串
    encode_content_twice = base64.standard_b64encode(add_content_encode_content.encode("utf-8")).decode("utf-8")

    return render_template("NewsList.html", news_content=news_content, encode_content_twice=encode_content_twice)

@blue.route('getshow')
def get_show():
    # 設置請求時間在1秒,如果超時則返回“哈哈”
    t = request.args.get('t')
    c = time.time() * 1000
    try:
        t = int(t)
    except:
        return '哈哈'
    if c > t  and c - t < 1000:
        with open(os.path.join(BASE_DIR, 'App/static/js/show.js'), 'r') as f:
            js_content = f.read()
            print(js_content)
        # render_template(js_content)
        return js_content
    else:
        return '哈囉'

4 爬蟲Demo

# -*- coding: utf-8 -*-
__author__='Tinlok'

import requests

def get_data():
    res = requests.get("http://127.0.0.1:5000/getnews")
    print(res.content.decode("utf-8"))

if __name__ == '__main__':
    get_data()

【注】本文僅供學習參考!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章