Want to make an encryption program!

Pages: 12
Feb 17, 2013 at 9:25am
closed account (ETAkoG1T)
I want to make an encryption program that works like the banks(RSA), there is a public key and a private key to unlock the message after it is encrypted. The problem is that I am not very good at math and can't make sense of the tutorials on this out there. Could someone please explain the process in making the program and show some code samples to make it easier to understand. Please use small numbers to make the process easier to follow. Thank You in advance.
Feb 17, 2013 at 9:52am
http://en.wikipedia.org/wiki/RSA_%28algorithm%29

It boils down to finding two huge prime numbers, and doing math on the M (message) you're trying to encrypt, as if it was a number.

Could someone please explain the process in making the program and show some code samples to make it easier to understand.

I have a suggestion: try writing the program yourself, then when you get stuck post in General or Beginners for help. And if you're successful, you should consider publishing your program as an Article.
Feb 17, 2013 at 10:23am
closed account (ETAkoG1T)
thanks for advice but I just want the formula written out in non-wikipedia non-mathexpert way. :)
Feb 17, 2013 at 11:37am
I downloaded a cryptography for dummies type pdf, I could send you if your interested or you could find it youself on a peer to peer thing havnt read it yet but I had a little fun scrambling code with an algorithm...i effed up at first tho... modulus and other things infinatley obfuscatify of whatever...havn't read it yet :D
Feb 17, 2013 at 11:57am
blah blah... use GNU GMP... blah blah... http://primes.utm.edu/lists/
Feb 17, 2013 at 1:09pm
is this why the us government pays you for high primes??
Feb 17, 2013 at 3:01pm
>Want to write encryption program.
>Not good at math.

Can't have both. Modern encryption is quite math intensive, and the people that write such things generally have degrees in math.
Feb 17, 2013 at 3:36pm
@ Resident I highly doubt you need a math degree just to write an encryption program. Maybe if you wanted to write your own algorithm you would need a degree.

@OP The best thing you can do is read the article, and every term you do not understand you then research , continue recursively until you understand the whole article.
Feb 17, 2013 at 3:44pm
Then why reinvent the wheel? There's tons of free libraries out there that implement the RSA algorithm just fine. And not having any understanding of the math behind it is likely going to cause you problems when trying to implement it.
Feb 17, 2013 at 3:46pm
@Resident true, there are many good libraries out there for most things, but sometimes people want to code something for educational purposes.
Feb 17, 2013 at 3:47pm
If it's for educational purposes, you'll need to understand the math behind it to actually gain anything. Otherwise you're just copying stuff from the Internet and not learning anything.
Feb 17, 2013 at 3:52pm
True, copying code often does not allow learning, but studying of that code can provide great insight and understanding. Plus the OP is mostly asking for help understanding the math, not for actual code.
Feb 18, 2013 at 4:52am
closed account (ETAkoG1T)
Can someone just explain the RSA algorithm with actual numbers? I don't have much problems with math, but I don't understand what is what, for example I don't know what numbers to choose as my key. Can I use two primes like 5 and 7? 5*7 = 35 simple enough, 35 is my public key, what do I do with the public key after that? Use some simple examples, I just want to understand the logic behind it, the coding won't be that hard when I understand it.
Last edited on Feb 18, 2013 at 4:53am
Feb 18, 2013 at 5:19am
Feb 18, 2013 at 6:01am
closed account (ETAkoG1T)
naruko can you explain step 4) ? I don't understand how he came up with 5 as e? Is it the first number that does not have a common divisor with m above one?

It is a very nice website for learning to make an encryption program, thank you for the link.
Last edited on Feb 18, 2013 at 6:06am
Feb 18, 2013 at 6:39am
e is a prime number that m is not divisible by.
Feb 18, 2013 at 6:44am
closed account (ETAkoG1T)
ok that makes sense, and for 5) were does the numbers come from?
n = 0 => d = 1 / 5 (no)
n = 1 => d = 109 / 5 (no)
n = 2 => d = 217 / 5 (no)
n = 3 => d = 325 / 5

were does 1, 109, 217 and 325 come from?
Last edited on Feb 18, 2013 at 6:46am
Feb 18, 2013 at 6:58am
1, 109, 217, and 325 are 1 + nm where n is 0, 1, 2, and 3 respectively, and 5 is e.
Feb 18, 2013 at 7:12am
closed account (ETAkoG1T)
Thank you now i am ready to make the program :)
Feb 18, 2013 at 8:26am
closed account (ETAkoG1T)
>Want to write encryption program.
>Not good at math.

Can't have both. Modern encryption is quite math intensive, and the people that write such things generally have degrees in math.


Math degree? That is why RSA encryption shines, its easy to use yet hard to crack. Everyone(interested in encryption) knows the mechanics but prime factoring high numbers takes so much computer power that it becomes a reliant method. To prove that anyone at any stupidity level can write a simple encryption program I made a program myself, based on the example from naruko's link.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51

#include <iostream>
#include "stdafx.h"

long long encrypt(long long msg, long long key_n, int key_e);
long long decrypt(long long encrypted_msg, long long secret_n, long long secret_d);

int main()
{

	int decrypted = decrypt(encrypt('a', 133, 5), 133, 65);
	std::cout << "encrypted: " << encrypt('a', 133, 5);
	std::cout << "\ndecrypted, " <<char(decrypted) << ", int value " << decrypted;

	std::cin.get();
	std::cin.get();
	return 0;
}

long long encrypt(long long msg, long long key_n, int key_e)
{
    long long encrypted_msg = msg;
	for(int i = 1; i < key_e; i++)
		encrypted_msg *= msg;
	encrypted_msg = encrypted_msg % key_n;

	return encrypted_msg;
}

long long decrypt(long long encrypted_msg, long long secret_n, long long secret_d)
{

	long long C = 1;
	bool times_msg = false;
	if(!(secret_d % 2 == 0))
	{
		times_msg = true;
		secret_d--;
		C = encrypted_msg;
	}
	for(; 1 != secret_d / 2;)
	{
		encrypted_msg *= encrypted_msg;
		encrypted_msg %= secret_n;
		secret_d /= 2;
	}
	long long decrypted = (encrypted_msg * encrypted_msg) % secret_n;
	decrypted = (C * decrypted) % secret_n;
	return decrypted;
}
Last edited on Feb 18, 2013 at 8:27am
Pages: 12