python实现移位加密和解密
作者:吉米多的围棋 时间:2022-03-20 09:09:27
本文实例为大家分享了python实现移位加密和解密的具体代码,供大家参考,具体内容如下
代码很简单,就不多做解释啦。主要思路是将字符串转为Ascii码,将大小写字母分别移位密钥表示的位数,然后转回字符串。需要注意的是,当秘钥大于26的时候,我使用循环将其不断减去26,直到密钥等效小于26为止。
def encrypt():
temp = raw_input("Please input your sentence: ")
key = int(raw_input("Please input your key: "))
listA = map(ord,temp)
lens = len(listA)
for i in range(lens):
a = listA[i]
if 65 <= a <= 90:
a += key
while a > 90:
a -= 26
elif 97 <= a <= 122:
a += key
while a > 122:
a -= 26
listA[i] = a
listA = map(chr,listA)
listA = ''.join(listA)
print listA
def unencrypt():
temp = raw_input("Please input your sentence: ")
key = int(raw_input("Please input your key: "))
listA = map(ord, temp)
lens = len(listA)
for i in range(lens):
a = listA[i]
if 65 <= a <= 90:
a -= key
while a < 65:
a += 26
elif 97 <= a <= 122:
a -= key
while a < 97:
a += 26
listA[i] = a
listA = map(chr, listA)
listA = ''.join(listA)
print listA
a = int(raw_input("input 0 to encrypt and 1 to unencrypt"))
if a == 0:
encrypt()
elif a == 1:
unencrypt()
效果
来源:https://blog.csdn.net/St_laurence/article/details/83352951
标签:python,加密,解密
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python新手如何进行闭包时绑定变量操作
2021-05-01 15:23:55
Ranorex通过Python将报告发送到邮箱的方法
2021-08-17 03:57:43
![](https://img.aspxhome.com/file/2023/4/97654_0s.png)
Mysql导入TXT文件
2012-01-05 19:01:10
数据库的选择原则是什么?
2010-07-14 21:05:00
Kears+Opencv实现简单人脸识别
2022-06-22 05:42:06
python列表插入append(), extend(), insert()用法详解
2021-05-12 13:32:40
![](https://img.aspxhome.com/file/2023/4/93874_0s.png)
Python卸载模块的方法汇总
2022-03-14 16:50:21
Mootools常用方法扩展(五)
2009-03-03 12:12:00
对Python3使运行暂停的方法详解
2021-02-05 22:55:21
![](https://img.aspxhome.com/file/2023/8/82738_0s.jpg)
Django自定义全局403、404、500错误页面的示例代码
2021-07-27 20:03:33
pycharm实现print输出保存到txt文件
2023-05-09 22:14:20
![](https://img.aspxhome.com/file/2023/4/99104_0s.jpg)
TensorFlow 实战之实现卷积神经网络的实例讲解
2022-11-16 20:35:15
Python使用win32 COM实现Excel的写入与保存功能示例
2021-03-30 11:28:50
![](https://img.aspxhome.com/file/2023/9/75859_0s.png)
Python即时网络爬虫项目启动说明详解
2022-11-29 18:09:24
![](https://img.aspxhome.com/file/2023/1/97341_0s.jpg)
防止网站被采集的理论分析以及十条方法对策第1/2页
2011-03-29 10:38:00
基于PHP+MySQL的聊天室设计
2023-11-22 19:26:25
如何实现网上站点维护?
2010-05-24 18:35:00
Django中的session用法详解
2023-01-12 11:40:45
![](https://img.aspxhome.com/file/2023/4/81804_0s.png)
如何基于Python实现自动扫雷
2023-12-13 15:51:08
![](https://img.aspxhome.com/file/2023/8/85128_0s.jpg)
python将字符串以utf-8格式保存在txt文件中的方法
2023-01-10 14:04:55
![](https://img.aspxhome.com/file/2023/7/98327_0s.jpg)