Click here to Skip to main content
Click here to Skip to main content

RSA encryption/decryption

By , 5 May 2003
 

Introduction

Note: Please refer rsa.doc for more details on code and software. I can be contacted at bhaskar_bora@yahoo.com for further queries. Implementation includes private and public key (32-bit) generation and support of text file encryption and decryption. This document contains the algorithms and information required to design and implement RSA algorithm in VC++. This software is only for text data encryption and decryption. Document also contains the software usage guidelines and screen-shots.

Intro to RSA

RSA algorithm is mainly a public key encryption technique used widely in network communication like in Virtual Private Networks (VPNs). In public key encryption technique, a key is split into two keys and they are called as public and private keys. Public key is advertised to the world and private key is kept secret. It is not possible to generate private key using the public key. So, someone who knows the public key cannot decrypt a message after it has been encrypted using the public key.

RSA algorithm is a block cipher technique in which plain text and cipher text are integers between ‘0’ and ‘n-1’ from some ‘n’. In RSA algorithm encryption and decryption are of following form, for some plain text M and cipher text C:

C = M^e mod n

M = C^d mod n

Both sender and receiver must know the value of ‘n’. The sender knows the value of ‘e’ and only receiver knows the value of ‘d’. Thus, this is a public-key encryption algorithm with a public key of KU={e, n} and private key of KR={d, n}. For the algorithm to be satisfactory for public-key encryption, the following requirement must be met

  1. It is possible to find values of e, d, n such that M^ed = M mod n for all M<n.
  2. It is relatively easy to calculate M^e and C^d for all values of M<n.
  3. It is infeasible to determine d given e and n.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

BhaskarBora
Web Developer
India India
Member
Working with EMC Corp, Bangalore (India) in distributed embedded software design. Like to read and code in storage, computer security and language design areas.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionUnicodes in encrypted files.memberNIKHIL788 Agrawal4 Dec '12 - 7:02 
When i use this code to encrypt my data from a text file, the data inside a .enc file is in unicode format like some special characters, here is an example for the same like €X@ . Can you explain why its happening .
 
Thanks,
Nikhil.
Questionhelpmembersteven stevanus23 Feb '12 - 17:01 
for metode encryp and decryp with RSA.
Questionhelpmembersteven stevanus23 Feb '12 - 17:00 
can u give me, source code for visual studio c#.net 2005.
Questionis your software Encrypt/Decrypt the text by 128 bit keymemberRaj.Amit29 Jul '10 - 21:51 
is your software Encrypt/Decrypt the text by 128 bit key
GeneralMy vote of 1memberKarstenK6 Jun '10 - 21:26 
it is only an introduction
Questionhow to run it in VS 2008?memberMember 15130995 Apr '10 - 12:53 
hi,
 
first ,thanks for this work.
 
i compiled it using Visual c++ 6,and it it works well.
 
but when i tried to run it using visual studio 2008,and it asked me to convert to the
current format and then some errors appered when i tried to comile it
"error C2593: 'operator =' is ambiguous ec1assignmentdlg.cpp line 218"
 
if(str[0] == 0x0A)
winStr = 0x0D;
 

any ideas about this?
 
thanks
GeneralThanks need helpmembertnla9 Nov '09 - 8:27 
have u any information about NTRU public key Smile | :)
GeneralMy vote of 1memberWalxer2 Nov '09 - 9:44 
test
Generaldecrpter for data in SHA1/RSA format.mdb filememberoviis_4u3 May '09 - 4:40 
want to decrypt data in .mdb which is in a format of SHA1/RSA..
GeneralRe: decrpter for data in SHA1/RSA format.mdb filememberhebbiche26 May '09 - 11:17 
Mad | :mad: Dead | X| Suspicious | :suss: Cool | :cool: Thumbs Up | :thumbsup: Thumbs Down | :thumbsdown: Thumbs Up | :thumbsup:
GeneralMy vote of 1memberKirk211221 Apr '09 - 8:54 
Inadequate text and a project that will not compile on first attempt.
GeneralNeed help !memberHackmahesh4 Apr '09 - 7:52 
Below is UdpClient Server program in which client make a request to add to no and sent it to the server and server give reply !but the reply is not coming instead exception occurs!
if get the solution plz reply on "patel.mahesh034@gmail.com ";) 
 

 

/**
* @(#)UdpClient.java
*
*
* @author HackMahesh
* @version 1.00 2009/3/18
*/
 
import java.io.*;
import java.net.*;
public class UdpClient {
 
public UdpClient() {
}
public static void main(String args[]) throws Exception
{
BufferedReader infromuser=new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket=new DatagramSocket();
InetAddress IPAddress=InetAddress.getByName("localhost");
byte[] receiveData=new byte[1024];
String sentence=infromuser.readLine();
byte[] sendData=sentence.getBytes();
DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IPAddress,9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence=new String(receivePacket.getData());
System.out.println("from server:"+modifiedSentence);
clientSocket.close();
}

}

/**
* @(#)UdpServer.java
*
*
* @author HackMahesh
* @version 1.00 2009/3/18
*/
 
