加解密算法的实现python_python

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 执行后,输入密码(数字), 然后选择动作, 最后输入密文或原文, 就能得到原文或密文了.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值