puyhon_bomb----字符串补充

随机字符串

导入string模块

ctrl+enter :自动导入模块

import string
import random

get_code = string.ascii_letters +string.digits
#输出所有字母(大小写)和数字
print(get_code)

#ctrl+enter :自动导入模块

《puyhon_bomb----字符串补充》

恺撒加密

每个英文字母替换为字母表第k个字母
text = ‘hello’
next = ‘khoor’

print(string.ascii_letters)

#旧的:abcdefghijklmnopqrstuvwxyz   ABCDEFGHIJKLMNOPQRSTUVWXYZ
#新的:defghijklmnopqrstuvwxyzabc   DEFGHIJKLMNOPQRSTUVWXYZABC
def kaisa(text='hello',k=3):
    #对原有小写字母向右移动k位
    lower = string.ascii_lowercase[k:]+string.ascii_lowercase[:k]
    upper = string.ascii_uppercase[k:]+string.ascii_uppercase[:k]
    #用于创建字符串映射的转换表,这里生成的table是一个字典{旧的ascii:新的ascii}
    table = str.maketrans(string.ascii_letters,lower+upper)
    #根据转换表去转换对应字符
    return text.translate(table)

crypt = kaisa()
print(crypt)

khoor

《puyhon_bomb----字符串补充》

暴力破解


import string
def kaisa(text='hello',k=3):
    #对原有小写字母向右移动k位
    lower = string.ascii_lowercase[k:]+string.ascii_lowercase[:k]
    upper = string.ascii_uppercase[k:]+string.ascii_uppercase[:k]
    #用于创建字符串映射的转换表,这里生成的table是一个字典{旧的ascii:新的ascii}
    table = str.maketrans(string.ascii_letters,lower+upper)
    #根据转换表去转换对应字符
    return text.translate(table)
    
def check(text):
    """
    思路:测试文本中是否存在至少两个最常见的英文单词,如果有,则代表破解成功
    """
    mostcommands =('is','and','have','to','not')
    #[1 for word in mostcommands if word in text]
    #遍历mostcommand,如果这个单词在破解后的文本里,列表添加‘1’
    return len([1 for word in mostcommands if word in text])>2


def bruteforce(text):
    for i in range(26): #所有可能的偏移值,一次次调用kaisa(),直到check()返回值True
        t = kaisa(text,-i)  #往左移,开始破解
        if check(t):    #如果满足check返回值是True
            print(i)
            print(t)
            break

text="If not to the sun for smiling, warm is still in the sun there, but wewill laugh more confident calm; if turned to found his own shadow, appropriate escape, the sun will be through the heart,warm each place behind the corner; if an outstretched palm cannot fall butterfly, then clenched waving arms, given power; if I can't have bright smile, it will face to the sunshine, and sunshine smile together, in full bloom."
cryptstr = kaisa(text=text,k=10)
print(cryptstr)

bruteforce(cryptstr)

Sp xyd dy dro cex pyb cwsvsxq, gkbw sc cdsvv sx dro cex drobo, led gogsvv vkeqr wybo myxpsnoxd mkvw; sp debxon dy pyexn rsc ygx crknyg, kzzbyzbskdo ocmkzo, dro cex gsvv lo drbyeqr dro rokbd,gkbw okmr zvkmo lorsxn dro mybxob; sp kx yedcdbodmron zkvw mkxxyd pkvv leddobpvi, drox mvoxmron gkfsxq kbwc, qsfox zygob; sp S mkx'd rkfo lbsqrd cwsvo, sd gsvv pkmo dy dro cexcrsxo, kxn cexcrsxo cwsvo dyqodrob, sx pevv lvyyw.
10
If not to the sun for smiling, warm is still in the sun there, but wewill laugh more confident calm; if turned to found his own shadow, appropriate escape, the sun will be through the heart,warm each place behind the corner; if an outstretched palm cannot fall butterfly, then clenched waving arms, given power; if I can't have bright smile, it will face to the sunshine, and sunshine smile together, in full bloom.

《puyhon_bomb----字符串补充》

    原文作者:SheenStar
    原文地址: https://segmentfault.com/a/1190000016059192
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