20231413 张桓溪 2023-2024-2 《Python程序设计》实验四报告

20231413张桓溪 2024-05-29 23:19:19

20231413 2023-2024-2《Python程序设计》实验三报告

课程:《Python程序设计》
班级: 2314
姓名: 张桓溪
学号: 20231413
实验教师:王志强
实验日期:2024年5月15日
必修/选修: 公选课

1.实验要求

Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。

(1)编写从社交网络爬取数据,实现可视化舆情监控或者情感分析。

(2)利用公开数据集,开展图像分类、恶意软件检测等

(3)利用Python库,基于OCR技术实现自动化提取图片中数据,并填入excel中。

(4)爬取天气数据,实现自动化微信提醒

(5)利用爬虫,实现自动化下载网站视频、文件等。

(6)编写小游戏:坦克大战、贪吃蛇、扫雷等等

注:在Windows/Linux系统上使用VIM、PDB、IDLE、Pycharm等工具编程实现。

实际实现内容:网络安全(Crypto-哈希加解密)

2.实验过程及结果

从阿里云的misc1的解题里得到灵感,把当时做那道题的sha256硬算后四位解法完善了一下,做了个支持sha256、MD5、RIPEMD、BLAKE2四种哈希32位加解密的小程序(解密只能解后四位,再多了枚举起来算的太慢了算不出来),那道题第一步就是算一个通过nc进入,每次进入先算一道不一样的sha256后四位解密,当时研究了好久才搞出这个枚举来(之前没见过哈希加密尤其是sha256而且是这种硬算的),自己学习了一下其他几种哈希加密及其原理(以前做的基本都是RSA)。
学习了一下调用hashlib这个库、加解密相关语法:
代码

import hashlib
import itertools

def encrypt_data(data, algorithm='sha256'):
    if algorithm == 'sha256':
        hash_obj = hashlib.sha256(data.encode())
    elif algorithm == 'md5':
        hash_obj = hashlib.md5(data.encode())
    elif algorithm == 'ripemd':
        hash_obj = hashlib.new('ripemd160', data.encode())
    elif algorithm == 'blake2':
        hash_obj = hashlib.blake2b(data.encode())
    else:
        raise ValueError("无效的解密算法选择。")
    
    encrypted_data = hash_obj.hexdigest()
    return encrypted_data

def decrypt_data(target_hash, prefix, algorithm='sha256'):
    # ASCII范围
    possible_chars = [chr(i) for i in range(32, 127)]

    # 尝试所有可能的四个字符组合
    for combo in itertools.product(possible_chars, repeat=4):
        test_str = prefix + "".join(combo)
        if algorithm == 'sha256':
            hash_obj = hashlib.sha256(test_str.encode())
        elif algorithm == 'md5':
            hash_obj = hashlib.md5(test_str.encode())
        elif algorithm == 'ripemd':
            hash_obj = hashlib.new('ripemd160', test_str.encode())
        elif algorithm == 'blake2':
            hash_obj = hashlib.blake2b(test_str.encode())
        else:
            raise ValueError("无效的解密算法选择。")
    
       test_hash = hash_obj.hexdigest()

        if test_hash == target_hash:
            return test_str[-4:]
    return None

