so - first of all - I cleaned up your code - re-formatted it - and, most important of all - put it into code-tags:
so - now lets try to split up what you tried - what you did - and what went wrong
Line 33/34
yes - it's "legal" java code - but - conventions states, that each variable is written in its own line - so it should look like this:
next logic issue: primitives always set to thier defaults - for int default is 0 - but - it makes a difference if you just declare a reference - or if you correctly initialize it
also - convention: variables use "lowerCamelCase"
to pretty it up - that's what it should look like:
next we got at line 36 - Scanner-mess
Ok - you got it working - but I guess only by luck - you shouldn't try to read console input this way.
Here you have a more reliable one:
So here is something a bit more common - so it's also way better recognized by experienced coders:
I'm not quite sure about that "less than (p*q)-z" - but "equal to" is surely wrong as this would result in "0" - so it has to be "less than". Also - you're using Euler PHI function - so your "z" should named "phi" or "phiN":
Just look at my code - or at SUNs code.
So you say your message should be "less than or equal" than: p x q - phiN
No parentheses need as basic math rule states: "multiply/divide always comes before addition/subtraction" - so writing "p x q - phiN" implies you also do "p x q" first - wich is N - and then do "N - phiN" as second step.
Also we now see - this is wrong. In RSA message has to be one bit shorter than modulus: for RSA2048 with N.length 2048 message can up to 2047 bits - as we use 8 bit per byte in modern computers - next lowest value would be 2040bits - this is clearly bigger than "N - phiN". Remember: phiN is (p-1) x (q-1), so you would end up with limit of:
p x q - (p-1)x(q-1)
Simple not-so-real-world-example with some big primes:
p=63709
q=48487
We ignore that it should be: p < q - for this example.
<br />
<br /> N=3089058283
<br /> phiN=(63709-1)x(48487-1)=63708x48486=3088946088
<br />
<br /> So with your limit you would end up with just 112195 - wich is just 17 bits - but you could go up to 31 bits - wich would be 2147483647 - when bound to 8bit-per-byte - we would end up with 24bits wich would be 16777215.
<br />
<br /> Next: line 48
<br />
Ok - text says what you try to get here - a random number co-prime to phiN. At least you're doin the right thing wich I didn't - you checked for co-prime.
Why is co-prime important? If you get a non-co-prime you end up with a multiple of phiN wich ends up in "0" after modulus.
Line 54
Not sure about that - about it looks like modInverse to calculate d. This is already in Java: BigInteger.modInverse().
Last line: 9
I didn't even tried to understand what you tried to do here - but "for();" is just an empty loop after wich you do nothin with your counter so you end up with just return x%n - wich obviously isn't the RSA "magic" function: msg^e mod N.
So, after going through your code one can easy spot some issues wich clearly breaks what ever you tried to accomplish.
This is why
you should not try to implement your own crypto - and as you seen - I missed check for co-prime - so even I'm really into this stuff - I also made a huge mistake wich could end up in just "0" by getting unlucky with primes. Even the bit I posted below wich at least tries to go for somewhat like anonymous RSA with AES - it's garbage and shouldn't used even for local file encryption.
If you want I could go for way more secure way using BouncyCastle wich provides secure ways to generate and handle local file encryption. For network connections you should go with TLS - wich Java always brings along and needed certificates can easy generated with openssl or with BouncyCastle.