BigInteger n = new BigInteger("1");
时间: 2024-06-05 15:06:23 浏览: 152
This line of code initializes a new BigInteger object with a value of 1. BigInteger is a class in Java that allows for the manipulation of arbitrarily large integers. This object can be used to perform mathematical operations on very large numbers that would not be possible with primitive data types like int or long.
相关问题
package RSA; import java.math.BigInteger; import java.util.Random; public class RSA { private BigInteger p; private BigInteger q; private BigInteger n; private BigInteger phi; private BigInteger e; private BigInteger d; public RSA(BigInteger p, BigInteger q) { this.p = p; this.q = q; this.n = p.multiply(q); this.phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); this.e = generateE(phi); this.d = e.modInverse(phi); } public BigInteger encrypt(String message) { byte[] bytes = message.getBytes(); BigInteger m = new BigInteger(bytes); return m.modPow(e, n); } public String decrypt(BigInteger cipher) { BigInteger m = cipher.modPow(d, n); return new String(m.toByteArray()); } private BigInteger generateE(BigInteger phi) { BigInteger e = BigInteger.probablePrime(phi.bitLength(), new Random()); while (!phi.gcd(e).equals(BigInteger.ONE)) { e = BigInteger.probablePrime(phi.bitLength(), new Random()); } return e; } public static void main(String[] args) { BigInteger p = BigInteger.valueOf(17); BigInteger q = BigInteger.valueOf(19); RSA rsa = new RSA(p, q); String message ="12"; System.out.println("Original message: " + message); BigInteger cipher = rsa.encrypt(message); System.out.println("Encrypted message: " + cipher); String decryptedMessage = rsa.decrypt(cipher); System.out.println("Decrypted message: " + decryptedMessage); }}为什么我解析的报文会得到乱码
您的加密和解密方法是基于字节转换为 BigInteger 来实现的,这可能会导致一些问题。因为当您将字节转换为 BigInteger 时,它们的编码方式可能会影响最终结果。如果您使用的是默认编码方式,那么在某些情况下,解密后的结果可能会是乱码。
为了解决这个问题,您可以在加密和解密方法中指定使用特定的编码方式,例如 UTF-8。具体来说,在 `encrypt` 方法中,您可以将 `String` 对象转换为字节数组时,使用 `getBytes("UTF-8")` 方法;在 `decrypt` 方法中,您可以将 `BigInteger` 对象转换为字节数组后,使用 `new String(bytes, "UTF-8")` 方法来构造字符串。
修改后的 `encrypt` 和 `decrypt` 方法如下所示:
```
public BigInteger encrypt(String message) {
byte[] bytes = message.getBytes("UTF-8");
BigInteger m = new BigInteger(bytes);
return m.modPow(e, n);
}
public String decrypt(BigInteger cipher) {
BigInteger m = cipher.modPow(d, n);
byte[] bytes = m.toByteArray();
return new String(bytes, "UTF-8");
}
```
这样修改后,您应该能够正确地加密和解密包含非 ASCII 字符的消息了。
改正下面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; } } }
在代码中,有以下几处错误:
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;
}
}
```
阅读全文