Jack Level 1

Joined: 20 Apr 2009 Posts: 1
|
Posted: Mon Apr 20, 2009 7:23 pm Post subject: RSA Encryption ... |
|
|
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]
Please help asap .. need to do it tonight |
|