python代码加密解密_Python加密解密代码

'''

作业:中英文加密+解密

做题思路:先练习单独用ascii和汉字加密和解密,见作业后的encode_game和decode_game

爬取中文用的是BeautifulSoup

作业encode_hw和decode_hw基于前面的练习进行整合

'''

import requests

from bs4 import BeautifulSoup

def get_hans(url):

res = requests.get(url)

soup = BeautifulSoup(res.text, 'html.parser')

item = soup.find_all('div', class_='bs_index3')

for i in item:

hans_list0 = i.find_all('li')

for j in hans_list0:

hans = j.text

hans_list.append(hans)

url = 'http://www.zdic.net/zd/zb/cc1'

hans_list = []

get_hans(url)

# 中英文加密

def encode_hw():

code_input = input('>>>>')

encode_input = ''

for tag_encode in code_input:

i_analy = ord(tag_encode)

if 64 < i_analy < 78 or 96 < i_analy < 110:

encode_input += chr(i_analy + 13)+ '|'

elif 77 < i_analy < 91 or 109 < i_analy < 123:

encode_input += chr(i_analy - 13)+ '|'

elif i_analy >= 128:

for index, element in enumerate(hans_list):

if tag_encode == element:

encode_input += str(index) + '|'

else:

encode_input += tag_encode

return encode_input

ret = encode_hw()

print('加密后的是:'+ret)

# 中英文解密

def decode_hw():

decode_input = ''

code_input = input('>>>>')

res = code_input.split('|')

for item in res:

if item.isalpha():

for item_ele in item:

j_analy = ord(item_ele)

if 64 < j_analy < 78 or 96 < j_analy < 110:

decode_input += chr(j_analy + 13)

elif 77 < j_analy < 91 or 109 < j_analy < 123:

decode_input += chr(j_analy - 13)

elif item.isdigit():

index_int = int(item)

decode_input += hans_list[index_int]

else:

decode_input += item

return decode_input

print('解密后的是:'+decode_hw())

# ASCII加密

# def encode_game():

# code_input = input('>>>>')

# encode_input = ''

# for i in code_input:

# i_analy = ord(i)

# if 64 < i_analy < 78 or 96 < i_analy < 110:

# encode_input += chr(i_analy + 13)

# elif 77 < i_analy < 91 or 110 < i_analy < 123:

# encode_input += chr(i_analy - 13)

# else:

# encode_input += i

# return encode_input

# print('加密后的是:'+encode_game())

#

# # ASCII解密

# def decode_game():

# code_input = input('>>>>')

# decode_input = ''

# for j in code_input:

# j_analy = ord(j)

# if 64 < j_analy < 78 or 96 < j_analy < 110:

# decode_input += chr(j_analy + 13)

# elif 77 < j_analy < 91 or 109 < j_analy < 123:

# decode_input += chr(j_analy - 13)

# else:

# decode_input += j

# return decode_input

# print('解密后的是:'+decode_game())

# 汉字加密

#

# import requests

# from bs4 import BeautifulSoup

# def get_hans(url):

# res = requests.get(url)

# soup = BeautifulSoup(res.text,'html.parser')

# item = soup.find_all('div',class_='bs_index3')

# for i in item:

# hans_list0 = i.find_all('li')

# for j in hans_list0:

# hans = j.text

# hans_list.append(hans)

# url = 'http://www.zdic.net/zd/zb/cc1'

# hans_list = []

# get_hans(url)

# # # print(len(hans_list))

# # 中文加密

# def encode_game():

# encode_input = input('>>>>')

# code_analy = ''

# for hans_input in encode_input:

# for index, element in enumerate(hans_list):

# if hans_input == element:

# code_analy += str(index) + '|'

#

# return code_analy

# ret = encode_game()

# print('加密后:'+ret)

#

# #中文解密

# decode_analy = ''

# res = ret.split('|')

# res.remove('')

# # for index in res:

# print(res)

# for i in res:

# index_int = int(i)

# decode_analy += hans_list[index_int]

# print('解密后:'+decode_analy)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值