Python md5转码识别(内含pymysql库使用)

前言

很久不更新了,想着做一份含金量技术量比较高的文章,做了一份md5解密的网站,使用python语言,主要应用flask库,后期数据库导入应用pymysql

读取文本查py

"""
Author:LuckyRiver
Date:2021
第一个flask项目
"""
from flask import Flask,render_template,request
from md5_decrypt import encrypt_md5

app = Flask(__name__) #创建应用对象

@app.route("/index")
def index():
    return render_template("index.html",
                           title="解密md5")

@app.route("/getmd5",methods=['get'])
def get_md5():
    md5_value = request.args.get("md5_value")
    status = False
    with open("./passwords.txt", "r") as f:
        line = f.readline()
        while line:
            # print(line.strip())
            line = f.readline()
            if md5_value.lower().strip() == encrypt_md5(line.strip()):
                status = True
                return line.strip()
                # text = "123456"
    if status == False:
        return "对不起,解不了,请更大的字典"

if __name__ == "__main__":
    app.run(debug=True) #

Md5解密库制作

"""
Author:LuckyRiver
Date:2021
md5解密
"""
fromhashlibimportmd5

def encrypt_md5(data):
	md=md5()
	md.update(data.encode(encoding="utf-8"))
	returnmd.hexdigest()
# print(encrypt_md5("123456"))

html代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{{ title }}</title>
<style>
.a1{
margin:0 auto;
text-align:center;
}
.font{
text-align: center;
}
</style>
<script>
    function show_md5() {
        xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("mydiv").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "/getmd5?md5_value=" + document.getElementById('md5').value, true);
        xmlhttp.send();
        document.getElementById('md5').value = "";
    }
    </script>
</head>

<body style="background-color: pink;">
<h1 style="background-color:red;text-align:center;" id="1">贝塔空间欢迎您</h1>
<hr>
<div class="a1">
<img border="0" src="#" alt="" width="500" height="150" title="这是代码信息">
</div>
<p style="font-family:arial;color:red;font-size:20px;text-align:center;">开始练习</p>
<hr>
<div style="margin-top:70px; font-size:23px; text-align:center;">Welcome <font>Test</font><br>
    密文:<input type="text"  id="md5"><br>
    <input type="button" value = "查询" οnclick="show_md5()">
    <div id="mydiv">解密</div>
</div>
</body>
</html>

在这里插入图片描述

将文本文件导入到数据库中

"""
Author:LuckyRiver
Date:2021
连接mysql案例
"""
from flask import Flask,render_template,request
from md5_decrypt import encrypt_md5
import pymysql

app = Flask(__name__) #创建应用对象

@app.route("/index")
def index():
    return render_template("index.html",
                           title="解密md5")

@app.route("/getmd5",methods=['get'])
def get_md5():
    md5_value = request.args.get("md5_value")
    status = False
    # with open("dic/passwords", "r") as f:
    #     line = f.readline()
    #     while line:
    #         line = f.readline()
    #         if md5_value.lower().strip() == encrypt_md5(line.strip()):
    #             status = True
    #             return line.strip()
    # 打开一个连接
    conn = pymysql.connect(host="127.0.0.1", user="root", password="123456", database="mdtest")
    # 数据读写
    cur = conn.cursor()
    # cur.execute("select password from md5")
    # results=cur.fetchall()
    # for line  in results:
    #     if md5_value.lower().strip() == encrypt_md5(line[0].strip()):
    #         status = True
    #         return line[0].strip()
    sql = "select * from md5 where md5(password) = '"+md5_value+"'"
    cur.execute(sql)
    # 关闭连接
    result = cur.fetchone()
    if result:
        status = True
        return result[1]
    cur.close()
    conn.close()
    if status == False:
        return "对不起,解不了,请更换更大的字典"

if __name__ == "__main__":
    app.run(debug=True) #

快速算法读取查库

"""
Author:LuckyRiver
Date:2021
第一个flask项目
"""
from flask import Flask,render_template,request
from md5_decrypt import encrypt_md5
import pymysql

app = Flask(__name__) #创建应用对象

@app.route("/index")
def index():
    return render_template("index.html",
                           title="解密md5")

@app.route("/getmd5",methods=['get'])
def get_md5():
    md5_value = request.args.get("md5_value")
    status = False
    # 打开一个连接
    conn = pymysql.connect(host="127.0.0.1", user="root", password="123456", database="mdtest")
    # 数据读写
    cur = conn.cursor()
    # cur.execute("select password from md5")
    # results=cur.fetchall()
    # for line  in results:
    #     if md5_value.lower().strip() == encrypt_md5(line[0].strip()):
    #         status = True
    #         return line[0].strip()
    sql = "select * from md5 where md5(password) = '"+md5_value+"'"
    cur.execute(sql)
    # 关闭连接
    result = cur.fetchone()
    if result:
        status = True
        return result[1]
    cur.close()
    conn.close()
    if status == False:
        return "对不起,解不了,请更换更大的字典"

if __name__ == "__main__":
    app.run(debug=True) #

在这里插入图片描述

总结

md5_decrypt库是自我制作的,第二段代码制作好即可调用,对库使用函数方法清晰认知,自我调出,再使用较为庞大的密码库文件,MD5加密解密使用即可轻松完成。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值