Домашнее задание по внедрению RSA [закрыто]JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Домашнее задание по внедрению RSA [закрыто]

Сообщение Anonymous »

Задача «простая». Используя ключи RSA, которые вы генерируете (вы создаете свою собственную реализацию), шифруйте и дешифруйте файлы, такие как .txt, .png, .pdf и т. д. Проблема в том, что он полностью работает для файлов .txt. Для файлов .pdf это работает нормально (некоторые страницы повреждены), но для PDF-файла, в котором 120 страниц и 2 повреждены, это просто потрясающе. Но для файлов png я получаю хороший рендеринг первого квартала, а затем он начинает портиться. Я предоставляю вам приведенный выше код и очень всем благодарю!!! Мне нужно сделать это с помощью BigInteger, и мне нужно разбить данные до размера фрагмента log2(n).
public static BigInteger calculateN(BigInteger p, BigInteger q) {
return p.multiply(q);
}

public static BigInteger calculateFi(BigInteger p, BigInteger q) {
return (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE));
}

public static BigInteger gcd(BigInteger a, BigInteger b) {
if (b.equals(BigInteger.ZERO)) {
return a;
}
return gcd(b, a.mod(b));
}

public static BigInteger chooseE(BigInteger p, BigInteger q) {
BigInteger fi = calculateFi(p, q);

for (BigInteger e = BigInteger.valueOf(3); e.compareTo(fi) < 0; e = e.add(BigInteger.TWO)) {
if (gcd(e, fi).equals(BigInteger.ONE)) {
return e;
}
}
return BigInteger.valueOf(-1);
}

public static BigInteger modInverse(BigInteger e, BigInteger fi) {
BigInteger m0 = fi;
BigInteger y = BigInteger.ZERO;
BigInteger x = BigInteger.ONE;

if (fi.equals(BigInteger.ONE)) return BigInteger.ZERO;

while (e.compareTo(BigInteger.ONE) > 0) {
BigInteger q = e.divide(fi);
BigInteger t = fi;

fi = e.mod(fi);
e = t;
t = y;

y = x.subtract(q.multiply(y));
x = t;
}

if (x.compareTo(BigInteger.ZERO) < 0) {
x = x.add(m0);
}

return x;
}

public static BigInteger calculateD(BigInteger e, BigInteger fi){
return modInverse(e,fi);
}

private static ArrayList readKey(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line = br.readLine();
br.close();

String[] parts = line.replaceAll("[^0-9,]", "").split(",");
ArrayList key = new ArrayList();

BigInteger first = new BigInteger(parts[0].trim());
BigInteger second = new BigInteger(parts[1].trim());

System.out.println(first);
System.out.println(second);

key.add(first);
key.add(second);

return key;
}

public static void generateKeys(int bits) {
ArrayList generatedNumbers = generate2PrimesUsingMillerRabinTest(5, bits);

if (generatedNumbers.size() < 2) {
throw new IllegalStateException("Failed to generate two primes");
}

BigInteger p = generatedNumbers.get(0);
BigInteger q = generatedNumbers.get(1);
System.out.println("First p : " + p + " Second q : " + q);

BigInteger n = calculateN(p, q);
System.out.println("N is : " + n);
BigInteger fi = calculateFi(p, q);
System.out.println("Fi is : " + fi);

BigInteger e = chooseE(p, q);
System.out.println("E is : " + e);
if (e == null) {
throw new IllegalStateException("Failed to find e");
}

BigInteger d = calculateD(e, fi);
System.out.println("D is : " + d);

// Prepare keys for saving
String publicKey = "(" + e + ", " + n + ")\n";
String privateKey = "(" + d + ", " + n + ")\n";

// Save public key to pubkey.txt
try (BufferedWriter writer = new BufferedWriter(new FileWriter("pubkey.txt"))) {
writer.write(publicKey);
} catch (IOException ex) {
System.err.println("Error writing public key to file: " + ex.getMessage());
}

try (BufferedWriter writer = new BufferedWriter(new FileWriter("privkey.txt"))) {
writer.write(privateKey);
} catch (IOException ex) {
System.err.println("Error writing private key to file: " + ex.getMessage());
}

System.out.println(publicKey);
System.out.println(privateKey);
}

public static void encrypt(String inputFile, String outputFile) throws IOException {
ArrayList key = readKey("pubkey.txt");
BigInteger e = key.get(0);
BigInteger n = key.get(1);

try (FileInputStream fis = new FileInputStream(inputFile);
DataOutputStream dos = new DataOutputStream(new FileOutputStream(outputFile))) {

// Calculate chunk size based on n (log2(n))
long chunkSize = n.bitLength();
int byteChunkSize = (int) Math.floor((double) chunkSize / 8);

byte[] buffer = new byte[byteChunkSize];
int bytesRead;

while ((bytesRead = fis.read(buffer)) != -1) {

byte[] dataChunk = new byte[bytesRead];
System.arraycopy(buffer, 0, dataChunk, 0, bytesRead);

BigInteger messageChunk = new BigInteger(1, dataChunk);

BigInteger encryptedChunk = messageChunk.modPow(e, n);

byte[] encryptedBytes = encryptedChunk.toByteArray();
dos.writeInt(encryptedBytes.length);
dos.write(encryptedBytes);
}
}

System.out.println("Encryption completed. Encrypted data saved to " + outputFile);
}

public static void decrypt(String inputFile, String outputFile) throws IOException {
ArrayList key = readKey("privkey.txt");
BigInteger d = key.get(0);
BigInteger n = key.get(1);

System.out.println(n.doubleValue() + " Double");
System.out.println(n.longValue() + " INT");

try (DataInputStream dis = new DataInputStream(new FileInputStream(inputFile));
FileOutputStream fos = new FileOutputStream(outputFile)) {

// Calculate chunk size based on n (log2(n))
long chunkSize = n.bitLength();
int byteChunkSize = (int) Math.floor((double) chunkSize / 8);

while (dis.available() > 0) {
int encryptedLength = dis.readInt();
byte[] encryptedBuffer = new byte[encryptedLength];
dis.readFully(encryptedBuffer);

BigInteger encryptedChunk = new BigInteger(1, encryptedBuffer);

BigInteger decryptedChunk = encryptedChunk.modPow(d, n);

byte[] decryptedBytes = decryptedChunk.toByteArray();
if (decryptedBytes.length > byteChunkSize) {
fos.write(decryptedBytes, decryptedBytes.length - byteChunkSize, byteChunkSize);
} else {
fos.write(decryptedBytes);
}
}
}

System.out.println("Decryption completed. Decrypted data saved to " + outputFile);
}

public class Utils {
private static BigInteger a = BigInteger.valueOf(6906);
private static BigInteger b = BigInteger.ONE;
private static BigInteger m = BigInteger.TWO.pow(32);
private static BigInteger PREVIOUS_R = BigInteger.ONE;
public static ArrayList keysTimeGeneration = new ArrayList();
public static ArrayList encryptionTime = new ArrayList();
public static ArrayList decryptionTime = new ArrayList();


Подробнее здесь: https://stackoverflow.com/questions/791 ... n-homework
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»