Python凯撒密码函数

发布于 2025-01-20 19:18:41 字数 914 浏览 3 评论 0原文

我想编写一个python函数,该功能会自动找到任何加密文本的键并解密文本。我想使用Caesar Cipher加密算法。

此代码是这样,您将输入腐烂,但我希望代码能够自动找到腐烂。

import string
from time import sleep

alphabet = string.ascii_lowercase # "abcdefghijklmnopqrstuvwxyz"

def decrypt(text, rot):
    
    decrypted_text = "" 
    ct = open("rfc8446.txt", "r")

    for cipher in text:

        if cipher in alphabet:  # check if character is an alphabet
            
            position = alphabet.find(cipher) # find the index position of each text 
               
            new_position = (position - rot) % 25 # find the new index position to decrypt
            
            new_character = alphabet[new_position] # 
            decrypted_text += new_character
        else:
            decrypted_text += cipher

    print(decrypted_text)
text = "hnbcnamjh vh krtn unoc vn knqrwm"
rot = 7
decrypt(text, rot)

I want to write a python function that automatically finds the key of any encrypted text and decrypt the text. I want to use the caesar cipher encryption algorithm.

This code is such that, you will enter the rot but I want the code to be able to find the rot by automatically by itself.

import string
from time import sleep

alphabet = string.ascii_lowercase # "abcdefghijklmnopqrstuvwxyz"

def decrypt(text, rot):
    
    decrypted_text = "" 
    ct = open("rfc8446.txt", "r")

    for cipher in text:

        if cipher in alphabet:  # check if character is an alphabet
            
            position = alphabet.find(cipher) # find the index position of each text 
               
            new_position = (position - rot) % 25 # find the new index position to decrypt
            
            new_character = alphabet[new_position] # 
            decrypted_text += new_character
        else:
            decrypted_text += cipher

    print(decrypted_text)
text = "hnbcnamjh vh krtn unoc vn knqrwm"
rot = 7
decrypt(text, rot)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦初醒的夏天 2025-01-27 19:18:41

只需通过循环循环循环,这将为您提供所有可能的解决方案。

import string
from time import sleep


def decrypt(text):
    for rot in range(26):
        alphabet = string.ascii_lowercase # "abcdefghijklmnopqrstuvwxyz"
        decrypted_text = ""
        for cipher in text:
            if cipher in alphabet:
                position = alphabet.find(cipher)
                new_position = (position - rot) % 25
                new_character = alphabet[new_position]
                decrypted_text += new_character
            else:
                decrypted_text += cipher
        print(decrypted_text)


text = "hnbcnamjh vh krtn unoc vn knqrwm"
decrypt(text)

但是,找到好的是更复杂的,因为您需要使用文本合理性矩阵之类的东西来识别哪种“外观”英语“看起来”英语

this will give you all possible solutions, simply by looping for all rotations.

import string
from time import sleep


def decrypt(text):
    for rot in range(26):
        alphabet = string.ascii_lowercase # "abcdefghijklmnopqrstuvwxyz"
        decrypted_text = ""
        for cipher in text:
            if cipher in alphabet:
                position = alphabet.find(cipher)
                new_position = (position - rot) % 25
                new_character = alphabet[new_position]
                decrypted_text += new_character
            else:
                decrypted_text += cipher
        print(decrypted_text)


text = "hnbcnamjh vh krtn unoc vn knqrwm"
decrypt(text)

however finding the good one is way more complicated since you need to identify which solution "looks" english, using stuff like text plausibility matrices

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文