java.security.NoSuchAlgorithmException: Cannot find any provider SunJCE
843810Jun 4 2004 — edited Jun 5 2004I was able to compile the below java code successfully but when I run it, it is giving me an error : "java.security.NoSuchAlgorithmException: Cannot find any provider supporting SunJCE".
I placed all the 4 jars( jce1_2_2.jar , sunjce_provider.jar , local_policy.jar , US_export_policy.jar ) which sun provided in the classpath.
Can u tell me why so??
--------------------------------------------------Java Source Code --------------------------------------------------------------------
import java.text.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import javax.crypto.*;
import sun.misc.*;
public class Self_Util {
Cipher ecipher;
Cipher dcipher;
SecretKey key;
public Self_Util() {
try {
ecipher = Cipher.getInstance("SunJCE");
System.out.println("ecipher ::" + ecipher);
dcipher = Cipher.getInstance("SunJCE");
System.out.println("dcipher ::" + dcipher);
key= KeyGenerator.getInstance("SunJCE").generateKey();
ecipher.init(Cipher.ENCRYPT_MODE, key);
dcipher.init(Cipher.DECRYPT_MODE, key);
} catch (javax.crypto.NoSuchPaddingException e) {
e.printStackTrace();
} catch (java.security.NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (java.security.InvalidKeyException e) {
e.printStackTrace();
}
}
public String enCryptPwd(String str) {
try {
System.out.println("before getBytes :: " + str);
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
System.out.println("after getBytes :: " + utf8);
System.out.println("ecipher : " + ecipher);
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
System.out.println("after doFinal :: " + enc);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
} catch (javax.crypto.BadPaddingException e) {
} catch (IllegalBlockSizeException e) {
} catch (UnsupportedEncodingException e) {
} catch (java.io.IOException e) {
}
return null;
}
public String deCryptPwd(String str)throws Exception{
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
public static void main(String args[]) throws Exception {
System.out.println("testing..");
String s2=null,s=null;
Self_Util u=new Self_Util();
String pwd="00000"; //presum the plainted pass is "00000"
System.out.println("testing...0000 before pwd ::: " + pwd);
s=u.enCryptPwd(pwd); //encrypt the plainted password
System.out.println("testing...0000 after");
byte[] b_tmp=s.getBytes();
System.out.println("testing111.." + b_tmp);
System.out.println("the length of the encrypted password before storing->"+b_tmp.length);
System.out.println("the content of the encrypted password before storing->"+s);
/*
byte[] b_tmp2=s_pwd.getBytes();
System.out.println("the length of the encrypted password before decrpyting->"+b_tmp2.length);
System.out.println("the content of the encrypted password before decrpyting->"+s_pwd);
s2=u.deCryptPwd(s_pwd); //decrypt the encrypted password
System.out.println("the content of the encrypted password after decrpyting->"+s2); //print the plainted password
*/
}
} // end of java source code .
----------------------------------------------------------------------------------------------------------------------------------------
when I run it, it is giving me an error :
java.security.NoSuchAlgorithmException: Cannot find any provider supporting SunJCE
at javax.crypto.Cipher.getInstance(DashoA6275)
at Self_Util.<init>(Self_Util.java:16)
at Self_Util.main(Self_Util.java:81)
testing...0000 before pwd ::: 00000
before getBytes :: 00000
after getBytes :: [B@21b6d
ecipher : null
Exception in thread "main" java.lang.NullPointerException
at Self_Util.enCryptPwd(Self_Util.java:46)
at Self_Util.main(Self_Util.java:87)