常规密码学加解密脚本(python) – 作者:youhao108

一、凯撒加解密(python3)

def change(c,i):
    c = c.lower()
    num = ord(c)
    if num >= 97 and num <= 122:
        num = 97 + ((num - 97) + i) % 26
    return chr(num)


def kaisa_jiami(string,i):
    string_new = ''
    for s in string:
        string_new += change(s,i)
    print(string_new)
    return string_new

def kaisa_jiemi(string):
    for i in range(25):
        print('\n', i, '\n')
        i += 1
        kaisa_jiami(string,i)


def main():
    print('请选择需要的操作:')
    print('1:凯撒加密')
    print('2:凯撒解密')
    choice = input()
    if choice == '1':
        string = input('请输入需要加密的字符串:')
        num = int(input('请输入需要偏移的位数:'))
        kaisa_jiami(string,num)
    elif choice == '2':
        string = input('请输入需要解密的字符串:')
        kaisa_jiemi(string)
    else:
        print('输入错误,请重试!')
        main()

if __name__ == '__main__':
    main()

二、base64和base32解密(python2)

import base64
readfile = open('base.txt','r')
writefile = open('flag.txt','w')
txt = readfile.readlines()[0]
while True:
    try:
        txt = base64.b32decode(txt)
    except:
        txt = base64.b64decode(txt)
    finally:
        print(txt)
        writefile.write(txt)
        writefile.write('\n')
writefile.close()
readfile.close()

三、RC-4加解密(python3)

def get_text():
    text=input('输入明文:')
    return text

def get_key():
    key=''
    active=True
    while active:
        key=input('输入密钥:')
        if key == '':
            print('密钥不能为空,请重新输入!')
        else:
            active=False
    return key

def init(key):
    s=list(range(256))
    j=0
    for i in range(256):
        j=(j+s[i]+ord(key[i%len(key)]))%256
        s[i],s[j] =s[j],s[i]
    print('s初始置换数组为:')
    print(s)
    return s

def trans_stream(message,s_box):
    result=[]
    i=j=0
    for s in message:
        i=(i+1)%256
        j=(j+s_box[i])%256
        s_box[i],s_box[j]=s_box[j],s_box[i]
        t=(s_box[i]+s_box[j])%256
        k=s_box[t]
        result.append(chr(ord(s)^k))
    print('密文为:')
    print(''.join(result))


def start():
    print('欢迎使用RC-4加密!')text=get_text()
    key=get_key()
    print('明文为:'+text)
    print('密钥为:'+key)
    s=init(key)
    trans_stream(text,s)
    temp=input('回车结束程序。')

start()

四、差分曼彻斯特解密(python2)

msg1 = 0x9a9a9a6a9aa9656699a699a566995956996a996aa6a965aa9a6aa596a699665a9aa699655a696569655a9a9a9a595a6965569a59665566955a6965a9596a99aa9a9566a699aa9a969969669aa6969a9559596669
s = bin(msg1)[2:]
print s
r = ""
tmp = 0
for i in xrange(len(s) / 2):
    c = s[i * 2]
    if c == s[i * 2 - 1]:
        r += '1'
    else:
        r += '0'
print hex(int(r, 2))[2:-1].decode('hex')

五、莫斯电码解密(python3)

from __future__ import print_function
while 1:
    a = input("input the string:")
    s = a.split(" ")
    dict = {'01': 'A',
            '1000': 'B',
            '1010': 'C',
            '100':'D',
            '0':'E',
            '0010':'F',
            '110': 'G',
            '0000': 'H',
            '00': 'I',
            '0111':'J',
            '101': 'K',
            '0100': 'L',
            '11': 'M',
            '10': 'N',
            '111': 'O',
            '0110': 'P',
            '1101': 'Q',
            '010': 'R',
            '000': 'S',
            '1': 'T',
            '001': 'U',
            '0001': 'V',
            '011': 'W',
            '1001': 'X',
            '1011': 'Y',
            '1100': 'Z',
            '01111': '1',
            '00111': '2',
            '00011': '3',
            '00001': '4',
            '00000': '5',
            '10000': '6',
            '11000': '7',
            '11100': '8',
            '11110': '9',
            '11111': '0',
            '001100': '?',
            '10010': '/',
            '101101': '()',
            '100001': '-',
            '010101': '.',
            '110011':',',
            '011010':'@',
            '111000':':',
            '101010':':',
            '10001':'=',
            '011110':"'",
            '101011':'!',
            '001101':'_',
            '010010':'"',
            '10110':'(',
            '1111011':'{',
            '1111101':'}'
            };
    for item in s:
        print (dict[item],end='')
    print("\n")

六、栅栏密码解密(python3)

def zhalan(e):
    elen = len(e)
    field = []
    for i in range(2, elen):
        if (elen % i == 0):
            field.append(i)

    for f in field:
        b = elen // f
        result = {x: '' for x in range(b)}
        for i in range(elen):
            a = i % b;
            result.update({a: result[a] + e[i]})
        d = ''
        for i in range(b):
            d = d + result[i]
        print(d)
        d.lower()


if __name__ == '__main__':
    e = 'qddpqwnp-cplen%prqwn_{_zz*d@gq}'
    zhalan(e)

七、转轮机密码解密(python3)

# 五、栅栏密码解密
```c
def zhalan(e):
    elen = len(e)
    field = []
    for i in range(2, elen):
        if (elen % i == 0):
            field.append(i)

    for f in field:
        b = elen // f
        result = {x: '' for x in range(b)}
        for i in range(elen):
            a = i % b;
            result.update({a: result[a] + e[i]})
        d = ''
        for i in range(b):
            d = d + result[i]
        print(d)
        d.lower()


if __name__ == '__main__':
    e = 'qddpqwnp-cplen%prqwn_{_zz*d@gq}'
    zhalan(e)

九、递推算法(python3)

n=['33DB76A7C594BFC3','CD36C2E32A371480',
'8CEE9FF3933365BC','57373FE3C783A78F',
'59B322834BB73B59','423719DD973C6AD3',
'C858FBEABF480DA3','3CC8C789BA7B8135']
#func1
a=1;b=2
f1={}
for i in range(3,100):
    t=a+b
    f1[hex(t)[-16:]]=str(str(i))
    a,b=b,t
#func2
a=1;b=2;c=3
f2={}
for i in range(4,100):
    t=a+b+c
    f2[hex(t)[-16:]]=str(str(i))
    a,b,c=b,c,t
#func3
a=1;b=2;c=3;d=4
f3={}
for i in range(5,100):
    t=a+b+c+d
    f3[hex(t)[-16:]]=str(str(i))
    a,b,c,d=b,c,d,t
#func-4
a=1;b=2;c=3;d=4;e=5
f4={}
for i in range(6,100):
    t=a+b+c+d+e
    f4[hex(t)[-16:]]=str(str(i))
    a,b,c,d,e=b,c,d,e,t
flag='flag{'
for i in range(0,8):
    n[i]=n[i].lower()
    if n[i] in f1.keys():
        flag=flag+f1[n[i]]+"_"
    elif n[i] in f2.keys():
        flag=flag+f2[n[i]]+"_"
    elif n[i] in f3.keys():
        flag=flag+f3[n[i]]+"_"
    elif n[i] in f4.keys():
        flag=flag+f4[n[i]]+"_"
flag=flag[:-1]+'}'
print(flag)

十、二进制幂数(python2)

#! /usr/bin/env python
#coding=utf-8
a="8842101220480224404014224202480122"
a=a.split("0")
flag=''
for i in range(0,len(a)):
     str = a[i]
     list=[]
     sum=0
     for j in str:
        list.append(j)
        length = len(list)
     for k in range(0,length):
        sum+=int(list[k])
     flag+=chr(sum+64)
print flag

来源:freebuf.com 2021-04-04 13:54:19 by: youhao108

© 版权声明
THE END
喜欢就支持一下吧
点赞0
分享
评论 抢沙发

请登录后发表评论