Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help me...  RSS feed

 
Kumaran Sowrirajan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends
I am new to JCE.I am doing the simple program to encrypt and decrypt the string, but while i compiling i am getting these errors.
Program :-
import javax.crypto.cipher;
import javax.crypto.KeyGenerator;
import java.security.Key;
import java.security.Security;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyEception;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException;
public class encryptTest
{
public static void main(String args[])
{
String password = "HelloWorld";
Security.addProvider(new com.sun.crypto.provider.SunJCE());
try
{
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
Cipher cipher = Cipher.getInstance("DES");
byte[] plain_password = password.getBytes();
String str_plain_password = new String(plain_password);
System.out.println("Plain Password = " + str_plain_password);
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] encrypted_password = cipher.doFinal(plain_password);
String str_encrypted_password = new String(encrypted_password);
System.out.println("Encrypted Password = " + str_encrypted_password);
cipher.init(Cipher.DECRYPT_CODE,key);
byte[] decrypted_password = cipher.doFinal(encrypted_password);
String str_decrypted_password = new String(decrypted_password);
System.out.println("Decrypted Password = " + str_decrypted_password);
}
catch(NoSuchAlgorithmException nsae)
{
System.out.println("No Such Algorithm Exception " + nsae.getMessage());
}
catch(NoSuchPaddingException nspe)
{
System.out.println("No Such Padding Exception " + nspe.getMessage());
}
catch(InvalidKeyException ike)
{
System.out.println("Invalid Key Exception " + ike.getMessage());
}
catch(IllegalStateException ise)
{
System.out.println("Illegal State Exception " + ise.getMessage());
}
catch(IllegalBlockSizeException ibse)
{
System.out.println("Illegal Block Size Exception " + ibse.getMessage());
}
catch(BadPaddingException bpe)
{
System.out.println("Bad Padding Exception " + bpe.getMessage());
}
}
}

Error :- import javax.crypto.cipher; ^ encryptTest.java:2: Class javax.crypto.KeyGenerator not found in import.
import javax.crypto.KeyGenerator; ^ encryptTest.java:6: Class java.security.InvalidKeyEception not found in import.
import java.security.InvalidKeyEception; ^ encryptTest.java:7: Class javax.crypto.NoSuchPaddingException not found in import.
import javax.crypto.NoSuchPaddingException; ^ encryptTest.java:8: Class javax.crypto.IllegalBlockSizeException not found in import.
import javax.crypto.IllegalBlockSizeException; ^ encryptTest.java:9: Class javax.crypto.BadPaddingException not found in import.
import javax.crypto.BadPaddingException;
I think this is mainly because of ClassPath problem.Can you please tell me what i need to do to rectify this error?.
Thanks
Kumaar.S
 
Matthew Webster
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct, you need to make sure that the environment variable CLASSPATH contains all the pathnames of the Jar's which you are importing classes from. If not, you could put them on the compile-time classpath using:
javac -classpath c:\java\MyJar.jar MyClass.java
Be warned: This will overwrite the known classpath that javac will get from the system.
You might consider using Jakarta's Ant to build your project.
You may also want to reduce all your catch blocks down to one:
catch (Exception e)
{
e.printStackTrace();
}
This will catch every exception you expect to get and output the result of the error to the standard error stream (visible in the normal command line output stream). You can always check for specific exception by adding other catch clauses before the block above. (There are many views on how to catch errors, but if you are currently learning, rather than developing a large application, you might want to make it easier on yourself, for the time being).
Good luck.
Ps: This should probably be in the Java In General forum.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!