python实现的Caesar加解密算法,pythoncaesar,Caesar算法是最简单
python实现的Caesar加解密算法,pythoncaesar,Caesar算法是最简单
Caesar算法是最简单的加解密算法...
# Caeser Cipher import sys,os MyCypher = 25 MyDict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz `1234567890-=~!@#$%^&*()_+[]\\;\',./{}|:"<>?' plaintext = 'Hello World!' cryptmsg = '' def encrypt(text, cypher): out_text = '' for e in text: x = e if (e in MyDict): idx = MyDict.find(e) idx = idx + cypher idx = idx % len(MyDict) x = MyDict[idx] out_text = "%s%c" % (out_text, x) return out_text def decrypt(msg, cypher): out_text = '' for e in msg: x = e if (e in MyDict): idx = MyDict.find(e) idx = idx - cypher + len(MyDict) idx = idx % len(MyDict) x = MyDict[idx] out_text = "%s%c" % (out_text, x) return out_text def ask_cypher(): user_input = raw_input('Input Cypher: ') return long(user_input) def ask_text(): user_input = raw_input('Input Text: ') return user_input def ask_action(): print '-----------------------' print '0 - Exit' print '1 - Encrypt' print '2 - Decrypt' print '-----------------------' user_input = raw_input('Select You Action: ') if user_input in ['0', '1', '2']: if user_input == '0': return 'exit' elif user_input == '1': return 'enc' elif user_input == '2': return 'dec' else: return 'exit' # --------------------------------------------------------------- # Program Start Here # --------------------------------------------------------------- MyCypher = ask_cypher() print 'Cypher: %d' % MyCypher for i in range(0, 100): action = ask_action() if action == 'dec': cryptmsg = ask_text() print decrypt(cryptmsg, MyCypher) elif action == 'enc': plaintext = ask_text() print encrypt(plaintext, MyCypher) else: print 'Exit!' break
执行后,输入密码(数字),然后选择动作,最后输入密文或原文,就能得到原文或密文了.
相关内容
- Argparse,,def main():
- 网络爬虫,,# -*- coding
- python socket 轻量级服务器,pythonsocket,使用非阻塞加多线
- python通过正则获取网页上的全部链接,python获取,impor
- python多线程多队列,python多线程队列,程序大概内容如下
- 使用requests登录V2EX,requests登录v2ex,# coding=utf
- Python爬虫暴漫gif下载,Python爬虫gif下载,#!/usr/bin/e
- 服务器日志清理备份,日志清理备份,#! /usr/bin/
- python 实现选择排序,python实现排序, #coding:
- python 转换数字为钱数,python数字钱数,目标: 输入一串
评论关闭