Sunteți pe pagina 1din 3

3/8/13

Share 1

Just go for it!!: RSA code in Java


More Next Blog Create Blog Sign In

Just go for it!!


We are the youths,we have an immense power to direct our future..so myself as one among you who has so many dreams don't wanna just dream but make it real!So an awakening call to the youths..please join hands with me to live for others and JUST GO FOR IT!! Believe me its fun!!
powered by

Apple

Fox News (blog) - Why Apple is ailing

Saturday, April 28, 2012

RSA code in Java


To encrypt and decrypt a string using RSA algorithm
The following code helps the user to provide a string for encryption and decryption using RSA algorithm with the help of Big Integer in java. Source code for RSA.java: import java.math.BigInteger; import java.util.Random; import java.io.*;

Translate
Select Language

Followers
Join this site
w ith Google Friend Connect

Members (11)

public class RSA { private BigInteger p; private BigInteger q; private BigInteger N; private BigInteger phi; private BigInteger e; private BigInteger d; private int bitlength = 1024; private int blocksize = 256; //blocksize in byte private Random r; public RSA() { r = new Random(); p = BigInteger.probablePrime(bitlength, r); q = BigInteger.probablePrime(bitlength, r); N = p.multiply(q); phi = p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE)); e = BigInteger.probablePrime(bitlength/2, r); while (phi.gcd(e).compareTo(BigInteger.ONE) > 0 && e.compareTo(phi) < 0 ) { e.add(BigInteger.ONE); } d = e.modInverse(phi); } public RSA(BigInteger e, BigInteger d, BigInteger N) { this.e = e; this.d = d; this.N = N; } public static void main (String[] args) throws IOException { RSA rsa = new RSA(); DataInputStream in=new DataInputStream(System.in); String teststring ; System.out.println("Enter the plain text:"); teststring=in.readLine(); System.out.println("Encrypting String: " + teststring); System.out.println("String in Bytes: " + bytesToString(teststring.getBytes())); // encrypt byte[] encrypted = rsa.encrypt(teststring.getBytes()); System.out.println("Encrypted String in Bytes: " + bytesToString(encrypted)); // decrypt byte[] decrypted = rsa.decrypt(encrypted);

Already a member? Sign in

Subscribe To

Posts Comments
Popular Posts

RSA code in Java To encrypt and decrypt a string using RSA algorithm The following code helps the user to provide a string for encryption and decry... Round Robin Process Scheduling Algorithm in C++ Round Robin Process Scheduling Algorithm in C++ Here is my own written code in C++ for Round Robin process scheduling algorit... Liang-Barsky line clipping program in c++ Liang-Barsky line clipping program in c++ This is my own code in c++ for LiangBarsky line clipping algorithm : Source code: #inclu... COHEN SUTHERLAND line clipping code in C++ COHEN SUTHERLAND line clipping code in C++ Here is my own generated code for
1/3

whereskyisnotthelimit.blogspot.in/2012/04/rsa-code-in-java.html

3/8/13

Just go for it!!: RSA code in Java


System.out.println("Decrypted String in Bytes: " + bytesToString(decrypted)); System.out.println("Decrypted String: " + new String(decrypted)); } private static String bytesToString(byte[] encrypted) { String test = ""; for (byte b : encrypted) { test += Byte.toString(b); } return test; } public byte[] encrypt(byte[] message) { return (new BigInteger(message)).modPow(e, N).toByteArray(); } public byte[] decrypt(byte[] message) { return (new BigInteger(message)).modPow(d, N).toByteArray(); } } OUTPUT:

Cohen Sutherland line clipping algorithm in C++.Hope its us... C++ Code for generating Sierpinski gasket (triangle) C++ Code for generating Sierpinski gasket (triangle) My code for generating fractal using C++. Programming fractals is very interest... C++ CODE FOR POLYGON CLIPPING C++ CODE FOR POLYGON CLIPPING So here's my C++ code for Polygon Clipping.This program can clip ant polygon except recta... DDA algorithm using DOSBox 0.74 Hello friends!!Here is a code for DDA line drawing algorithm which prints the line pixel by pixel.The code is written in C++ and executed u... FLOOD FILL ALGORITHM FLOOD FILL ALGORITHM This my own code for polygon filling using the flood dill algorithm. Hope you enjoy this! Source code: #inclu...

Posted by Sonali Dinkar at 12:45 AM Reactions:


funny (0) interesting (1) cool (0)

+1 Recommend this on Google

5 comments: krish October 7, 2012 at 8:04 AM sonali what if i want to encrypt a file... Reply Replies Sonali Dinkar October 8, 2012 at 10:39 AM

Bresenham line drawing algorithm Bresenham line drawing algorithm My code for Bresenham line drawing algorithm which will help you to learn how the algorithm actua... Connecting PCs in LAN Local Area Network Connection This is what I learn t and explored during my project training in BARC..so very eager to share it wit...
About Me

I haven't tried programming on that but try this link..i think this will solve ur problem.. http://www.c.happycodings.com/File_Operations/code2.html

Sonali Dinkar

January 8, 2013 at 8:07 AM

Sonali Dinkar View my complete profile


Blog Archive

also u can visit this site: http://www.flexiprovider.de/examples/ExampleRSA.html Reply

Verdy Yanto January 5, 2013 at 12:22 AM i want to ask why if i decrease a bit length the decryption text it can be symbol? not a text? Reply Replies Sonali Dinkar January 8, 2013 at 8:25 AM

2013 (3) 2012 (52) October (1) September (4) August (7) July (4)
2/3

I didnt get your doubt..if you mean that the decrypted text on reducing the bit length

whereskyisnotthelimit.blogspot.in/2012/04/rsa-code-in-java.html

3/8/13

Just go for it!!: RSA code in Java


is a symbol..then it is not so..Whatever text you provide as plain text is decrypted and returned! If not clear then do ask...thank you. Reply

May (4) April (6) Just go for it!!: Primitive chat program using XML... Primitive chat program using XMLRPC Just go for it!!: RSA code in Java RSA code in Java Just go for it!!: Remove hyperlinks from a word fi... Remove hyperlinks from a word file at a time! March (12) February (6)

E n t e ry o u rc o m m e n t . . .

Comment as: Google Account Publish Preview

Newer Post
Subscribe to: Post Comments (Atom)

Home

Older Post

January (8)

Travel template. Template images by Silberkorn. Powered by Blogger.

whereskyisnotthelimit.blogspot.in/2012/04/rsa-code-in-java.html

3/3

S-ar putea să vă placă și