Hi Barry,
I'm a total ruby-nuby, so ther's not much ruby content here.
In the RSA algorithm, you need to be able to evaluate a**b mod n
using integer arithmetic only.
(Note : "mod n" means "remainder after division by n". )
The easiest way to describe the standard method is by a
somewhat contrived example.
Let's say you want to calculate A=(25800**100 mod 257)
First, note that 100 base 10 = 1100100 base 2,
so that 100**100 = (100**64) * (100**32) * (100**4)
Now calculate,
25800**1 mod 257 = 100
100**2 mod 257 = 100*100 mod 257 = 235
100**4 mod 257 = 235*235 mod 257 = 15
100**8 mod 257 = 15*15 mod 257 = 225
100**16 mod 257 = 225*225 mod 257 = 253
100**32 mod 257 = 253*253 mod 257 = 16
100**64 mod 257 = 16 * 16 mod 257 = 256
Then,
A = ((256 * 16) mod 257) * (15 mod 257)
A= ( 241 * 15) mod 257
So, the answer is A=17
You can see that that no calculation above requires more
than 5 digits (257**2 = 66049)
Specialized languages like Wolfram's Mathematica handle this sort of
calculation very efficiently for large values of a,b and n.
I hope this is useful to you.
Regards,
Tad
(Embedded image moved to file: pic29358.pcx)
Internet
barry@angleinc.com - 27/08/2004 23:07
Veuillez répondre à ruby-talk@ruby-lang.org
Pour : ruby-talk
cc :
Continuing on the track of the previous question about ASCII, I'm using
each_byte to change a string to ASCII for the purpose of encrypting it,
using the theory of the RSA algorithm ( to see if I can! ). Part of
this algorithm requires getting a power ( a**b ) in which the "a" is the
ASCII value of the string and the "b" is some large number that is
dictated by the RSA algorithm ( about 25,000,000,000,000 for my test
string ). However, when I do this I get the error
NaN
test_ascii.rbw:20: warning: in a**b, b may be too big
While I could simulate a power with a loop, doing this 25 trillion times
might take a little while. I could also simulate the power with logs,
but I'm sure that this would add inaccuracies and the integral number
has to be exact. Is there a way out or is this just a fundamental
limitation of Bignums in Ruby?
Barry
This message and any attachments (the "message") is
intended solely for the addressees and is confidential.
If you receive this message in error, please delete it and
immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not
therefore be liable for the message if modified.
pic29358.pcx (2.01 KB)
···
Objet : ASCII, RSA AND BIGNUM
---------------------------------------------
Ce message et toutes les pieces jointes (ci-apres le
"message") sont etablis a l'intention exclusive de ses
destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir
immediatement l'expediteur. Toute utilisation de ce
message non conforme a sa destination, toute diffusion
ou toute publication, totale ou partielle, est interdite, sauf
autorisation expresse. L'internet ne permettant pas
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce
message, dans l'hypothese ou il aurait ete modifie.