Message Boards Message Boards

Fix code that does RSA decryption?

Posted 8 years ago

A few days ago I started working on a method to decrypt a text with a given number phi(N). For that, I need to create a list of possible (e,d). I can calculate e and d (in this example d=b and z=e, e is the list with (e,d)) with

If[CoprimeQ[phi, z],
{y, {a, b}} = ExtendedGCD[phi, z]; 
If[b > 0, e = Append[e, {z, b}]]
]

where phi and e have to be coprime and 1 < e < phi. Now instead of iterating this in the first place over all possible z (which don't have to be prime but uneven), I want to create a small number of (e,d). This number can vary and depends on phi. Now if I test a few z, not all of the z will give me another (e,d) because not all z are coprime to phi and not all b are positive.

But in order to guarantee that i always get just one more (e,d) i wrote a little code, which doesn't seem to work and I just don't know why.

e = {}; z = 0; v = 0;
While[Length[e] <= v,
v = Length[e];
If[CoprimeQ[phi, z], {y, {a, b}} = ExtendedGCD[phi, z]; 
If[b > 0, e = Append[e, {z, b}]]];
z++
]

v is the old length of the list e and should be compared to the new length. If the new length is smaller or equal to v, the algorithm should start over again until the new length is bigger than v, the old length.

Now for some reason v is always equal to 0. But Length[e] is not! Can someone tell me what happens here?

POSTED BY: Jens Burkhart
3 Replies

Your problem is that the v=Length[e] is in the While loop -- it needs to be outside -- the loop never runs a second time the way you have it. Do this (this is after 3 iterations with phi = 61):

v = Length[e];
While[Length[e] <= v,
 If[CoprimeQ[phi, z], {y, {a, b}} = ExtendedGCD[phi, z];
  If[b > 0, e = Append[e, {z, b}]]];
 z++; Print["z = ", z, "; v=", v, "; e = ", e]]

z = 10; v=2; e = {{1,1},{8,23}}

z = 11; v=2; e = {{1,1},{8,23}}

z = 12; v=2; e = {{1,1},{8,23}}

z = 13; v=2; e = {{1,1},{8,23}}

z = 14; v=2; e = {{1,1},{8,23}}

z = 15; v=2; e = {{1,1},{8,23}}

z = 16; v=2; e = {{1,1},{8,23}}

z = 17; v=2; e = {{1,1},{8,23}}

z = 18; v=2; e = {{1,1},{8,23},{17,18}}
POSTED BY: Neil Singer

as soon as e gets assigned its first value, the iteration stops (Length[e]=1, v=0, Length[e] is not <=v, so e will have one element and v will be zero.

This seems to work as programmed, I am not sure I understand what you want to happen (so I can suggest a fix). Do you want the list to be rest to {}? If so, its not in the loop so it will not be.

Try this -- it does the same thing:

e = {}; z = 0; v = 0;
While[Length[e] <= v, v = Length[e];
 If[z == 5, e = {{1, 2}}];
 z++; Print["z = ", z, "; v=", v, "; e = ", e]]

z = 1; v=0; e = {}

z = 2; v=0; e = {}

z = 3; v=0; e = {}

z = 4; v=0; e = {}

z = 5; v=0; e = {}

z = 6; v=0; e = {{1,2}}
POSTED BY: Neil Singer
Posted 8 years ago

Yeah basically I want this function to iterate and stop if an element got added to the list. This works the first time but does not after that. v gets set to 0 for some reason.

POSTED BY: Jens Burkhart
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract