欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python实现移位加密和解密

时间:2021-06-14 09:46:56|栏目:Python代码|点击:

本文实例为大家分享了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()

效果

上一篇:Python实现的连接mssql数据库操作示例

栏    目:Python代码

下一篇:python制作一个简单的gui 数据库查询界面

本文标题:python实现移位加密和解密

本文地址:http://www.codeinn.net/misctech/141797.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有