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)));
}
}
com.sun.crypto.provider.SunJCE的更多相关文章
- Maven打包编译找不到com.sun.crypto.provider.SunJCE类
Maven配置 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>mav ...
- pom里引入lib下的包后编译报 package com.sun.crypto.provider does not exist问题解决
最近正在迭代开发的一个项目编译安装时出现报“package com.sun.crypto.provider does not exist”的错误,由于本人能力水平有限,也是第一次遇到该问题,来来回回折 ...
- 解决 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 ...
- 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 ...
- 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 ...
- 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: ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 2016 - 1 - 20 runloop学习(2)
一:CFRunLoopModeRef 1. CFRunLoopModeRef带表RunLoop的运行模式 2. 一个Runloop可以有若干个mode,每个mode又包含若干个sourse,timer ...
- 《view programming guide for iOS 》之可以使用动画效果的属性
frame—Use this to animate position and size changes for the view. ,框架,可以视图动态改变大小和位置 bounds—Use this ...
- apache环境Zf2要装的intl验证
sudo apt-get install php5-intl
- K Best(最大化平均数)_二分搜索
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- [图论]Dijkstra 算法小结
Dijkstra 算法小结 By Wine93 2013.11 1. Dijkstra 算法相关介绍 算法阐述:Dijkstra是解决单源最短路径的算法,它可以在O(n^2)内计算出源点(s)到图中 ...
- 重学STM32---(五)ADC
这两天把外部中断和ADC看了下,个人感觉外部中断不是很难,也就没有把记下来了,毕竟写这个挺浪费时间.ADC是比较复杂的,如果想让完全自由的运用ADC必须经过多次实践可能才可以.由于已经学过库函数,也就 ...
- dedecms头部标签(标题,关键词,描述标签)(借用)
先说说dedecms头部标题,关键词,描述标签的作用我相信网络上也有很多这样的信息,那为什么我还要写这个?因为这个对我们初学者来说还是比较重要的,因为做SEO就要用到这些标签.首先我写下首页头部标签我 ...
- mysql中在表中insert数据时,有重复主键id时,变成update
MySQL 自4.1版以后开始支持INSERT … ON DUPLICATE KEY UPDATE语法 例如: id name sex age 1 kathy male 23 2 Javer f ...
- R处理xml文件
最近处理数据的时候需要处理一些xml文件,但是xml文件的格式之前并不是很熟悉.幸好R有一些函数可以帮助快速处理xml文件.其中的xmlToList函数尤其方便,可以使你从陌生的XML迅速回到熟悉的R ...
- [转]C语言文件操作
1,两种文件存取方式(输入,输出方式) 顺序存取 直接存取 2,数据的两种存放形式 文本文件 二进制文件 13.2文件指针 定义文件类型指针变量的一般形式: FILE *指针变量名; 例如: FILE ...