0
import java.io.*;
import java. util.*;
public abstract class gonullu {
public static void main(string[] args) {
scanner input = new scanner(system.in);
// n'th input
system.out. print("enter n: ");
long number = input. nextlong();
system.out. print("enter the message file: ");
string codefile = input. next();
// reading from the chosen file
file file = new file(codefile);
scanner filescanner = new scanner(codefile);
system.out. println("starting to hack... ");
long p = smallprime(number);
long q = number / p;
// initilazin e and d
long e = calculatee(p, q);
long d = calculated(e, p, q);
int count = 0;
// scanning the number from file
long[] mesg = new long[100];
while (filescanner. hasnextlong()) {
mesg[count] = filescanner. nextlong();
count++;
}
system.out. println("p: " + p);
system.out. println("q: " + q);
system.out. println("e: " + e);
system.out. println("d: " + d);
// evaluating each values e's and d's
for (int l = 0; l < count; l++) {
mesg[l] = exppower(mesg[l], d, (p * q));
}
long[] finalmessage = new long[count * 2];
for (int j = 0; j < finalmessage. length; j++) {
}
for (int k = 0; k < finalmessage. length - 1; k++) {
if (finalmessage[k]
0)
system.out. print("a");
if (finalmessage[k]
1)
system.out. print("b");
if (finalmessage[k] 2)
system.out. print("c");
if (finalmessage[k]
3)
system.out. print("d");
if (finalmessage[k] 4)
system.out. print("e");
if (finalmessage[k]
5)
system.out. print("f");
if (finalmessage[k] 6)
system.out. print("g");
if (finalmessage[k]
7)
system.out. print("h");
if (finalmessage[k] 8)
system.out. print("i");
if (finalmessage[k]
9)
system.out. print("j");
if (finalmessage[k] 10)
system.out. print("k");
if (finalmessage[k]
11)
system.out. print("l");
if (finalmessage[k] 12)
system.out. print("m");
if (finalmessage[k]
13)
system.out. print("n");
if (finalmessage[k] 14)
system.out. print("o");
if (finalmessage[k]
15)
system.out. print("p");
if (finalmessage[k] 16)
system.out. print("q");
if (finalmessage[k]
17)
system.out. print("r");
if (finalmessage[k] 18)
system.out. print("s");
if (finalmessage[k]
19)
system.out. print("t");
if (finalmessage[k] 20)
system.out. print("u");
if (finalmessage[k]
21)
system.out. print("v");
if (finalmessage[k] 22)
system.out. print("w");
if (finalmessage[k]
23)
system.out. print("x");
if (finalmessage[k] 24)
system.out. print("y");
if (finalmessage[k]
25)
system.out. print("z");
if (finalmessage[k] == 26)
system.out. print(" ");
}
}
// if a number has 3 or more divisor it's not prime
public static boolean isprime(long n) {
boolean bolyın = true;
int count=0;
for(long i=1;i<n/2;i++){
if(n%i==0){
count++;
}
}
if(count>=2){
bolyın=false;
}
return bolyın;
}
//recursive gcd code
public static long gcd(long x, long y) {
if (y == 0) {
return x;
} else {
return gcd(y, x % y);
}
}
public static long max(long x, long y) {
if (x >= y)
return x;
else
return y;
}
// finds smallest prime factor of given number
public static long smallprime(long n) {
long x = 1;
for (long i = 3; i <= math. sqrt(n); i++) {
if (isprime(i) && (n % i == 0)) {
x= i;
break;
}
}
return x;
}
//calculates for a smallest value of e which is grater than p and q
//and there doesnt exist any common divisor intiger of e and n except 1
public static long calculatee(long p, long q) {
long n = (p - 1) * (q - 1);
long e = 1;
for (long i = 2; i < p - 1; i++) {
if (gcd(i, n) == 1) {
e = i;
}
}
return e;
}
//calculates decryption key d is the inverse of e modulo (p – 1)(q – 1).
public static long calculated(long e, long p, long q) {
long r = (p - 1) * (q - 1);
long d = inverseof(e, r);
return d;
}
// finds the invers of a number by checking if its mod is equal to 1 in modulo x
public static long inverseof(long number, long x) {
int result = 1;
while ((number * result) % x != 1) {
result++;
}
return result;
}
//it calculates the exp power of m in modulo x
// it's recursive if power is 0 then it returns 1 or power is 1 it returns m mod x
// else it multiply the result with it's self as well as power equal to 1
public static long exppower(long m, long power, long x) {
if (power 0)
return 1;
else if (power
1)
return (m % x);
else if (power % 2 == 0)
return (exppower(m, power / 2, x) * (exppower(m, power / 2, x)) % x);
else
return ((m * (exppower(m, (power - 1), x))) % x);
}
}
Tümünü Göster