aspose file tools
The moose likes Beginning Java and the fly likes Facing problem with primitives... pls advice Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Facing problem with primitives... pls advice " Watch "Facing problem with primitives... pls advice " New topic
Author

Facing problem with primitives... pls advice

balaji karth
Greenhorn

Joined: Jan 27, 2006
Posts: 8
Hi all..
Am facing a problem with the primitive types ..

Am trying to develop a mini program on implementing RSA Algorithm to encrypt and decrypt files. I hav mentioned the steps what i did ..

1. I read the file content into a byte array and get those int values and do RSA encryption and get a key .
2. Now on the decryption part am using this key and generate the above int value and bring it back to char.

eg: 6 -> the ascii value to be encrypted

int encryptVal = (int)Math.pow(6,7) % 119;
int decryptVal = (int)Math.pow(encryptVal ,77)%119;

but the decryptVal is not showing as 6 .. when i get it as double am getting different values ..
I dont know where its going wrong .. pls help me out

Thanks,
Balaji
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Math.pow(48, 7) is a double with a value of 5.87068342272E11, which is beyond the range of an int. In fact, casting this to type int...

(int)Math.pow(48,7) is 2147483647, which you might recognize as Integer.MAX_VALUE.

Now, applying the modulus, Math.pow(48, 7) % 119 is 5.87068342272E11 % 119, which is a double 6.0. Casting this result to type int...

(int)(Math.pow(48, 7) % 119) is (int)(5.87068342272E11 % 119), which is 6. But note that this is not the same as casting the double 5.87068342272E11 to int 2147483647 and then applying the modulus...

(int)Math.pow(48,7) % 119 is 2147483647 % 119, which is 8.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Also...

I would be remiss if I didn't mention the risk of losing precision with floating-point arithmetic. See Some Things You Should Know About Floating Point Arithmetic.

If you are performing floating-point arithmetic that demands precision, then you should consider using BigDecimal instances rather than primitive float or double types.
Dan Fowler
Greenhorn

Joined: Jan 11, 2007
Posts: 3
If you don't need the decimals in you computations, you could also consider using BigInteger.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Facing problem with primitives... pls advice
 
Similar Threads
Decryption of file is taking too much time.
problem in XOR of private key using public key....
HashMap problem
getting null parameters in post
Cipher problem with JRE 6 update 22