C# 仿射加解密
页面布局![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3af09b6d54d673453d38b310dee9ae0f.png)
加密
private void button1_Click_1(object sender, EventArgs e)
{
string plainFs = fsInput.Text;
string newPlainFs = string.Empty;
int a = Convert.ToInt32(textA.Text);
int b = Convert.ToInt32(textB.Text);
string origin = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
fsOutput.Text = "";
foreach (char c in plainFs) // 大小写转换+去空格
{
if (c >= 'a' && c <= 'z')
{
char newc = (char)(c - 32);
newPlainFs += newc;
}
else if (c >= 'A' && c <= 'Z')
{
newPlainFs += c;
}
else if (c == ' ') continue;
}
fsTxet.Text = newPlainFs;
foreach (char c in newPlainFs)
{
fsOutput.Text += origin[(a * (c - 65) + b) % 26];
}
}
解密
private void fsDecryBtn_Click(object sender, EventArgs e)
{
string cipherFs = fsInput.Text;
string newCipherFs = string.Empty;
string origin = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int a = Convert.ToInt32(textA.Text);
int b = Convert.ToInt32(textB.Text);
fsOutput.Text = "";
foreach (char c in cipherFs) // 大小写转换+去空格
{
if (c >= 'a' && c <= 'z')
{
char newc = (char)(c - 32);
newCipherFs += newc;
}
else if (c >= 'A' && c <= 'Z')
newCipherFs += c;
else if (c == ' ') continue;
}
fsTxet.Text = newCipherFs;
int inv = 0;
switch (a)
{
case 1: inv = 1; break;
case 3: inv = 9; break;
case 5: inv = 21; break;
case 7: inv = 15; break;
case 9: inv = 3; break;
case 11: inv = 19; break;
case 15: inv = 7; break;
case 17: inv = 23; break;
case 19: inv = 11; break;
case 21: inv = 5; break;
case 23: inv = 17; break;
case 25: inv = 25; break;
default: fsOutput.Text = "该a无乘法逆元,请重新输入!";break;
}
foreach (char c in newCipherFs)
{
fsOutput.Text += origin[(inv * (c - 65 - b)) % 26];
}
}
又偷懒了,鄙视自己的switch