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编程环境中,有时我们需要处理加密和解密操作,这时会用到`com.sun.crypto.provider.SunJCE`。这个类是Java加密扩展(Java Cryptography Extension, JCE)的一部分,由Sun Microsystems(现已被Oracle收购)...
Java加密解密字符串找不到 com.sun.crypto.provider.SunJCE() 用到jar包
new com.sun.crypto.provider.SunJCE()
这个压缩包主要是为了解决在某些Java运行环境中遇到的问题,即“找不到com.sun.crypto.provider.SunJCE()”。 在Java开发中,"com.sun.crypto.provider.SunJCE" 是Sun Microsystems公司提供的一个加密服务提供者类...
security.provider.5=com.sun.crypto.provider.SunJCE security.provider.6=sun.security.jgss.SunProvider security.provider.7=com.sun.security.sasl.Provider security.provider.8=org.jcp.xml.dsig.internal...
at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..) at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..) at javax.crypto.Ciphe
3. **添加SunJCE**:在`java.security`文件中添加如下行以指定SunJCE为安全提供者之一:`security.provider.n=com.sun.crypto.provider.SunJCE`。其中`n`代表提供者的优先级,需要确保序号递增且不跳过任何数字。 #...
import com.sun.crypto.provider.SunJCE; import java.io.Serializable; public class EncryptData implements Serializable { byte[] encryptKey; DESedeKeySpec spec; SecretKeyFactory keyFactory; Secret...
1. 添加 provider 对象,如:a)Security.addProvider(new com.sun.crypto.provider.SunJCE()); 2. 生成秘钥,例如:KeyGenerator generator= KeyGenerator.getInstance("DESede");generator.init(new SecureRandom(...
Security.addProvider(new com.sun.crypto.provider.SunJCE()); // 添加 Sun 提供的 JCE Key key = getKey(strKey); // 生成密钥 Cipher encryptCipher = Cipher.getInstance("DES"); // 创建 Cipher 对象 ...
Security.addProvider(new com.sun.crypto.provider.SunJCE()); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); // 设置密钥长度,通常为2048位或更长 KeyPair keyPair...
Security.addProvider(new com.sun.crypto.provider.SunJCE()); KeyGenerator keygen = KeyGenerator.getInstance("DES"); SecretKey deskey = keygen.generateKey(); ``` `SecretKey`接口用于存储对称密钥,它是...
Security.addProvider(new com.sun.crypto.provider.SunJCE()); } public static byte[] getKey() throws Exception { KeyGenerator keygen = KeyGenerator.getInstance(ALGORITHM); SecretKey deskey = ...
Security.addProvider(new com.sun.crypto.provider.SunJCE()); Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); ...
Security.addProvider(new com.sun.crypto.provider.SunJCE()); Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); ...
`sunjce_provider.jar`是Java Cryptography Extension (JCE)的一部分,由Sun Microsystems(现为Oracle公司)提供。JCE是一组用于实现加密、散列、密钥协议和其他密码学功能的API。`sunjce_provider.jar`是其中的一...