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))); 
  } 
  }

com.sun.crypto.provider.SunJCE的更多相关文章

  1. Maven打包编译找不到com.sun.crypto.provider.SunJCE类

    Maven配置 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>mav ...

  2. pom里引入lib下的包后编译报 package com.sun.crypto.provider does not exist问题解决

    最近正在迭代开发的一个项目编译安装时出现报“package com.sun.crypto.provider does not exist”的错误,由于本人能力水平有限,也是第一次遇到该问题,来来回回折 ...

  3. 解决 java 使用ssl过程中出现"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

    今天,封装HttpClient使用ssl时报一下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc ...

  4. javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certificatio

    场景:Java调用PHP接口,代码部署在服务器上后,调用报错,显示PHP服务器那边证书我这边服务器不信任(我猜的). 异常信息: 2019-08-06 14:00:09,102 [http-nio-4 ...

  5. java程序中访问https时,报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    在java中使用https访问数据时报异常: Caused by: sun.security.validator.ValidatorException: PKIX path building fail ...

  6. Maven:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    还是记录使用 maven 时遇到的问题. 一.maven报错 maven package 进行打包时出现了以下报错: Non-resolvable parent POM for com.wpbxin: ...

  7. mvn 编译报错mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested targ

    mavn 编译报错: mavn sun.security.validator.ValidatorException: PKIX path building failed: sun.security.p ...

  8. Flutter配置环境报错“PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target”

    背景:最近看了很多Flutter漂亮的项目,想要尝试一下.所有环境都搭建好之后,按照文档一步一步配置(抄袭),但始终报如下图错误. PKIX path building failed: sun.sec ...

  9. PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    注:网上搜来的快照,暂未验证 在java代码中请求https链接的时候,可能会报下面这个错误javax.net.ssl.SSLHandshakeException: sun.security.vali ...

随机推荐

  1. 在某个目录下的所有文件中查找包含某个字符串的Windows命令

    findstr可以完成这个工作.   上面的命令表示,当前目录以及当前目录的所有子目录下的所有文件中查找"string"这个字符串. *.*表示所有类型的文件. /s 表示当前目录 ...

  2. 对于我的linux常用命令的说明

    我所列出的linux常用命令中的选项并不是全部的选项,是一些我们经常用到的linux命令及选项

  3. SQL调用系统存储过程整理

    SQL系统存储过程用法整理: xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 xp_loginconfig --*报 ...

  4. Mac命令行语法高亮设置

    mkdir -p ~/.vim/autoloadcurl -fLo ~/.vim/autoload/plug.vim https://raw.githubusercontent.com/junegun ...

  5. Euro Efficiency_完全背包

    Description On January 1st 2002, The Netherlands, and several other European countries abandoned the ...

  6. 2016年11-29 mysql数据库

    php搭建环境:WAMP APPSERVERLAM架构:linux系统,apache服务器管理软件,mysql数据库,php语言 php apache.net iisjava tomcat mysql ...

  7. Selenium - 封装WebDrivers (C#)

    Web element仍然使用OpenQA.Selenium.IWebElement, 本类库将Selenium原装支持的各浏览器统一为OnDriver, 并将常用操作封装. using System ...

  8. magento在产品详细页面添加分享链接的方法

    1,在产品详细页面的对用位置加入一下代码 <div class="sharethis_box">          <?php echo $this->ge ...

  9. php计算一天的时间

    代码如下: $time=time(); //一天的开始 $start=strtotime(date('Y-m-d',$time).'00:00:01'); echo date('Y-m-d H:i:s ...

  10. MySQL优化—工欲善其事,必先利其器之EXPLAIN(转)

    最近慢慢接触MySQL,了解如何优化它也迫在眉睫了,话说工欲善其事,必先利其器.最近我就打算了解下几个优化MySQL中经常用到的工具.今天就简单介绍下EXPLAIN. 内容导航 id select_t ...