资讯专栏INFORMATION COLUMN

puyhon_bomb----字符串补充

wwq0327 / 2161人阅读

摘要:随机字符串导入模块自动导入模块输出所有字母大小写和数字自动导入模块恺撒加密每个英文字母替换为字母表第个字母旧的新的对原有小写字母向右移动位用于创建字符串映射的转换表,这里生成的是一个字典旧的新的根据转换表去转换对应字符暴力破解对原有小写字母

随机字符串

导入string模块

ctrl+enter :自动导入模块

import string
import random

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

#ctrl+enter :自动导入模块

恺撒加密

每个英文字母替换为字母表第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

暴力破解
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.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/42247.html

相关文章

  • 【C语言】C语言符串 | 关于String | 通过国外教材讲解符串 | 专栏遗漏知识点补充

    摘要:语言中使用斜杠来表示一个字符串的结束。注意事项由于被替换,如果字符串末尾有,换行会出现两次。函数的原型成功返回失败返回将的字符串录入到文件指针中。 前言: 考虑到之前在专栏中讲字符串的时候有些知识点没有详细地讲解,所以本篇是对字符串相关的知识点的补充篇!例如 %s 输出左对齐右对齐,限制...

    trigkit4 评论0 收藏0
  • Java learn 03 常用类 & 容器 (待补充笔记)

    摘要:容器的类图如下图所示接口定义了存取一组对象的方法其子接口和分别定义了存储方式。中的数据对象没有顺序且不可以重复。注意相等的对象应该具有相等的 Chap 6 常用类 focus on : 字符串相关类 (String, StringBuffer) 基本数据类型包装类 Math类 File类 枚举类 6.1 字符串相关类 String 类 -- java.lang.String 类...

    bladefury 评论0 收藏0
  • 一个基于vue和element-ui的树形穿梭框组件

    摘要:在市面上找到一个好用的树形穿梭框组件都很难,又不想仅仅因为一个穿梭框在之外引入其他重量级插件,因此就有了。版本增加穿梭框左侧右侧数据勾选事件,穿梭框左侧右侧底部。 el-tree-transfer 简介·请先阅读文档及版本说明 因为公司业务使用vue框架,ui库使用的element-ui。在市面上找到一个好用的vue树形穿梭框组件都很难,又不想仅仅因为一个穿梭框在element-ui之...

    Corwien 评论0 收藏0
  • 关于BBS的一些功能性补充以及踩坑记录(持续补充)

    摘要:声明以下记录了本人实验性地探索过程,不代表正确,请谨慎食用。取消注释,并添加两个属性,。由于在中被设置成了的,所以并不适合加密存入。算法碰撞的可能性很小,因此基本可以保证和加密后都是独一无二的,防止黑客用彩虹表爆表。 声明:以下记录了本人实验性地探索过程,不代表正确,请谨慎食用。也欢迎提出各种批评建议,帮助我改正错误。谢谢! 1.注册 注册时在注册的jsp页面使用js函数进行合法性验证...

    hightopo 评论0 收藏0
  • JavaScript 的内部字符编码是 UCS-2 还是 UTF-16

    摘要:二和之间的不同和都是的字符编码方式。提示如果你喜欢阅读关于的内部字符编码,可以,这里更详细解释了实际的问题,以及提供了解决方法。 对于 JavaScript 使用的是 UCS-2 还是 UTF-16 这个问题,我找了很久,没有发现一个权威的回答,我决定自己研究一下它。这个回答来自于你对 JavaScript 引擎或者对 JavaScript 语言的理解。 一、著名的 BMP(Basic...

    BlackHole1 评论0 收藏0

发表评论

0条评论

wwq0327

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<