import java.io.*;
import java.net.*;
public class UdpServer
{ String addition(String sentence)
{ int len=sentence.length();
int add=sentence.indexOf('+');
String first=sentence.substring(0,add);
String last=sentence.substring(add+1,len);
int a=Integer.parseInt(first);
int b=Integer.parseInt(last);
int c=a+b;
return(String.valueOf(c));
}
 

public static void main(String args[]) throws Exception
{ UdpServer UDP=new UdpServer();
DatagramSocket serverSocket=new DatagramSocket(9876);
byte[] receiveData=new byte[1024];
byte[] sendData=new byte[1024];
while(true)
{DatagramPacket receivePacket=new DatagramPacket(receiveData,receiveData.length);
serverSocket.receive(receivePacket);
String sentence=new String(receivePacket.getData());
System.out.println("received: "+sentence);

InetAddress IPAddress=receivePacket.getAddress();
int port=receivePacket.getPort();
String capitalizedSentence=UDP.addition(sentence);
sendData=capitalizedSentence.getBytes();
DatagramPacket sendPacket=new DatagramPacket(sendData,sendData.length,IPAddress,port);
serverSocket.send(sendPacket);


}
}

}
 


 

GeneralMy vote of 1memberZinkyu22 Dec '08 - 9:10 
Article provides very little detail, referring to the included source code rather than explain anything the article might be about. Lack of any code in the article is also a deterrent.
Questionkamine kutte ye kya tha?????????memberanubhavkapuuri28 Nov '08 - 3:31 
exam se pehle aisa tortore..sharam nahi aate...engineer bana ki nahi..???besharam..baddimag..mar ja..kyo maa baap ka paisa dubo raha hai..desh be bojh...jai hind jai bharat
Generali can't run program!membernguyenvanchinh1214 Nov '08 - 23:37 
i can't run program!I use VS 2008!please help me!
QuestionHOW TO HANDEL 128 BIT KEY GENARETIONmembertandra islam21 Apr '08 - 7:56 
HOW I CREATE A CODE FOR 128 BIT KEY.
QuestionHow to handle 521 bit encryption/decryptionmemberYiping Zou21 Jun '04 - 16:01 
I have an assignment: I was given an encrypted file( was encrypted using 512 bit RSA), and a private key pair {D, N)-- 64 bytes hex ascii. both D and N are Big Endiand format. I need to write a program to decrypt the encrypted data. I saw your article and source code which implement the encryption and decryption by 32 bit. I do not know how to do with 512 bit. As I cannot simply use the mod function to calculate by using 64 bytes data.
 
Do you have any idea to help me?
 
Yiping
GeneralDoesn't work for N less than the biggest ASCII code, which is Your defaultmemberMaxim Violin31 Jan '04 - 0:44 
Your program doesn't work for default prime numbers, which are 7 and 17. It happens, because (according to theory), the chunks of data, which are encrypted must be less than prime1 x prime2 = N. Otherwise it can not work. You should break the text to be encrypted to smaller chunks. If You didn't want to bother yourself, then at least it would be nice of You to warn people to select prime numbers correspondingly.
 
Max
GeneralRe: Doesn't work for N less than the biggest ASCII code, which is Your defaultmemberBhaskarBora13 Dec '04 - 22:53 
This is a RSA tutor software. It demonstrate working fo RSA and also RSA failure conditions can be checked.
 
___________________________
When a thing is new, people say, "It's not true." Later, when its truth becomes obvious, people say, "It's not important." Finally, when its importance cannot be denied, people say, "Well, it's not new."
GeneralRe: Doesn't work for N less than the biggest ASCII code, which is Your defaultmemberbygreencn10 Dec '06 - 16:48 
Big Grin | :-D Big Grin | :-D Big Grin | :-D
Great work!!!!
Mad | :mad: Mad | :mad: Mad | :mad:
But your program doesn't work for default prime numbers, which are 7 and 17. If it could not work how does it demonstrate working fo RSA? We all hope that you could fix it.
QuestionUsing linear search to check if a number is prime or not?memberMustafa Demirhan6 May '03 - 8:39 
Man, this will not work in real world. You cannot check a 100 digit number using that way. It will take forever...
 
Also, using rand () function to generate a prime number is not a good idea too. The main reason is that it is predictable and it is better to use a cryptographic random number generator. Also in you implementation you just get the value of the random number generator. This mean that your key will most probably be smaller than 32 bits...
 
Another important thing: The two prime number that you generate should NOT be close to each other. For example, if you use 100 digit number, it is a good idea to use one 99 and one 100 digit prime numbers. If the value of those prime number are close to each other, there is a very good reverse engineering mechanism that will allow people to find both your prime numbers very fastly.
 
Anyway, I think your implementation of RSA is just a school homework. This is not even close to a real implementation and should not be used in practice (even with large numbers). In fact it will take more than a million years to generate a random number with your method Poke tongue | ;-P
 
Mustafa Demirhan
http://www.macroangel.com
Sonork ID 100.9935:zoltrix

They say I'm lazy but it takes all my time
GeneralRe: 32 Bits!!!memberNiklas Lindquist6 May '03 - 2:14 
Actually 32 bits is about 4 billion numbers and can be cracked in a matter of seconds.
GeneralRe: 32 Bits!!!memberJörgen Sigvardsson6 May '03 - 6:06 
HEh, 10 seconds with pen and paper. Big Grin | :-D
 
--
Run around in the radiation
Run around in the acid rain,
On a black, black planet

GeneralRe: 32 Bits!!!memberKastellanos Nikos6 May '03 - 6:19 
You miss the point.

The author avoid to use big-Integers to keep it simple.
He just concetrate on explaning the RSA algorithm and provides a basic implemetnation.
 

 


 
- - - - - - - - - - - - - - - - - -
Memory leaks is the price we pay \0
01234567890123456789012345678901234
GeneralRe: 32 Bits!!!memberAntony Michael Kancidrowski12 May '03 - 23:25 
If I recall 1024bit prime numbers will ensure that you wouldnt care if the code was broken as we would have long since passed away.
 
Ant.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 6 May 2003
Article Copyright 2003 by BhaskarBora
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid