Caesar_Ciphers(凯撒移位)python描述

#encryption code from #http://thelivingpearl.com/2013/06/03/caesar-ciphers-in-#python/ 
import sys

def main(argv):
    if (len(sys.argv) != 2):
        sys.exit('Usage: sub.py <k>')

    plaintext = list(raw_input('Enter message: '))
    alphabet = list('abcdefghijklmnopqrstuvwxyz')
    k = int(sys.argv[1])
    cipher = ''

    for c in plaintext:
        if c in alphabet:
            cipher += alphabet[(alphabet.index(c)+k)%(len(alphabet))]

    print 'Your encrypted message is: ' + cipher


if __name__ == "__main__":
    main(sys.argv[1:])

#Mycode to brute force
import sys

def encrypt(string,k):

    plaintext = list(string)
    alphabet1= list('abcdefghijklmnopqrstuvwxyz')
    alphabet2= list('ABVDEFGHIJKLMNOPQRSTUVWXYZ')
    cipher = ''

    for c in plaintext:
        if c in alphabet1:
            cipher += alphabet1[(alphabet1.index(c)+k)%(len(alphabet1))]
        if c in alphabet2:
            cipher += alphabet2[(alphabet2.index(c)+k)%(len(alphabet2))]

    print 'Your encrypted message is: ' + cipher

def main():
        argv=raw_input("Enter message:")
        for k in range(26):
            encrypt(argv,k)
main()
#Another method for Caesar Ciphers from
#http://thelivingpearl.com/2013/06/03/caesar-ciphers-in-#python/
import sys 

def encrypt(k):
    plaintext = raw_input('Enter plain text message: ')
    cipher = ''

    for each in plaintext:
        c = (ord(each)+k) % 126

        if c < 32: 
            c+=31

        cipher += chr(c)

    print 'Your encrypted message is: ' + cipher

def decrypt(k):
    cipher = raw_input('Enter encrypted message: ')
    plaintext = ''

    for each in cipher:
        p = (ord(each)-k) % 126

        if p < 32:
            p+=95

        plaintext += chr(p)

    print 'Your plain text message is: ' + plaintext

def main(argv):
    if (len(sys.argv) != 3):
        sys.exit('Usage: ceaser.py <k> <mode>')

    if sys.argv[2] == 'e':
        encrypt(int(sys.argv[1]))
    elif sys.argv[2] == 'd':
        decrypt(int(sys.argv[1]))
    else:
        sys.exit('Error in mode type')


if __name__ == "__main__":
    main(sys.argv[1:])

#The decode code for above code
import sys 

def decrypt(k,cipher):
    plaintext = ''

    for each in cipher:
        p = (ord(each)-k) % 126

        if p < 32:
            p+=95

        plaintext += chr(p)

    print plaintext

def main(argv):
    if (len(sys.argv) != 1):
        sys.exit('Usage: brute_ceaser.py')

    cipher = raw_input('Enter message: ')

    for i in range(1,95,1):
        decrypt(i,cipher)

if __name__ == "__main__":
    main(sys.argv[1:])

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值