- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个凯撒密码,我正在尝试弄清楚如何让它同时处理大写和小写字母。例如,如果我输入“Hello World”,我希望它以相同的格式“Ifmmp Xpsme”进行加密。
using System;
namespace CaesarCipher1
{
class Program
{
static string Encrypt(string value, int shift)
{
shift %= 26;
char[] alphabet = value.ToCharArray();
for (int i = 0; i < alphabet.Length; i++)
{
char letter = alphabet[i];
if (letter == ' ')
continue;
letter = (char)(letter + shift);
if (letter > 'z')
{
letter = (char)(letter - 26);
}
else if (letter < 'a')
{
letter = (char)(letter + 26);
}
// Store.
alphabet[i] = letter;
}
return new string(alphabet);
}
static string Decrypt(string value, int shift)
{
return Encrypt(value, 26 - shift);
}
static void Main(string[] args)
{
bool Continue = true;
Console.WriteLine(" Ceasar Cipher");
Console.WriteLine("-------------------------\n");
while (Continue)
{
try
{
Console.WriteLine("\nType a string to encrypt:");
string UserString = Console.ReadLine();
Console.Write("\nShift: ");
int key = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nEncrypted Data: ");
string cipherText = Encrypt(UserString, key);
Console.WriteLine(cipherText);
Console.Write("\n");
Console.WriteLine("Decrypted Data:");
string t = Decrypt(cipherText, key);
Console.WriteLine(t);
Console.WriteLine("\nDo you want to continue?");
Console.WriteLine("Type in Yes to continue or press any other key and then press enter to quit:");
string response = Console.ReadLine();
Continue = (response == "Yes");
}
catch (FormatException)
{
Console.WriteLine("You entered a bad operation, try another one");
}
}
}
}
}
这就是我得到的,但它是不正确的。
Type a string to encrypt:
Hello World
Shift: 1
Encrypted Data:
cfmmp rpsme
Decrypted Data:
bello qorld
Do you want to continue?
Type in Yes to continue or press any other key and then press enter to quit:
最佳答案
难点在于加密大写字母
时,说'Z'
我们可以得到小写,例如'd'
:
'Z' + 10 == 'd'
让我们提取大小写并使用模运算:
for (int i = 0; i < alphabet.Length; i++) {
char letter = alphabet[i];
if (letter == ' ')
continue;
letter = (char)(char.IsLower(letter)
? (letter - 'a' + shift) % 26 + 26 + 'a'
: (letter - 'A' + shift) % 26 + 26 + 'A');
// Store.
alphabet[i] = letter;
}
或者提取三种情况:
'a'..'z'
'A'..'Z'
代码:
static string Encrypt(string value, int shift) {
if (string.IsNullOrEmpty(value))
return value; // Nothing to encrypt
// ensure, that shift is in [0..25] range
shift = ((shift % 26) + 26) % 26;
StringBuilder sb = new StringBuilder(value.Length);
foreach(char letter in value)
if (letter >= 'a' && letter <= 'z')
sb.Append((char) ((letter - 'a' + shift) % 26 + 'a'));
else if (letter >= 'A' && letter <= 'Z')
sb.Append((char) ((letter - 'A' + shift) % 26 + 'A'));
else
sb.Append(letter);
return sb.ToString();
}
// shift % 26 - in order to avoid integer overflow when shift == int.MinValue
static string Decrypt(string value, int shift) =>
Encrypt(value, 26 - shift % 26);
演示:
string initial = "Hello World!";
int shift = 10;
string encrypted = Encrypt(initial, shift);
string decrypted = Decrypt(encrypted, shift);
Concole.Write($"{initial} -> {encrypted} -> {decrypted}");
结果:
Hello World! -> Rovvy Gybvn! -> Hello World!
关于c# - Caesar Cipher C# - 如何处理大写和小写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57328188/
您好,我正在研究这个 Caesar 密码解密器,它采用加密字符串并使用所有 key 对其进行解密(如果它们与另一条消息相似,则另一个函数将匹配它们)。我有 Decipher 函数,但我只是想知道是否有
我正在研究 cs50 类(class)中的 pset2,但我不明白为什么我一直收到这个我没有声明的错误,因为我想我做了。首先我要求一个数字用作一个用于加密的 key ,而不是我要求的纯文本,它应该用给
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 6 年前。 Improve this qu
我一直在尝试解决 cs50 的凯撒问题的不太舒服的版本,并且只想在这个问题之前说我是一个完全的初学者。 该程序应该要求用户输入 key 和明文,然后使用提供的 key 对该文本进行加密。例如,对于 k
在下面的代码中,根据放置 'Printf("Ciphertext: )' 语句的位置,我得到不同的输出,我无法理解为什么。有人可以指出我缺少什么吗?下面你可以找到代码,以及输出(粗体变量部分)。 #i
来 self 的 caesar.c 文件的代码 #include #include #include #include #include int main(int argc, string
我正在尝试用 JS 创建一个 Caesar Cypher。它应该接受一个字符串和一个数字,并将字符串中的每个字母按提供的数字在字母表中移动,然后返回密码字符串。 我写了一些代码,将每个字母更改为它的
我正在尝试创建一个函数 rotate_character(char, rot) 接收一个字符“char”(长度为1的字符串)和一个整数“rot”。该函数应返回一个长度为 1 的新字符串,这是将 cha
对于类(class),我必须用 C++ 开发一个凯撒加密项目,它接收一个字符串,并将所有 ascii 代码移动一个随机整数。我的项目运行完美,代码如下: #include #include #in
我正在编写一个具有 4 个不同函数的程序,其中一个询问用户是否要编码/解码,另一个询问在动态内存中输入字符串、实际解密函数和输出函数。我做了一切,但我遇到的唯一问题是考虑输入的单词中是否有空格。 在我
我用 C 为我的类(class)编写了一个凯撒密码程序,但它似乎没有正确加密。该程序从句子“THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG”中获取输入并使用从键
我在网上搜索了很长时间,寻找一个简单的 C 语言凯撒芯片/加密算法。我找到了一个,但并不完美,所以我已经更改了代码。仍然有问题,因为一位 friend 说该程序也应该能够处理大 key 。例如带有键的
我正在尝试制作 Julius Caesar Cipher 程序,但通过在句子的开头和结尾添加一个随机字母来添加一个扭曲。出于某种原因,当我输入一个长字符串时,打印时字符串的一部分丢失了。我正在使用 p
为什么我的代码不接受不包含字符 a-z A-Z 0-9 的字符串?如果要对此进行加密以转移例如“aaaaa[[[[[[”,我得到一个错误。我想要代码,以便它也可以接受空格或任何内容,并跳过那些不是 a
我正在尝试运行 edx 编程简介中的程序分配 caesar.c。它需要一个能够使用凯撒加密来加密字符串的程序:因此,用户必须输入 key (命令行);例如, key 为 2 时,“A”字符需要用“C”
我正在创建一个凯撒密码,我正在尝试弄清楚如何让它同时处理大写和小写字母。例如,如果我输入“Hello World”,我希望它以相同的格式“Ifmmp Xpsme”进行加密。 using System;
我正在做一个凯撒密码。尝试将字符串中的所有字符替换为移位字母表中的某个字符。 这是我到目前为止的代码 public static String caesarify(String str, int
我完全是 C++ 的初学者,到目前为止,在学校里我们只学习和使用过 Java。我们今年的第一个项目是创建一个凯撒密码,但我们必须使用我们教授提供的头文件。在这一点上,我只是试图在编码加密和解密方法之前
所以目前,我的代码看起来像这样(感谢我在另一篇文章中的帮助) phrase = raw_input("Enter text to Cipher: ") shift = int(raw_input("P
尝试在 C 中创建一个 Caesar Cypher,并在移动字母时从超过 'z' 的小写字母中获得一些麻烦的输出 #include void confab(const char* inText, i
我是一名优秀的程序员,十分优秀!