Encrypt
Hi
I have a cw assignment to create a very simple encryption tool.
The variables are:
p = 53
q = 61
e = 17
T = 123
To encrypt the program shall perform the following maths function:
p * q = pq
(T ^ e) mod pq
==> (123 ^ 17) mod 3233
123 to the power 17 is a hug number, but when mod 3233 is applied, the result is 855.
This is information we are GIVEN. When I calculate this using windows calc,it returns the same result.
I used the following code to attempt to replicate this:
public double Encrypt(int p, int q, int e, int T)
{
int pq = p * q;
double tmp1 = (Math.pow(T,e));
double result = (tmp1) % (pq);
return result;
}
However, when I input the figures into this calc, I get 992
Any ideas why?
I was thinking it is because my value for 123 ^ 17 is simply too large?
[920 byte] By [
xPETEZxa] at [2007-11-26 19:17:56]

The problem is not that t^e is too large, but rather that floating point computation is inherently inaccurate, an din this case causes quite a difference in the answer. Convert T to a BigDecimal, Then use the BigDecimal Methods pow and remainder to do the math, then if you want a double return, just use BigDecimal.doubleValue() to return the result.
~Tim
I am unfamiliar with the BigDecimal Methods, could you link me to a page where there use is described?Thanks alot!
http://www.google.com/search?q=bigdecimal
better yet, use http://en.wikipedia.org/wiki/Modular_exponentiationor use the BigInteger class that already has "modpow"
this should do ur multiplication
N = p.multiply(q);
and for the MOD bit
BigInteger b = a.modPow(e, N);
A is the number that the user is inputing to encrypt...
B is the encrypted number
this will be entered into ur decryption class.
in order for it to be decrypted back to A
hope this help..
Ok, from that link I got this:
public double Encrypt(int p, int q, int e, int T)
{
int pq = p * q;
BigDecimal Encrypt;
Encrypt = BigDecimal.valueOf(T);
Encrypt.pow(e);
//Encrypt BigDecimal.remainder(pq); << this dosent work. Says cannot find remainder. Not sure if it would do what I want any way.
double tmp1 = Encrypt.doubleValue();
double result = (tmp1) % (pq);
return result;
}
The return now = 123....
Am I using BigDecimal incorrectly?
> this should do ur multiplication
This this will work using BigInteger is is not required since the calculation
==> (123 ^ 17) mod 3233
will result in an integer less than 3233. One can do this using
(((123 ^ 8) mod 3233) * ((123 ^ 8) mod 3233) * 123) mod 3233
and (123^8) mod 3233 can be written as
((((((123 ^ 2) mod 3233) ^ 2) mod 3233) ^ 2) mod 3233)
This is relatively easy to generalize.
> double result = (tmp1) % (pq);'double' is not the way to go and is not required.
Thanks for all the replies guys, really appricate it.
However, now I am so confused :(
Should I use BigInterger? BigDecimal?
The method you suggested of breaking down the mulitplication so the value never becomes too large seems overly complicated to me. Seeing as I would have to derive a method to break down my power of value, which would vary if it is odd or even.
I realise double is not the way to go, but how is it not required? if I leave nothing there, it will request a type. If I put anything but double, it returns that double was expected.
What should I put there instead?
> Thanks for all the replies guys, really appricate
> it.
>
> However, now I am so confused :(
>
> Should I use BigInterger? BigDecimal?
Neither - use int.
>
> The method you suggested of breaking down the
> mulitplication so the value never becomes too large
> seems overly complicated to me. Seeing as I would
> have to derive a method to break down my power of
> value, which would vary if it is odd or even.
Fairly simple recursive function.
>
>
> I realise double is not the way to go, but how is it
> not required? if I leave nothing there, it will
> request a type. If I put anything but double, it
> returns that double was expected.
>
> What should I put there instead?
THANKS GUYS!
Managed to get it to work!
This was my final code:
public double Encrypt(int p, int q, int e, int T)
{
int pq = p * q;
BigDecimal pq2 = BigDecimal.valueOf(pq); /
BigDecimal T2 = BigDecimal.valueOf(T);
BigDecimal T3 = T2.pow(e);
BigDecimal R = T3.remainder(pq2);
double result = R.doubleValue();
return result;
}
Apparently the remainder method is NOT a mod method, could this cause me problems later?
ATM, it works for all the numbers I have thrown at it.
I would be surprised if this satisfies the person marking your assignment.
Why is that?Whats wrong with my code?
Nothing really, it's just that instructors can have strange ideas about what are the acceptable solutions. Often it's not allowed to use the standard class library much. So while your solution works with reasonably sized input it still might not fulfill the requirements (of which we know nothing, of course).
> Nothing really, it's just that instructors can have
> strange ideas about what are the acceptable
> solutions. Often it's not allowed to use the standard
> class library much. So while your solution works with
> reasonably sized input it still might not fulfill the
> requirements (of which we know nothing, of course).
Exactly! But also, BigInteger should have been used rather than BigDecimal since one only need whole numbers.
Message was edited by:
sabre150
I attempted to use the BigInteger Code that was given earlier, but I could not get it to work.
If I put the inputs to "BigInteger P" and then I input a value for P, it always gave the error "Big Integer expected" :(
Thanks for all the help guys! I am gonna show my code to the lecture on Tuesday!
> If I put the inputs to "BigInteger P" and then I
> input a value for P, it always gave the error "Big
> Integer expected" :(
>
What kind of "input" are you talking about?
String?
BigInteger p = new BigInteger("1234567890123456789");
int or long?
BigInteger p = BigInteger.valueOf(53);
The BigInteger class has a modPow method that efficiently calculates powers of an integer modulo another much more efficiently than first calculating the power and then applying the modulus.
hi i;m also tryin to inplement a similar example..where i want the user to input the variable. p = new BigInteger ("255");how would i make the 255 a variable, that the user can input?
blah (int a)p = new BigInteger("a");Where blah is your method name.
Apparently this is continued at: http://forum.java.sun.com/thread.jspa?threadID=5149585It's rude to open multiple topics about the same thing.