恺撒加密以及生产者,消费者模型

一 字符串部分补充
如何随机生成验证码, 快速生成内推码

import random
import string
code_str = string.ascii_letters + string.digits
print(code_str)
def gen_code(len=4):
    return "".join(random.sample(code_str, len))
print([gen_code(len=6) for i in range(100)])

随机生成6位,包含大小写字母和数字,且不会重复
二 恺撒加密
凯撒加密—加密算法
在密码学中,恺撒密码是一种最简单且最广为人知的加密技术。它是一种替换加密的技术,明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文

import string
import random
def kaisacrypt(text='hello',k=3):
    #对原有大小写字母向右移动k位
    lower=string.ascii_lowercase[k:]+string.ascii_lowercase[:k]
    upper=string.ascii_uppercase[k:]+string.ascii_uppercase[:k]
    #用于创建字符串映射的转换表'hello'
    d=str.maketrans(string.ascii_letters,lower+upper)
    #根据转换表去转换对应的字符
    return text.translate(d)
cryptStr=kaisacrypt()
print(cryptStr)

#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.

def check(text):
    """测试文本中是否存在至少两个最常见的英文单词,如果有,代表破解成功"""
    mostCommonWords=('If','the','is','to','not','and','have','than','for')
    print([word for word in mostCommonWords if word in text])
    # 向前偏移1位的时候,并没有找到常见的word,因此集合为空,直到跟原文本偏移量相同时(18),偏移结束,转换为原文件
    return len([word for word in mostCommonWords if word in text])>2
#暴力破解
def bruteForce(text):
    for i in range(26):
        t=kaisacrypt(text,-i)
        if check(t):
            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=kaisacrypt(text=text,k=18)
print(cryptStr)
bruteForce(cryptStr)

如何来查看asikii码:
通过ord命令和chr命令来查看
三 生产者,消费者模型
产生数据的模块,就形象地称为生产者;而处理数据的模块,就称为消费者;生产者和消费者之间的中介就叫做缓冲区
生产者消费者模式就是通过一个容器来解决生产者和消费者的强耦合问题。生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯,所以生产者生产完数据之后不用等待消费者处理,直接扔给阻塞队列,消费者不找生产者要数据,而是直接从阻塞队列里取,阻塞队列就相当于一个缓冲区,平衡了生产者和消费者的处理能力。这个阻塞队列就是用来给生产者和消费者解耦的

import random
import time

cacheList=[]
cacheListLen=5
def isfull():
    #缓冲区满了
    return len(cacheList)==5

def consumer(name):
    """消费者"""
    print('%s准备买包子......' %(name))
    while True:
        kind=yield
        print('%s购买%s包子成功' %(name,kind))

def producer(name):
    """生产者"""
    print('%s厨师正在生产包子......'%(name))
    kinds=['茄子','豆沙','鸡汁','青椒']
    while True:
        if not isfull():
            #模拟生产数据耗费的时间
            time.sleep(random.random())
            kind=random.choice(kinds)
            print('%s厨师生产%s包子好了' %(name,kind))
            cacheList.append(kind)
        else:
            print('已经有足够的包子')
            yield
p=producer('大锤')
next(p)
consumers=[consumer('user'+str(i)) for i in range(100)]
for i in consumers:
    if not cacheList:
        print('目前没有包子,请等一会')
    else:
        #如果缓冲区没有满,就让生产者继续生产包子,从上次停止的地方继续执行
        if not isfull():
            next(p)
        #从缓冲区拿出包子给消费者
        kind=cacheList.pop()
        next(i)  #执行消费者买包子
        i.send(kind)  #消费者接收包子

猜你喜欢

转载自blog.csdn.net/weixin_42668123/article/details/81806313