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 ...
随机推荐
- golang函数调用计时
package main import ( "log" "time" ) func f() { defer timeoutCheck("f slow& ...
- Java三大主流开源工作流引擎技术分析
首先,这个评论是我从网上,书中,搜索和整理出来的,也许有技术点上的错误点,也许理解没那么深入.但是我是秉着学习的态度加以评论,学习,希望对大家有用,进入正题! 三大主流工作流引擎:Shark,oswo ...
- 学习:Log中'main', 'system', 'radio', 'events'
在Android中不同的log写到不同的设备中,共有/dev/log/system, /dev/log/main, /dev/log/radion, /dev/log/events四中类型.其中默认L ...
- Ubuntu 升级VisualBox后无法启动 Kernel driver not installed (rc=-1908)
VisualBox之所以在Linux上比传统的VMware快得多,关键一点就是它和Linux内核的结合比较紧密,这也是开源的优点. 不过Linux内核更新很频繁,每次更新内核后启动VirtualBox ...
- win下隐藏任务栏
C# 隐藏任务栏开始按钮 关闭shell 分类: .NET C# 2011-07-14 15:26 789人阅读 评论(1) 收藏 举报 shell任务c#stringnulluser 一.隐藏任务栏 ...
- 11、SQL基础整理(变量)
变量 定义变量:declare @hello varchar(20) 赋值:set @hello = ‘你好’ select(结果框显示)/print(消息框显示) @hello *三行必须同时 ...
- iOS-NSThread使用
NSThread: 优点:NSThread 比其他两个轻量级(Cocoa NSOperation.GCD) 缺点:需要自己管理线程的生命周期,线程同步.线程同步对数据的加锁会有一定的系统开销 Coco ...
- editplus查找替换的正则表达式应用
表达式 说明\t 制表符.\n 新行.. 匹配任意字符.| 匹配表达式左边和右边的字符. 例如, "ab|bc" ...
- Linux下解决“shutdown: command not found"问题
今天关机时,使用shutdown来执行此操作,但出现了一个"bash:shutdown:command not found"错误提示.这让我很困惑,这个命令在系统中是肯定存在的,但 ...
- yii2后台上传图片,前台也能显示 的方法
备注:::很low 高端人士 勿入 支持 吐槽 有你们的吐槽才有我的进步 还不会yii上传的 go out (哈哈,开玩笑)