• Post Reply Bookmark Topic Watch Topic
  • New Topic

Facing problem with primitives... pls advice  RSS feed

 
balaji karth
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't need the decimals in you computations, you could also consider using BigInteger.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!