改正下面C#的代码错误public partial class Form7 : Form { private BigInteger p, q, n, phi_n, e, d; public Form7() { InitializeComponent(); } private void Form7_Load(object sender, EventArgs e) { GenerateKeys(); } private void GenerateKeys() { // 选择两个质数p和q string string1, string2; Int64 n, p, q, phi_n, e; p = BigInteger.Parse("12347534159895123"); q = BigInteger.Parse( "987654321357159852"); n = p * q; phi_n = (p - 1) * (q - 1); // 选择e并计算d e = 65537; d = ModInverse(e, phi_n); publicKeyTextBox.Text = $"{n} {e}"; privateKeyTextBox.Text = $"{n} {d}"; } private BigInteger ModInverse(BigInteger e, long phi_n) { throw new NotImplementedException(); } // 加密函数 private string Encrypt(string message, BigInteger n, BigInteger e) { BigInteger m = StrToBig(message); BigInteger c = BigInteger.ModPow(m, e, n); return BigToStr(c); } // 解密函数 private string Decrypt(string ctext, BigInteger n, BigInteger d) { BigInteger c = StrToBig(ctext); BigInteger m = BigInteger.ModPow(c, d, n); return BigToStr(m); } // 字符串转换为大整数 private BigInteger StrToBig(string str) { byte[] bytes = System.Text.Encoding.Unicode.GetBytes(str); return new BigInteger(bytes); } // 大整数转换为字符串 private string BigToStr(BigInteger big) { byte[] bytes = big.ToByteArray(); return System.Text.Encoding.Unicode.GetString(bytes); } // 计算模反元素 private BigInteger ModInverse(BigInteger a, BigInteger m) { BigInteger x, y; ExtendedGCD(a, m, out x, out y); return x; } // 扩展欧几里得算法 private void ExtendedGCD(BigInteger a, BigInteger b, out BigInteger x, out BigInteger y) { if (b == 0) { x = 1; y = 0; } else { ExtendedGCD(b, a % b, out y, out x); y -= a / b * x; } } private void encryptButton_Click(object sender, EventArgs e) { string message = inputTextBox.Text; string ctext = Encrypt(message, n, e); outputTextBox.Text = ctext; } private void decryptButton_Click(object sender, EventArgs e) { string ctext = outputTextBox.Text; string message = Decrypt(ctext, n, d); outputTextBox.Text = message; } } }
时间: 2024-03-24 15:38:51 浏览: 80
C#例子代码 A0001_Partial
在代码中,有以下几处错误:
1. 在类中定义了多个变量,名称相同但类型不同:
```
private BigInteger p, q, n, phi_n, e, d;
...
Int64 n, p, q, phi_n, e;
```
需要将其中一个变量改名,避免重名。
2. 在调用 ModInverse 函数时,传入了错误的参数类型:
```
private BigInteger ModInverse(BigInteger e, long phi_n) { ... }
// 应该改为:
private BigInteger ModInverse(BigInteger a, BigInteger m) { ... }
```
3. 在 Encrypt 和 Decrypt 函数中,调用了未定义的 StrToBig 和 BigToStr 函数。这两个函数的实现已经在代码中给出,但是需要将它们的访问修饰符改为 public,以便在其他函数中调用。
修改后的代码如下:
```
public partial class Form7 : Form
{
private BigInteger p, q, n, phi_n, e, d;
public Form7()
{
InitializeComponent();
}
private void Form7_Load(object sender, EventArgs e)
{
GenerateKeys();
}
private void GenerateKeys()
{
// 选择两个质数p和q
BigInteger p, q, phi_n;
p = BigInteger.Parse("12347534159895123");
q = BigInteger.Parse("987654321357159852");
n = p * q;
phi_n = (p - 1) * (q - 1);
// 选择e并计算d
e = 65537;
d = ModInverse(e, phi_n);
publicKeyTextBox.Text = $"{n} {e}";
privateKeyTextBox.Text = $"{n} {d}";
}
// 加密函数
public string Encrypt(string message, BigInteger n, BigInteger e)
{
BigInteger m = StrToBig(message);
BigInteger c = BigInteger.ModPow(m, e, n);
return BigToStr(c);
}
// 解密函数
public string Decrypt(string ctext, BigInteger n, BigInteger d)
{
BigInteger c = StrToBig(ctext);
BigInteger m = BigInteger.ModPow(c, d, n);
return BigToStr(m);
}
// 字符串转换为大整数
public BigInteger StrToBig(string str)
{
byte[] bytes = System.Text.Encoding.Unicode.GetBytes(str);
return new BigInteger(bytes);
}
// 大整数转换为字符串
public string BigToStr(BigInteger big)
{
byte[] bytes = big.ToByteArray();
return System.Text.Encoding.Unicode.GetString(bytes);
}
// 计算模反元素
public BigInteger ModInverse(BigInteger a, BigInteger m)
{
BigInteger x, y;
ExtendedGCD(a, m, out x, out y);
return x;
}
// 扩展欧几里得算法
public void ExtendedGCD(BigInteger a, BigInteger b, out BigInteger x, out BigInteger y)
{
if (b == 0)
{
x = 1;
y = 0;
}
else
{
ExtendedGCD(b, a % b, out y, out x);
y -= a / b * x;
}
}
private void encryptButton_Click(object sender, EventArgs e)
{
string message = inputTextBox.Text;
string ctext = Encrypt(message, n, e);
outputTextBox.Text = ctext;
}
private void decryptButton_Click(object sender, EventArgs e)
{
string ctext = outputTextBox.Text;
string message = Decrypt(ctext, n, d);
outputTextBox.Text = message;
}
}
```
阅读全文