标签: caesar-cipher

Python中的Caesar Cipher函数

我正在尝试在Python中创建一个简单的Caesar Cipher函数,它根据用户的输入移动字母,并在最后创建一个最终的新字符串.唯一的问题是最终的密文只显示最后一个移位的字符,而不是一个包含所有移位字符的整个字符串.

这是我的代码:

plainText = raw_input("What is your plaintext? ")
shift = int(raw_input("What is your shift? "))

def caesar(plainText, shift): 

    for ch in plainText:
        if ch.isalpha():
            stayInAlphabet = ord(ch) + shift 
            if stayInAlphabet > ord('z'):
                stayInAlphabet -= 26
            finalLetter = chr(stayInAlphabet)
        cipherText = ""
        cipherText += finalLetter

    print "Your ciphertext is: ", cipherText

    return cipherText

caesar(plainText, shift)
Run Code Online (Sandbox Code Playgroud)

python caesar-cipher

15
推荐指数
4
解决办法
12万
查看次数

循环Python解码器环中的字符串中的每个字符

我正在尝试用Python制作一个简单的解码器环.

例:

a=b, `b=c, c=d, etc.  
Run Code Online (Sandbox Code Playgroud)

我希望脚本采用编码消息并输出解码的消息.
例如,我会输入"ifmmp"并输出"hello".

我一直在想我需要将所有角色分开并循环遍历它们并改变它们chr()ord()值.

在python中似乎没有任何关于此的文档.

python string decoder caesar-cipher

10
推荐指数
1
解决办法
4万
查看次数

检查无效输入

我正在编写一个程序,它将一个字符串作为输入,并检查其中的元素是否有效.我希望我的输入只包含较低的字符和句点,感叹号和空格,而不是空字符串.如果用户输入空字符串或无效字符,则会要求他们再次输入字符串:

我知道如何检查字符串中的字符.我用这种方法

alpha ="abcdefghijklmnopqrstuvwxyz"
message= input("Enter message: ")
for i in message:
   if i in alpha:
      print i
Run Code Online (Sandbox Code Playgroud)

通常我会使用下面的方法来检查无效输入,但如果我想检查字符串中的字符,它将不适用于这种情况.我只能使用它来检查消息是否为空

textOK = False
while not textOK:
        message= input(prompt)
        if len(message) == 0:
          print("Message is empty)
        else:
          textOK= True
Run Code Online (Sandbox Code Playgroud)

这将在用户输入空字符串时重新提示.我不知道如何结合这两种方法.简而言之,我想检查我的输入是否只包含较低的字母,句号,感叹号和空格.如果它包含其他特殊字符或数字或者是空字符串,则会提示用户再次输入该消息.请帮忙!!

python invalid-characters while-loop caesar-cipher

6
推荐指数
1
解决办法
2795
查看次数

JavaScript 中的凯撒密码

我正在尝试编写一个程序来解决javascript中的以下问题(写在本段下面)。我不知道为什么我的代码不起作用。有人可以帮助我吗?我是 JavaScript 新手;这是一个免费的代码训练营问题。

\n\n

“现代常见的用法是 ROT13 密码,其中字母的值移动 13 位。因此 \'A\' \xe2\x86\x94 \'N\'、\'B\' \xe2\x86\ x94\'O\' 等等。

\n\n

编写一个函数,将 ROT13 编码字符串作为输入并返回解码后的字符串。”

\n\n

\r\n
\r\n
function rot13(str) { // LBH QVQ VG!\r\n  \r\n  var string = "";\r\n  for(var i = 0; i < str.length; i++) {\r\n    var temp = str.charAt(i);\r\n    if(temp !== " " || temp!== "!" || temp!== "?") {\r\n       string += String.fromCharCode(13 + String.prototype.charCodeAt(temp));\r\n    } else {\r\n      string += temp;\r\n    }\r\n  }\r\n  \r\n  return string;\r\n}\r\n\r\n// Change the inputs below to test\r\nconsole.log(rot13("SERR …
Run Code Online (Sandbox Code Playgroud)

javascript caesar-cipher

6
推荐指数
2
解决办法
5万
查看次数

加密包含unicode的字符串会导致无法识别的字符

我正在尝试在C#中加密字符串:

static public string Encrypt(char[] a)
{
    for (int i = 0; i < a.Length; i++)
    {
        a[i] -= (char)(i + 1);
        if (a[i] < '!')
        {
            a[i] += (char)(i + 20);
        }
    }
    return new string(a);
}
Run Code Online (Sandbox Code Playgroud)

现在,当我输入这个字符串时:

"Qui habite dans un ananas sous la mer?".
Run Code Online (Sandbox Code Playgroud)

加密结果如下:

`Psf3c[[ak[3XT`d3d\3MYKWIZ3XSXU3L@?JAMR`
Run Code Online (Sandbox Code Playgroud)

在@之后,那里有一个无法辨认的角色.我不知道它是怎么到达的,我不知道为什么.

如果我尝试解密它(使用这种方法:)

static public string Decrypt(char[] a)
{
    for (int i = 0; i < a.Length; i++)
    {
        a[i] += (char)(i + 1);
        if ((a[i] - 20) - i <= '!') …
Run Code Online (Sandbox Code Playgroud)

c# encryption caesar-cipher

2
推荐指数
1
解决办法
708
查看次数

使用cs.50.h中的`string`分割C代码中的错误

我在这里有一个程序,我正在尝试使用ceasar密码解码一串字母; 本质上我将字符串中的每个字符"向下"移动一个字母("a" - >"b","f" - >"g","z" - >"a").

我移动一封信的数量取决于我给它的关键.

在这个特定的程序中,我有一个秘密编码的消息硬编码到main()函数中,一个for循环遍历每个可能的密钥.

这个想法是,如果这个秘密信息只是向下移动了x个字母,吐出25个版本的密码将揭示一个可理解的答案.

不幸的是,我正在使用一些对我来说很新的概念 - argc,argv和一个程序中的多个函数.我对此很新.

有人可以帮我解释我得到的分段错误错误吗?我认为我没有引起任何参数溢出.

#include <stdio.h>
#include <cs50.h>
#include <string.h>

string decode(int key, string message);

int main(void)
{
    string secret_message = "ueuag rKJ AGIQ GIAR FEgN";

    for (int i = 0; i < 25; i++)
    {
        decode(i, secret_message);
        printf("%s", secret_message);
    }

    return 0;
}

string decode(int key, string message)
{
    int i;

    for (i = 0; i < strlen(message); i++)
    {
        if (message[i] >= 'a' && message[i] <= 'z') …
Run Code Online (Sandbox Code Playgroud)

c segmentation-fault caesar-cipher cs50

2
推荐指数
1
解决办法
371
查看次数

我的程序在调试时运行正常,但在调试时运行不正确

我目前正在用 C 编写一个简单的程序,该程序从用户那里获取一个命令行参数,该参数是一个替换密码密钥。目前,我的程序正在检查用户可能输入的所有可能不正确的参数。

无论输入是什么,通过调试器运行它都会使我的程序每次都完美运行。但是,在没有调试器的情况下运行我的程序,同时使参数至少有一个小写字母(例如 ./substitution VCHPRZGJNTLSKFBDQWAXEuymoi),会导致“sum”变量在大约 50% 的情况下具有不正确的值。

我将快速解释“sum”变量的用途,以便更容易理解。'sum' 从 2015 年开始,因为这是根据 ASCII 表将所有 26 个大写字母相加的值。我的函数中的 for 循环然后从 2015 中减去密码密钥中的每个字符。最后,最后一个 if 语句检查 sum = 0,这意味着只输入了唯一字符。

在没有调试器的情况下运行它时,'sum' 有时是 0,有时是 -32 * 小写字母的数量。例如,VCHPRZGJNTLSKFBDQWAXEuymoi 有 4 个小写字母,'sum' 有时为 0,有时为 -128。

我不知道这个问题是从哪里来的,我将不胜感激任何帮助。

这是我的代码:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

string sub(string plaintext,string cipher);

int main(int argc, string argv[])
{
    string cipher = argv[1];
    // checks for whether an argument was provided
    if (argc != 2)
    {
        printf("Usage: ./substitution KEY\n");
        return 1;
    }
    // checks …
Run Code Online (Sandbox Code Playgroud)

c debugging caesar-cipher cs50

2
推荐指数
1
解决办法
114
查看次数

TypeError:'instancemethod'对象不可迭代(Python)

我对python和线程很陌生。我试图编写一个使用线程和队列的程序,以便使用凯撒密码对txt文件进行加密。当我单独使用加密功能时,加密功能本身可以很好地工作,但是在程序中使用它时出现错误。错误从此行开始:

for c in plaintext:
Run Code Online (Sandbox Code Playgroud)

这是整个代码:

import threading
import sys
import Queue

#argumanlarin alinmasi
if len(sys.argv)!=4:
    print("Duzgun giriniz: '<filename>.py s n l'")
    sys.exit(0)
else:
    s=int(sys.argv[1])
    n=int(sys.argv[2])
    l=int(sys.argv[3])

#Global
index = 0

#caesar sifreleme


#kuyruk deklarasyonu
q1 = Queue.Queue(n)
q2 = Queue.Queue(2000)


lock = threading.Lock()

#Threadler
threads=[]

#dosyayi okuyarak stringe cevirme
myfile=open('metin.txt','r')
data=myfile.read()


def caesar(plaintext, key):
    L2I = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ", range(26)))
    I2L = dict(zip(range(26), "ABCDEFGHIJKLMNOPQRSTUVWXYZ"))

    ciphertext = ""
    for c in plaintext:
        if c.isalpha():
            ciphertext += I2L[(L2I[c] + key) % 26]
        else: …
Run Code Online (Sandbox Code Playgroud)

python multithreading typeerror caesar-cipher

1
推荐指数
1
解决办法
1万
查看次数

我应该如何让用户输入我想要的信息?

我刚开始学习计算机科学。

我正在通过在哈佛在线教授的 CS50 学习。好吧,我正在解决这个问题,我需要在命令行中从用户那里获取密钥,然后是明文,然后将该文本转换为 ASCII 中的密钥数量以生成密文。

这是我到目前为止所得到的。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int main(int argc, string argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./caesar key\n");
    }

    {
        string plaintext = get_string("plaintext:  ");

        int key = atoi(argv[1]);
        int n = strlen(plaintext);
        char chr[n];

        printf("ciphertext: ");
        for (int i = 0; i < n; i++)
        {
            chr[i] = plaintext[i];
            if (isalpha(chr[i]))
            {
                if (isupper(chr[i]))
                {
                    chr[i] = (chr[i] - 65 + key) % 26 + 65;
                    printf("%c",chr[i]);    
                } …
Run Code Online (Sandbox Code Playgroud)

c caesar-cipher cs50

1
推荐指数
1
解决办法
116
查看次数

一旦到达列表的末尾,就返回到开头,python

我有一个包含所有字母的“字母表”列表,程序应该使用某个单词生成一个字母序列,使用给用户的数字,例如:

Word input = "sun"
Shift_number input = 3
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
Run Code Online (Sandbox Code Playgroud)

输出应该是“vxq”,因为索引向右移动了三个空格,我的问题是当索引的移动超过列表中的变量数量时,例如:

Word input = "zero"
Shift_number = 1
Run Code Online (Sandbox Code Playgroud)

输出应该是“afsp”,但我收到此错误:“列表索引超出范围”。我只需要索引从“z”到“a”

python caesar-cipher

0
推荐指数
1
解决办法
1535
查看次数