RSA Encryption Algorithm, anyone care to help me on this one?

MrGrim

Golden Member
Oct 20, 1999
1,653
0
0
I say this program on TV, which referred to encryption and I found it really interesting, so I thought I should make a small encryption program to implement all those things I saw and read later.

I want my program to encrypt and decrypt the message using the RSA algorithm. It doesn't have to be anything advanced, I am only doing it for fun. For the past 3 days I have been trying to make the original RSA algorith to work, but I simply can't.

Here is a link to a picture that has the algorithm and here is the only part on the site that eplains it a bit.



<< The RSA algorithm works as follows: take two large primes, p and q, and compute their product n = pq; n is called the modulus. Choose a number, e, less than n and relatively prime to (p-1)(q-1), which means e and (p-1)(q-1) have no common factors except 1. Find another number d such that (ed - 1) is divisible by (p-1)(q-1). The values e and d are called the public and private exponents, respectively. The public key is the pair (n, e); the private key is (n, d). The factors p and q may be destroyed or kept with the private key. >>



I think that the best way to make you see my mistake (which I can't) is to tell how I think it works and what equations I use in my program. So here I go.

First I pick two prime numbers, P and Q. Then I run a loop where I try to find a number E that doesn't have any common factors with (P-1)*(Q-1), ie when E mod A = 0 E mod (P-1)*(Q-1) =/ 0 (2<A<E). If for example I have the prime numbers P=11 and Q=17 I come up with E being 3. Of course there is more than one E, but loop stops when it finds the first number than fullfills all the criteria.

So according to the algorithm, E is part of the public key, of what you tell everyone in order to encrpt their messages and send them to you, and they do so with this equation

C = M^E mod N (C = encrypted message, M= the message you want to encrypt, N = P*Q)

So if say I want to send the message '88' what I do is say, C = 88^3 mod 187 which is 44. Now in order to decrypt the message C I have to use this equation, M = C^D mod N. We need D though. For D though he have two equations, the first one from the picture above and it is

E*D = 1 mod (P-1)*(Q-1) (it's not really '=' but three dashes which means equevalent, I don't know if that makes a big difference, if so please tell me! )

Now, correct if I am wrong but (P-1)*(Q-1) can't be zero because your prime numbers have to be greater than 1 there for 1 mod (P-1)*(Q-1) is always 1! Which leads to the conclution than E*D = 1, ie D = 1/D. I am 100% sure than this is completely wrong because if your secret key was 1 over the public key, then it's not secret! That's why I didn't use this equation in my program, I used this one

(E*D-1) mod (p-1)*(q-1) = 0 (I get this equation from this frase, &quot;Find another number d such that (ed - 1) is divisible by (p-1)(q-1)&quot;) In my example E is 3, P = 11 and Q = 17 so D could be 53. Again D can be a lot of numbers as long as they fullfill the previous criteria, and 53 does, because 3*53+1 = 160 and 160 mod 160 = 0!

So if everything that I have done so far is correct I would be able to encrypt a message and then decrypt it using these two equations


C = M^E mod N
C = 88^3 mod 187 => C = 44

M = C^D mod N
M = 44^53 mod N => M = 176 which is wrong because my message was '88'.


So what exactly am I doing wrong here? Can you spot something? I bet my mistake is so big and obvious that I am going to have to hide.

BTW, on that TV show, I remember seeing this equation M = C^1/e *(Q-1)*(P-1).

I just had a flash. In my program (VB) I use &quot;Long&quot; which is basically big integers, could it be that I should be using Decimals? OMFG of course I do. Still, can you see something else that is wrong with my thinking?

Thanx in advance

[edit]Now that I think about it again, I don't have to use floats do I?[/edit]
 

xtreme2k

Diamond Member
Jun 3, 2000
3,078
0
0
i dont know if i am correct
but look at this
http://www.rsasecurity.com/rsalabs/faq/A-2.html

C = 88^3 mod 187
C doesnt look like it is 44 to me

C - 88^3 needs to be divisible by 187

C = 88^3 mod 187 means
quotation from the link
a = b (mod n)
Given integers a,b, and n with n > 0, we say that a and b are congruent modulo n if a-b is divisible by n, that is, if [(a-b)/( n)] is an integer.
 

MrGrim

Golden Member
Oct 20, 1999
1,653
0
0
Hmmm, that explains a lot but not everything I am afraid. The link you gave me explains what the difference between equevalent and equal is when using mod. If you look at my message I say:

E*D = 1 mod (P-1)*(Q-1) (it's not really '=' but three dashes which means equevalent, I don't know if that makes a big difference, if so please tell me! )

And then I say:

(E*D-1) mod (p-1)*(q-1) = 0 (I get this equation from this frase, &quot;Find another number d such that (ed - 1) is divisible by (p-1)(q-1)&quot In my example E is 3, P = 11 and Q = 17 so D could be 53. Again D can be a lot of numbers as long as they fullfill the previous criteria, and 53 does, because 3*53+1 = 160 and 160 mod 160 = 0!

This doesn't aply when using '=' though, ie a = b mod n is not the same as (a - b) mod n = 0. If it was A is equevalent to B mod N then yes, the answer would be (A - B) mod N = 0.

So C = 88^3 mod 187 is 44. Of course I could be wrong, and just to be sure, I will try the algorithm by replacing equal, with equevalent and see what happens, although I don't expect it to work.

Something else is wrong where I am afraid. You have been great help though, thanx. Everything makes more sence now.
 

MrGrim

Golden Member
Oct 20, 1999
1,653
0
0
I though that was the algorith, in the picture. Do you know something that I don't?
 

superbaby

Senior member
Aug 11, 2000
464
0
0
ARRGGHHHH NOT RSA ENCRYPTION!!!

That stuff hurt my brain in university and I sure as hell don't want to look at it now... GOO AWAY!!!!
 

LocutusX

Diamond Member
Oct 9, 1999
3,061
0
0
Just one tip... the RSA encryption relies on discrete math so it can't be right if you need to use floats or decimals. Just pure integers. Have fun.
 

MrGrim

Golden Member
Oct 20, 1999
1,653
0
0
Yep, after looking at the maths again, I realised that you only need integers too. Couldn't you spot something wrong with the maths or maybe hte usage of the equations?
 

GammaRayX

Senior member
Oct 11, 1999
282
0
0
MrGrim Did you check for &quot;integer overflows&quot;? Because if you just use the basic a^b mod c method, you are going to run into problems, because a^b becomes very large very quickly.

For example 44^53 = 1.2676e87 and when you get large numbers, round off errors are significant. And the modulus values are very dependant on the lower bit values.

You might have to resort to &quot;tricks&quot; to deal with the a^ b mod c equations.

*Update*
Just did 44^53 mod 187 (by hand) and got 176 too.
My numbers were (53 = 110101b)
154*154*55*44 mod 187 = 176

humm, don't know whats wrong.
 

MrGrim

Golden Member
Oct 20, 1999
1,653
0
0
First of all, thanx for all your replies, I didn't expect so many of you to take the time and read my long post.

Now, I did use to get overflows, but I solved it by using &quot;long&quot; instead of integers, at least I think I did.

rahvin that sounds a good idea, but I would still like to sort this out. It has just got to me, you know one of those things, the more problems it gives you the more you want to mess about with it. At the end of the day though, it would be usefull to have the source code, in case I never find out what is wrong with it. Could you plz provide a link?

Thanx again.
 

Mark R

Diamond Member
Oct 9, 1999
8,513
14
81
You've calculated d wrongly.

You've calculated d for ed=159 whereas (ed-1) = 160 [(p-1)(q-1)].

Try again with d = 107. (ed-1) = 320

You might be pleased to know that (44^107) mod 187 = 88.

Helpful hint to avoid overflows: (a^b) mod c = ((a mod c * a) mod c * a) mod c....
 

MrGrim

Golden Member
Oct 20, 1999
1,653
0
0
Mark R you are totally right! Thanx a lot, but I didn't quite understand the workaround. Is this what you mean?

C^D mod N = (C mod N*C) mod N*C) mod N*C) ... and that will go on for D times.

If that is the case, it doesn't work for me. Here is simple example why. Say Q = 5 and P = 7, then E = 5, D = 5 and C = 9. Using your workaround we have that

(((9 mod 35*9) mod 35*9) mod 35*9) ... for 5 times

Now this is always 9 no matter how many times you do mod. You porbably mean something else, but it's 2:00a.m here so I'm sorry if it's obvious, but could you explain it again to me?

Thanx again!
 

Mark R

Diamond Member
Oct 9, 1999
8,513
14
81
Grr. brackets.

Try this:

(((((((((9 mod 35)*9)mod 35)*9)mod 35)*9)mod 35)*9)mod 35

9 mod 35 =9
9 * 9 = 81
81 mod 35 = 11
11 * 9 = 99
99 mod 35 = 29
29 * 9 = 261
261 mod 35 = 16
16 * 9 = 144
144 mod 35 = 4
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |