Aviran's Place Forum Index Aviran's Place
The Technology Forum
 
 Home PageHome Page  FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

* Yahoo will purchase Tumblr for $1.1 billion * Continuous Delivery - Part 6 - Backward & Forward Compatibility * Tip: How to remove / disable metro UI windows 8 * Continuous Delivery - Part 5 - Startup - Self Test * Microsoft: Google unfairly using Android to promote Google's products * Continuous Delivery - Part 4 - A/B Testing *

RSA Encryption ...

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Aviran's Place Forum Index -> Developer's Corner
View previous topic :: View next topic  
Author Message
Jack
Level 1
Level 1


Joined: 20 Apr 2009
Posts: 1

PostPosted: Mon Apr 20, 2009 7:23 pm    Post subject: RSA Encryption ... Reply with quote

Hi there ... i need to compile a program which allows user to write a line of message and then encrypts the text and stores it in a file.
another person with the same program is then allowed to decrypt the file and view its contents as it is done through a preshared key using RSA.
Please help ASAP ... Thanks ...

Ive got a base start beloe ...

[code:1]

import java.io.*;
import java.security.*;
import javax.crypto.*;

class enc {

public static void main (String[] args) throws NoSuchAlgorithmException,

InvalidKeyException, IllegalBlockSizeException, NoSuchProviderException,

BadPaddingException, NoSuchPaddingException {

/* Generate a RSA key pair */

KeyPairGenerator keyGen = KeyPairGenerator.getInstance(”RSA”);
SecureRandom random = SecureRandom.getInstance(”SHA1PRNG”, “SUN”);

keyGen.initialize(1024, random);

KeyPair pair = keyGen.generateKeyPair();
PrivateKey priv = pair.getPrivate();
PublicKey pub = pair.getPublic();
//System.out.println(”Public is” + pub);
//System.out.println(”Private is” + priv);

/* Create the cipher */
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

// Initialize the cipher for encryption
rsaCipher.init(Cipher.ENCRYPT_MODE, pub);

// Cleartext
byte[] cleartext = null;
cleartext = “This is Bilal”.getBytes();
//byte[] cleartext = “This is Bilal”.getBytes();
//String cleartext = “This is just an example”;
System.out.println(”the original cleartext is: ” + cleartext.toString());
//System.out.println(”the original cleartext is: ” + cleartext);

// Encrypt the cleartext
byte[] cleartext = null;
cleartext = "This is Bilal".getBytes();
System.out.println(cleartext.toString()); // This will print the Object's reference address
String theString = new String(cleartext);
System.out.println(theString); // This will print clearrext content

// Initialize the same cipher for decryption
rsaCipher.init(Cipher.DECRYPT_MODE, priv);

// Decrypt the ciphertext
byte[] cleartext1 = rsaCipher.doFinal(ciphertext);
//String cleartext1 = rsaCipher.doFinal(ciphertext);
System.out.println(”the final cleartext is: ” + cleartext1.toString());
//System.out.println(”the final cleartext is: ” + cleartext1);
}
}
[/code:1] Sad

Please help asap .. need to do it tonight
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Aviran's Place Forum Index -> Developer's Corner All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group