Want to make an encryption program!

Pages: 12
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.
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.
closed account (ETAkoG1T)
thanks for advice but I just want the formula written out in non-wikipedia non-mathexpert way. :)
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
blah blah... use GNU GMP... blah blah... http://primes.utm.edu/lists/
is this why the us government pays you for high primes??
>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.
@ 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.
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.
@Resident true, there are many good libraries out there for most things, but sometimes people want to code something for educational purposes.
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.
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.
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
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
e is a prime number that m is not divisible by.
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
1, 109, 217, and 325 are 1 + nm where n is 0, 1, 2, and 3 respectively, and 5 is e.
closed account (ETAkoG1T)
Thank you now i am ready to make the program :)
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
Pages: 12