【发布时间】:2016-02-29 06:53:59
【问题描述】:
我想制作一种加密方法,它可以在纯文本变量中接受大写和小写,比如(“Hello World)它只接受小写字母,请帮助我,我想 它工作正常,只有小写字母作为输入,请帮助我
using System;
class SubstitutionCipher
{
static void Main()
{
string key = "jfkgotmyvhspcandxlrwebquiz";
string plainText = "the quick brown fox jumps over the lazy dog";
string cipherText = Encrypt(plainText, key);
string decryptedText = Decrypt(cipherText, key);
Console.WriteLine("Plain : {0}", plainText);
Console.WriteLine("Encrypted : {0}", cipherText);
Console.WriteLine("Decrypted : {0}", plainText);
Console.ReadKey();
}
//encryption method
static string Encrypt(string plainText, string key)
{
char[] chars = new char[plainText.Length];
for(int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = plainText[i] - 97;
chars[i] = key[j];
}
}
return new string(chars);
}
//decryption method
static string Decrypt(string cipherText, string key)
{
char[] chars = new char[cipherText.Length];
for(int i = 0; i < cipherText.Length; i++)
{
if (cipherText[i] == ' ')
{
chars[i] = ' ';
}
else
{
int j = key.IndexOf(cipherText[i]) - 97;
chars[i] = (char)j;
}
}
return new string(chars);
}
}
【问题讨论】:
标签: c# encryption substitution