def main():
    print("欢迎使用加密解密程序!")
    choice = input("请选择操作:\n1. 加密\n2. 解密\n输入操作编号:")

    if choice == "1":
        data = input("请输入要加密的数据(32位):")
        print("请选择加密算法:\n1. SHA-256\n2. MD5\n3. RIPEMD\n4. BLAKE2")
        encrypt_choice = input("输入加密算法编号:")
        if encrypt_choice == "1":
            encrypted_data = encrypt_data(data, 'sha256')
        elif encrypt_choice == "2":
            encrypted_data = encrypt_data(data, 'md5')
        elif encrypt_choice == "3":
            encrypted_data = encrypt_data(data, 'ripemd')
        elif encrypt_choice == "4":
            encrypted_data = encrypt_data(data, 'blake2')
        else:
            print("无效的加密算法选择。")
            return
        print(f"加密后的结果为:{encrypted_data}")
    elif choice == "2":
        target_hash = input("请输入加密后的64位结果:")
        prefix = input("请输入原数据的前28位:")
        print("请选择解密算法:\n1. SHA-256\n2. MD5\n3. RIPEMD\n4. BLAKE2")
        decrypt_choice = input("输入解密算法编号:")
        if decrypt_choice == "1":
            decrypted_suffix = decrypt_data(target_hash, prefix, 'sha256')
        elif decrypt_choice == "2":
            decrypted_suffix = decrypt_data(target_hash, prefix, 'md5')
        elif decrypt_choice == "3":
            decrypted_suffix = decrypt_data(target_hash, prefix, 'ripemd')
        elif decrypt_choice == "4":
            decrypted_suffix = decrypt_data(target_hash, prefix, 'blake2')
        else:
            print("无效的解密算法选择。")
            return
        if decrypted_suffix:
            print(f"原数据的后4位为:{decrypted_suffix}")
        else:
            print("解密失败,无法找到匹配的字符串。")
    else:
        print("无效的操作选择。")

if __name__ == "__main__":
    main()

结果截图(比较简略,都粘上太多了)

img

img

验算:(找了个网站

img

3.遇到的问题以及实验体会

其实我做这个程序很简单,只是调用一下库并且学习一下语法,但我感觉真正珍贵的是我在遇到那道题的问题之后学习sha256这种加密算法,并且在刚学会python的基础语法(刚上了几节课)之后自己学习调用库、学习进阶一点的语法的过程和在做实验四觉得过于简陋之后自己接着添加MD5、RIPEMD和BLAKE2,了解哈希加密的各种分支的这个过程。好像主要是Crypto方面的进展哈哈,但是这个小程序从阿里云ctf开始,到python课结课我开始写实验四,这个小程序真的代表了我的python学习的进程。它的原始代码从这样开始:

import hashlib
import itertools

target_hash = "8b6c37a5be0a75c26d844fe60fc47c3685c15188877caae31a4795091f524bcc"
prefix = "jcgt7jlnsx7n9tx9uoaipxu8hc9h"

# ASCII范围
possible_chars = [chr(i) for i in range(32, 127)]

# 尝试所有可能的四个字符组合
for combo in itertools.product(possible_chars, repeat=4):
    test_str = prefix + "".join(combo)
    hash_obj = hashlib.sha256(test_str.encode())
    test_hash = hash_obj.hexdigest()

    if test_hash == target_hash:
        print(f"找到匹配的字符串:{test_str}")
        break

当时甚至连数据都是自己从nc网站9999端口之后从终端里copy过来的哈哈,这个程序从学习哈希加密到学习python再到写出来几乎用了我一下午,虽然那天我们队就做出来一道题,这道题在我做出这个程序之后又卡在了wireshark找包,但是学习的过程真的很有意思

img

(这道题)(真不会)

扯远了,总之虽然这个项目不是用爬虫爬点东西(本来想试试反反爬虫的原理)但是感觉挺有意义的。虽然估计碰不着这种题了哈哈。之后打算尝试一下用华为云先搭个不断刷新信息的网站,再用爬虫爬网站某个端口的信息自己试试。

4.课程体会

很荣幸当王老师的课代表!上咱们课的氛围真的很好,而且奖励分也让人有回答问题的欲望(可惜学习通没有举手功能)

上咱们这门课让我接触了python这门对我来说很新颖的编程语言,希望我能在之后的python学习中以咱们课程的内容为基础,继续加强我对这门语言的掌握!也很谢谢老师幽默风趣的教导~

...全文
169 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

110

社区成员

发帖
与我相关
我的任务
社区描述
人生苦短,我用Python!
python3.11 高校
社区管理员
  • blackwall0321
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