com.sun.crypto.provider.SunJCE

Could not instantiate bean class [com.lz.monitor.alert.service.ServiceImp]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: com.sun.crypto.provider.SunJCE 
Caused by: java.lang.NoClassDefFoundError: com.sun.crypto.provider.SunJCE 
3DES加密解密調用示例 
文章分類:Java編程 關鍵字: 3des加密解密調用示例 
在java中調用sun公司提供的3DES加密解密算法時,須要使用到$JAVA_HOME/jre/lib/目錄下以下的4個jar包: 
  jce.jar 
  security/US_export_policy.jar 
  security/local_policy.jar 
  ext/sunjce_provider.jar 
  Java運行時會自動加載這些包,所以對於帶main函數的應用程序不須要設置到CLASSPATH環境變量中。對於WEB應用,須要把這些包加到WEB-INF/lib目錄下。 
  如下是java中調用sun公司提供的3DES加密解密算法的樣本代碼: 
  /*字符串 DESede(3DES) 加密*/ 
  import java.security.*; 
  import javax.crypto.*; 
  import javax.crypto.spec.SecretKeySpec; 
  public class ThreeDes { 
  private static final String Algorithm = "DESede"; //定義 加密算法,可用 DES,DESede,Blowfish 
  //keybyte爲加密密鑰,長度爲24字節 
  //src爲被加密的數據緩衝區(源) 
  public static byte[] encryptMode(byte[] keybyte, byte[] src) { 
  try { 
  //生成密鑰 
  SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); 
  //加密 
  Cipher c1 = Cipher.getInstance(Algorithm); 
  c1.init(Cipher.ENCRYPT_MODE, deskey); 
  return c1.doFinal(src); 
  } 
  catch (java.security.NoSuchAlgorithmException e1) { 
  e1.printStackTrace(); 
  } 
  catch (javax.crypto.NoSuchPaddingException e2) { 
  e2.printStackTrace(); 
  } 
  catch (java.lang.Exception e3) { 
  e3.printStackTrace(); 
  } 
  return null; 
  } 
  //keybyte爲加密密鑰,長度爲24字節 
  //src爲加密後的緩衝區 
  public static byte[] decryptMode(byte[] keybyte, byte[] src) { 
  try { 
  //生成密鑰 
  SecretKey deskey = new SecretKeySpec(keybyte, Algorithm); 
  //解密 
  Cipher c1 = Cipher.getInstance(Algorithm); 
  c1.init(Cipher.DECRYPT_MODE, deskey); 
  return c1.doFinal(src); 
  } 
  catch (java.security.NoSuchAlgorithmException e1) { 
  e1.printStackTrace(); 
  } 
  catch (javax.crypto.NoSuchPaddingException e2) { 
  e2.printStackTrace(); 
  } 
  catch (java.lang.Exception e3) { 
  e3.printStackTrace(); 
  } 
  return null; 
  } 
  //轉換成十六進制字符串 
  public static String byte2hex(byte[] b) { 
  String hs=""; 
  String stmp=""; 
  for (int n=0;n<b.length;n++) { 
  stmp=(java.lang.Integer.toHexString(b[n] & 0XFF)); 
  if (stmp.length()==1) hs=hs+"0"+stmp; 
  else hs=hs+stmp; 
  if (n<b.length-1) hs=hs+":"; 
  } 
  return hs.toUpperCase(); 
  } 
  public static void main(String[] args){ 
  //添加新安全算法,若是用JCE就要把它添加進去 
  Security.addProvider(new com.sun.crypto.provider.SunJCE()); 
  final byte[] keyBytes = {0x11, 0x22, 0x4F, 0x58, 
  (byte)0x88, 0x10, 0x40, 0x38, 0x28, 0x25, 0x79, 0x51, 
  (byte)0xCB, (byte)0xDD, 0x55, 0x66, 0x77, 0x29, 0x74, 
  (byte)0x98, 0x30, 0x40, 0x36, (byte)0xE2 
  }; //24字節的密鑰 
  String szSrc = "This is a 3DES test. 測試"; 
  System.out.println("加密前的字符串:" + szSrc); 
  byte[] encoded = encryptMode(keyBytes, szSrc.getBytes()); 
  System.out.println("加密後的字符串:" + new String(encoded)); 
  byte[] srcBytes = decryptMode(keyBytes, encoded); 
  System.out.println("解密後的字符串:" + (new String(srcBytes))); 
  } 
  }java

相關文章
相關標籤/搜索