Программисты JAVA общаются здесь
Anonymous
Исключение индекса массива за пределами в Java
Сообщение
Anonymous » 25 янв 2025, 00:54
Есть эта java.lang.arrayindexoutofboundsexception: 0 Ошибка появляется всякий раз, когда я запускаю свою программу, которая является алгоритмом RSA. < /p>
Код: Выделить всё
package cn;
import java.math.BigInteger;
import java.security.SecureRandom;
public class rsa
{
private final static BigInteger one = new BigInteger("1");
private final static SecureRandom random = new SecureRandom();
private BigInteger privateKey;
private BigInteger publicKey;
private BigInteger modulus;
rsa(int N)
{
BigInteger p = BigInteger.probablePrime(N/2, random);
BigInteger q = BigInteger.probablePrime(N/2, random);
BigInteger phi = (p.subtract(one)).multiply(q.subtract(one));
modulus = p.multiply(q);
publicKey = new BigInteger("65537");
privateKey = publicKey.modInverse(phi);
}
BigInteger encrypt(BigInteger message)
{
return message.modPow(publicKey, modulus);
}
BigInteger decrypt(BigInteger encrypted)
{
return encrypted.modPow(privateKey, modulus);
}
public String toString()
{
String s = "";
s += "public = " + publicKey + "\n";
s += "private = " + privateKey + "\n";
s += "modulus = " + modulus;
return s;
}
public static void main(String[] args)
{
int N = Integer.parseInt(args[0]);
rsa key = new rsa(N);
System.out.println(key);
BigInteger message = new BigInteger(N-1, random);
BigInteger encrypt = key.encrypt(message);
BigInteger decrypt = key.decrypt(encrypt);
System.out.println("message = " + message);
System.out.println("encrypted = " + encrypt);
System.out.println("decrypted = " + decrypt);
}
}
Как найти место ошибки? В eclipse IDE ошибка была указана во второй строке основной функции.
Подробнее здесь:
https://stackoverflow.com/questions/476 ... on-in-java
1737755665
Anonymous
Есть эта java.lang.arrayindexoutofboundsexception: 0 Ошибка появляется всякий раз, когда я запускаю свою программу, которая является алгоритмом RSA. < /p> [code]package cn; import java.math.BigInteger; import java.security.SecureRandom; public class rsa { private final static BigInteger one = new BigInteger("1"); private final static SecureRandom random = new SecureRandom(); private BigInteger privateKey; private BigInteger publicKey; private BigInteger modulus; rsa(int N) { BigInteger p = BigInteger.probablePrime(N/2, random); BigInteger q = BigInteger.probablePrime(N/2, random); BigInteger phi = (p.subtract(one)).multiply(q.subtract(one)); modulus = p.multiply(q); publicKey = new BigInteger("65537"); privateKey = publicKey.modInverse(phi); } BigInteger encrypt(BigInteger message) { return message.modPow(publicKey, modulus); } BigInteger decrypt(BigInteger encrypted) { return encrypted.modPow(privateKey, modulus); } public String toString() { String s = ""; s += "public = " + publicKey + "\n"; s += "private = " + privateKey + "\n"; s += "modulus = " + modulus; return s; } public static void main(String[] args) { int N = Integer.parseInt(args[0]); rsa key = new rsa(N); System.out.println(key); BigInteger message = new BigInteger(N-1, random); BigInteger encrypt = key.encrypt(message); BigInteger decrypt = key.decrypt(encrypt); System.out.println("message = " + message); System.out.println("encrypted = " + encrypt); System.out.println("decrypted = " + decrypt); } } [/code] Как найти место ошибки? В eclipse IDE ошибка была указана во второй строке основной функции. [code]rsa key = new rsa(N);[/code] Подробнее здесь: [url]https://stackoverflow.com/questions/47610200/array-index-out-of-bounds-exception-in-java[/url]