C#的加密解密,请教这个哪有有关问题如何改
C#的加密解密,请问这个哪有问题怎么改?
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int Key;
string p, c;
while(true)
{
Console.WriteLine("请输入密钥:");
Key = int.Parse(Console.ReadLine());
Console.WriteLine("加密请输入(1),解密请输入(2)");
int n=int.Parse(Console.ReadLine());
if(n==1)
{
Console.WriteLine("请输入明文:");
string str = Console.ReadLine();
char[] plaintext = str.ToCharArray();
for (int i = 0; i <= plaintext.Length; i++)
{
if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')
{
int k = (int)plaintext[i];
k += Key;
if (plaintext[i] > 'Z')
{
k -= 26;
}
}
if (i >= 'a' && i <= 'z')
{
k += Key;
if (i > 'z')
{
k += 26;
}
}
if (i >= '0')
{
k += Key;
}
char i=(char) plaintext[k];
}
Console.WriteLine("输出地密文为:{0}",i);
}
Console.WriteLine();
if(n==2)
{
Console.WriteLine("请输入密文:");
string str=Console.ReadLine();
char[] ciphertext = str.ToCharArray();
for(int i=0;i<=ciphertext.Length;i++)
{
if(i >= 'A' && i <= 'Z')
{
i=i-Key;
if(i>'Z')
{
i=i+26;
}
}
if(i>='a'&&i<='z')
{
i=i+Key;
if(i>'z')
{
i=i+26;
}
}
if(i>='0')
{
i=i-Key;
}
}
Console.WriteLine("输出地明文为:{0}",p);
}
Console.WriteLine("* * * * * * * * * * * 感谢使用 * * * * * * * * * * *");
}
}
}
}
------解决思路----------------------
对字符串进行加密和解密
private void btn_Encrypt_Click(object sender, EventArgs e)
{
if (txt_password.Text.Length == 4)//判断加密密钥长度是否正确
{
try
{
txt_EncryptStr.Text = //调用实例ToEncrypt方法得到加密后的字符串
new Encrypt().ToEncrypt(
txt_password.Text, txt_str.Text);
//Encrypt P_Encrypt = new Encrypt();
//P_Encrypt.ToEncrypt(""
}
catch (Exception ex)//捕获异常
{
MessageBox.Show(ex.Message);//输出异常信息
}
}
else
{
MessageBox.Show("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
}
}
private void btn_UnEncrypt_Click(object sender, EventArgs e)
{
if (txt_password2.Text.Length == 4)//判断加密密钥长度是否正确
{
try
{
txt_str2.Text = //调用ToDecrypt方法得到解密后的字符串
new Encrypt().ToDecrypt(
txt_password2.Text, txt_EncryptStr2.Text);
}
catch (Exception ex)//捕获异常
{
MessageBox.Show(ex.Message);//输出异常信息
}
}
else
{
MessageBox.Show("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int Key;
string p, c;
while(true)
{
Console.WriteLine("请输入密钥:");
Key = int.Parse(Console.ReadLine());
Console.WriteLine("加密请输入(1),解密请输入(2)");
int n=int.Parse(Console.ReadLine());
if(n==1)
{
Console.WriteLine("请输入明文:");
string str = Console.ReadLine();
char[] plaintext = str.ToCharArray();
for (int i = 0; i <= plaintext.Length; i++)
{
if (plaintext[i] >= 'A' && plaintext[i] <= 'Z')
{
int k = (int)plaintext[i];
k += Key;
if (plaintext[i] > 'Z')
{
k -= 26;
}
}
if (i >= 'a' && i <= 'z')
{
k += Key;
if (i > 'z')
{
k += 26;
}
}
if (i >= '0')
{
k += Key;
}
char i=(char) plaintext[k];
}
Console.WriteLine("输出地密文为:{0}",i);
}
Console.WriteLine();
if(n==2)
{
Console.WriteLine("请输入密文:");
string str=Console.ReadLine();
char[] ciphertext = str.ToCharArray();
for(int i=0;i<=ciphertext.Length;i++)
{
if(i >= 'A' && i <= 'Z')
{
i=i-Key;
if(i>'Z')
{
i=i+26;
}
}
if(i>='a'&&i<='z')
{
i=i+Key;
if(i>'z')
{
i=i+26;
}
}
if(i>='0')
{
i=i-Key;
}
}
Console.WriteLine("输出地明文为:{0}",p);
}
Console.WriteLine("* * * * * * * * * * * 感谢使用 * * * * * * * * * * *");
}
}
}
}
------解决思路----------------------
对字符串进行加密和解密
private void btn_Encrypt_Click(object sender, EventArgs e)
{
if (txt_password.Text.Length == 4)//判断加密密钥长度是否正确
{
try
{
txt_EncryptStr.Text = //调用实例ToEncrypt方法得到加密后的字符串
new Encrypt().ToEncrypt(
txt_password.Text, txt_str.Text);
//Encrypt P_Encrypt = new Encrypt();
//P_Encrypt.ToEncrypt(""
}
catch (Exception ex)//捕获异常
{
MessageBox.Show(ex.Message);//输出异常信息
}
}
else
{
MessageBox.Show("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
}
}
private void btn_UnEncrypt_Click(object sender, EventArgs e)
{
if (txt_password2.Text.Length == 4)//判断加密密钥长度是否正确
{
try
{
txt_str2.Text = //调用ToDecrypt方法得到解密后的字符串
new Encrypt().ToDecrypt(
txt_password2.Text, txt_EncryptStr2.Text);
}
catch (Exception ex)//捕获异常
{
MessageBox.Show(ex.Message);//输出异常信息
}
}
else
{
MessageBox.Show("密钥长度不符!", "提示");//提示用户输入密钥长度不正确
}
}