using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace KasiEncrypt
{
class Program
{
public static string Encrypt(string str, int key, bool b)
{
for (int i = 'a'; i <= 'z'; i++)
{
char old = (char)i;
char newchar;
if (b)
newchar = (char)(i + key);
else
newchar = (char)(i - key);
str = str.Replace(old, newchar);
} for (int i = 'A'; i <= 'Z'; i++)
{
char old = (char)i;
char newchar;
if (b)
newchar = (char)(i + key);
else
newchar = (char)(i - key);
str = str.Replace(old, newchar);
}
return str;
}
static void Main(string[] args)
{
Console.WriteLine("Kaisa Encryption Example. Please choose:");
Console.WriteLine("1. Encryption");
Console.WriteLine("2. Decryption");
char i = (char)Console.Read();
bool b = true;
if (i == '1')
{
b = true;
Console.WriteLine("Please input plaintext now:");
}
else if(i == '2')
{
b = false;
Console.WriteLine("Please input cipher now:");
}else
{
Console.WriteLine("Bad Choices.");
return ;
} String str = Console.ReadLine();
Console.WriteLine("Please input the key(0~25) now:");
string key = Console.ReadLine();
int intkey = int.Parse(key);
str = Encrypt(str, intkey, b);
if(b)
Console.WriteLine("Encrypted message is : {0}",str);
else
Console.WriteLine("Decrypted message is : {0}", str);
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace KasiEncrypt
{
class Program
{
public static string Encrypt(string str, int key, bool b)
{
for (int i = 'a'; i <= 'z'; i++)
{
char old = (char)i;
char newchar;
if (b)
newchar = (char)(i + key);
else
newchar = (char)(i - key);
str = str.Replace(old, newchar);
} for (int i = 'A'; i <= 'Z'; i++)
{
char old = (char)i;
char newchar;
if (b)
newchar = (char)(i + key);
else
newchar = (char)(i - key);
str = str.Replace(old, newchar);
}
return str;
}
static void Main(string[] args)
{
Console.WriteLine("Kaisa Encryption Example. Please choose:");
Console.WriteLine("1. Encryption");
Console.WriteLine("2. Decryption");
char i = (char)Console.Read();
bool b = true;
if (i == '1')
{
b = true;
Console.WriteLine("Please input plaintext now:");
}
else if(i == '2')
{
b = false;
Console.WriteLine("Please input cipher now:");
}else
{
Console.WriteLine("Bad Choices.");
return ;
} String str = Console.ReadLine();
Console.WriteLine("Please input the key(0~25) now:");
string key = Console.ReadLine();
int intkey = int.Parse(key);
str = Encrypt(str, intkey, b);
if(b)
Console.WriteLine("Encrypted message is : {0}",str);
else
Console.WriteLine("Decrypted message is : {0}", str);
}
}
}
1. Encryption
2. Decryption
1
Please input plaintext now:
Please input the key(0~25) now:
static void Main(string[] args)
{
Console.WriteLine("Kaisa Encryption Example. Please choose:");
Console.WriteLine("1. Encryption");
Console.WriteLine("2. Decryption");
char i = (char)Console.Read();
bool b = true;
if (i == '1')
你在输1或者2后按回车了吧?!
因为Console.Read是读取1或2之后就已经执行到下面了,你按回车之后string key = Console.ReadLine();这句就只读到了一个回车,啥也没有