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. hdu 2081

    PS:...找到好多水题.... #include "stdio.h" int main(){ ]; int i,j,n,N; scanf("%d",& ...

  2. linux基础命令学习五(软件包管理、下载管理)

    Linux 软件包管理   本文主要是记录下RedHat系列的软件包管理. 内容分为以下二个部分:二进制包的管理,源代码包的管理 一.二进制包的管理 1.1概念 主要有RPM和YUM这两种包管理. 两 ...

  3. linux基础命令学习(一)

    pwd 输出当前工作路径tree 以树状图列出目录的内容ctrl+c 取消命令的执行clear 清空屏幕ls 列出文件目录 蓝色是目录,白色是普通文件alias cls=clear 别名终端:本地终端 ...

  4. 一个简单的Dump转文本工具—Dump2Text

    每次电脑重装都得烦心,要把庞大的IDE重新配置一次,正准备安装Visual Stdio 2010,上网找镜像的时候发现,Visual Stdio 2013推出了Community版,不仅没有lite掉 ...

  5. Redis - 密码配置和主从复制

    使用config set命令修改requirepass参数配置Redis密码config set requirepass password 也可以通过配置文件修改密码,重启后生效. 克隆虚拟机,分别运 ...

  6. Testing the CATCHER_DP

    Description A military contractor for the Department of Defense has just completed a series of preli ...

  7. 4、SQL基础整理(规范函数)

    规范函数: 绝对值 select abs(-5) print abs(-5) 表中取绝对值的方法: select code,name,abs(chinese)as yuwen from xueshen ...

  8. 面向过程部分 Java 和 C++ 的区别

    前言 Java 和 C++ 在面向过程部分区别并不大,但还是有的,本文罗列了这些区别. 在 Java 中: 1. 数据类型的范围和机器无关 2. 加上前缀 0b 可以表示二进制数,如 0b1001 就 ...

  9. Particles.js基于Canvas画布创建粒子原子颗粒效果

    文章目录 使用方法 自定义参数 相关链接 Particles.js是一款基于HTML5 Canvas画布的轻量级粒子动画插件,可以设置粒子的形状.旋转.分布.颜色等属性,还可以动态添加粒子,效果非常炫 ...

  10. MyEclipse+Struts+Hibernate+Mysql开发环境配置

    软件: jdk-6u22-windows-x64.exe apache-tomcat-6.0.29.exe mysql-5.1.51-winx64.exe myeclipse-8.6.0-win32. ...