errors in rsa algorithm.

hi all

i am doing the following program.

//import java.net.*;

import java.io.*;

import java.security.*;

import javax.crypto.*;

class Rsa_Encryption

{

public static void main(String arg[]) throws NoSuchAlgortihmException, InvalidKeyException, IllegalBlockSizeException, NoSuchProviderException,BadPaddingException, noSuchPaddingException

{

KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA");

SecureRandom random = SecureRandom.getInstance("SHA1PRNG","SUN");

keygen.initialize(1024,random);

KeyPair pair = keygen.generateKeyPair();

PrivateKey private = pair.getPrivate();

PublicKey public = pair.getPublic();

System.out.println("Public key :"+public);

System.out.println("Private Key :"+private);

Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCSlpadding");

rsaCipher.init(Cipher.ENCRYPT_MODE,public);

byte[] original = null;

original = "This is Muthukumar".getBytes();

System.out.println("The Original Text is : "+original.toString()+"in Bytes :"+original);

byte[] ciphertext = null;

ciphertext = rsaCipher.doFinal(original);

System.out.println("Cipher Text : "+ciphertext);

rsaCipher.init(Cipher.DECRYPT_MODE,private);

byte[] final=null;

final=rsaCipher.doFinal(ciphertext);

String final1 = final.toString();

System.out.println("The result String is :"+final1);

}

}

it has lot of errors....

C:\>javac Rsa_Encryption.java

Rsa_Encryption.java:17: not a statement

PrivateKey private = pair.getPrivate();

Rsa_Encryption.java:17: ' ; ' expected

PrivateKey private = pair.getPrivate();

Rsa_Encryption.java:17: not a statement

PublicKey public = pair.getPublic();

Rsa_Encryption.java:17: ' ; ' expected

PublicKey public = pair.getPublic();

Rsa_Encryption.java:17: illegal start of expression

System.out.println("Public key :"+public);

Rsa_Encryption.java:17: ' )' expected

System.out.println("Public key :"+public);

Rsa_Encryption.java:17: illegal start of expression

System.out.println("Private key :"+private);

Rsa_Encryption.java:17: ' )' expected

System.out.println("Private key :"+private);

and so on..

please help me as soon as possible...

thanks in advance...

regards,

muthukumar.

[2446 byte] By [amuthukumar712a] at [2007-10-3 4:27:30]
# 1

1) 'public', 'private' and 'final' are reserved words.

2) You have mis-spelled one of the exceptions.

3) This

final=rsaCipher.doFinal(ciphertext);

String final1 = final.toString();

does not give you a String representation of the content of the byte array. If you must have a String representation then use Hex or Base64 encoding. Google for 'Jakarat Commons Codec' for a library to help with this.

4) The SunJCE provider does not provide "RSA/ECB/PKCSlpadding".

5) Having sorted out the above problems, you will probably then find a problem encrypting a message longer than your RSA modululs.

Message was edited by:

sabre150

sabre150a at 2007-7-14 22:30:19 > top of Java-index,Security,Cryptography...