package 自己的包名;
import java.security.*;
import javax.crypto.*;
public class DESPlus {
private static String strDefaultKey = "national";
private Cipher encryptCipher = null;
private Cipher decryptCipher = null;
/**
* ★将byte数组转换为表示16进制值的字符串★, 如:byte[]{8,18}转换为:0813, 和public static byte[]
* hexStr2ByteArr(String strIn) 互为可逆的转换过程
*
* @param arrB
* 需要转换的byte数组
* @return 转换后的字符串
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
*/
public static String byteArr2HexStr(byte[] arrB) throws Exception {
int iLen = arrB.length;
// 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
StringBuffer sb = new StringBuffer(iLen * 2);
for (int i = 0; i < iLen; i++) {
int intTmp = arrB[i];
// 把负数转换为正数
while (intTmp < 0) {
intTmp = intTmp + 256;
}
// 小于0F的数需要在前面补0
if (intTmp < 16) {
sb.append("0");
}
sb.append(Integer.toString(intTmp, 16));
}
return sb.toString();
}
/**
* ★将表示16进制值的字符串转换为byte数组★, 和public static String byteArr2HexStr(byte[]
* arrB) 互为可逆的转换过程
*
* @param strIn
* 需要转换的字符串
* @return 转换后的byte数组
* @throws Exception
* 本方法不处理任何异常,所有异常全部抛出
* @author <a href="mailto:tangjunfeng52099@gmail.com">Daniel</a>
*/
public static byte[] hexStr2ByteArr(String strIn) throws Exception {
byte[] arrB = strIn.getBytes();
int iLen = arrB.length;
// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
byte[] arrOut = new byte[iLen / 2];
for (int i = 0; i < iLen; i = i + 2) {
String strTmp = new String(arrB, i, 2);
arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
}
return arrOut;
}
/**
* 默认构造方法,使用默认密钥
*
* @throws Exception
*/
public DESPlus() throws Exception {
this(strDefaultKey);
}
/**
* 指定密钥构造方法
*
* @param strKey
* 指定的密钥
* @throws Exception
*/
public DESPlus(String strKey) throws Exception {
Security.addProvider(new com.sun.crypto.provider.SunJCE());
Key key = getKey(strKey.getBytes());
encryptCipher = Cipher.getInstance("DES");
encryptCipher.init(Cipher.ENCRYPT_MODE, key);
decryptCipher = Cipher.getInstance("DES");
decryptCipher.init(Cipher.DECRYPT_MODE, key);
}
/**
* 加密字节数组
*
* @param arrB
* 需加密的字节数组
* @return 加密后的字节数组
* @throws Exception
*/
public byte[] encrypt(byte[] arrB) throws Exception {
return encryptCipher.doFinal(arrB);
}
/**
* 加密字符串
*
* @param strIn
* 需加密的字符串
* @return 加密后的字符串
* @throws Exception
*/
public String encrypt(String strIn) throws Exception {
return byteArr2HexStr(encrypt(strIn.getBytes()));
}
/**
* 解密字节数组
*
* @param arrB
* 需解密的字节数组
* @return 解密后的字节数组
* @throws Exception
*/
public byte[] decrypt(byte[] arrB) throws Exception {
return decryptCipher.doFinal(arrB);
}
/**
* 解密字符串
*
* @param strIn
* 需解密的字符串
* @return 解密后的字符串
* @throws Exception
*/
public String decrypt(String strIn) throws Exception {
return new String(decrypt(hexStr2ByteArr(strIn)));
}
/**
* 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
*
* @param arrBTmp
* 构成该字符串的字节数组
* @return 生成的密钥
* @throws java.lang.Exception
*/
private Key getKey(byte[] arrBTmp) throws Exception {
// 创建一个空的8位字节数组(默认值为0)
byte[] arrB = new byte[8];
// 将原始字节数组转换为8位
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
arrB[i] = arrBTmp[i];
}
// 生成密钥
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key;
}
public static void main(String[] args) {
try {
String test = "Hello!";
DESPlus des = new DESPlus();
System.out.println("加密前的字符:" + test);
System.out.println("加密后的字符:" + des.encrypt(test));
System.out.println("解密后的字符:" + des.decrypt(des.encrypt(test)));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
分享到:
相关推荐
在PHP中实现DESPlus加密解密,主要涉及以下几个关键知识点: 1. **DES算法**:DES是一种块加密算法,由IBM在1970年代提出,后来被美国国家标准局采纳为数据加密标准。它使用56位密钥对64位的数据块进行加密,通过一...
Java DESPlus 加密解密 DESPlus.java .
DESPlus.java加密解密算法实现
DESPlus是DES算法的一种增强形式,通常包括更复杂的密钥扩展或者使用多轮加密,以提高安全性。在Java中实现DES加密和解密是一个常见的任务,特别是在处理敏感数据时。 在Java中,`javax.crypto`包提供了用于加密和...
在这个名为"DESPlus.zip_DES JAVA_Des加密解密_des"的压缩包中,包含了一个名为"DESPlus.java"的源代码文件,这是一个关于如何在Java中使用DES进行加密和解密的实例。 DES是一种块密码,它的基本思想是将明文数据...
在 DESPlus 类的构造方法中,我们使用了默认密钥或指定的密钥来初始化加密和解密的 Cipher 对象。然后,我们可以使用 encryptCipher 对象来加密密码,使用 decryptCipher 对象来解密密码。 在我们的示例代码中,...
andriod des加密,实现.net跟客户端加密解密
在`DESPlus(加密解密2).java`中,可能会展示如何使用增强的DES算法进行更安全的数据处理。 3. **RSA**:RSA是一种非对称加密算法,由两个密钥组成:公钥和私钥。公钥用于加密,私钥用于解密。这种机制使得数据可以...
本篇将深入探讨如何使用Java进行文件数据的加密,重点是描述中提到的自定义密钥以及基于DES(Data Encryption Standard)的加强版——DESPlus。 首先,我们要理解加密的基本概念。加密是一种将明文数据转换为看似...
标题中的"DESPlus+Base64+MD5Util"指的是三种不同的加密与编码技术,它们在信息技术领域中常用于数据安全。DESPlus是基于传统DES(Data Encryption Standard)的增强版,Base64是一种二进制转文本的编码方式,而MD5...
public DESPlus(String strKey) throws Exception { Security.addProvider(new com.sun.crypto.provider.SunJCE()); Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES"); ...
### JAVA DES 加密算法 #### 知识点概述 本文将详细介绍 Java 中的 DES(Data Encryption Standard)加密算法实现,并通过具体的代码示例来解析其工作原理与使用方法。 #### DES 加密算法简介 DES(数据加密标准...
本篇将探讨两个核心的文件:Base64Util.java和DESPlus.java,它们分别涉及Base64编码和DES(Data Encryption Standard)加密算法。 首先,让我们来看看Base64Util.java。Base64是一种用于在网络上传输二进制数据的...
这段Java代码定义了一个`DESPlus`类,包含了加密和解密两个方法。`DESKeySpec`用于创建DES密钥,`Cipher`则用于执行实际的加密和解密操作。注意这里使用的是ECB模式和PKCS5Padding填充,这是DES最常用的模式和填充...
DESPlus des = new DESPlus(key); byte[] code = des.decrypt(config); String decodeConfig = new String(code,0,code.length); System.out.println("解密后的配置:"+ decodeConfig); // 加密解密 public ...
如果有报CacheTranscoder.java,DesBase64Tool.java和DESPlus.java 类找不到相关的错误,直接删掉就行,这个是有关加密的算法 另外,将applicationContenxt.xml 中 <prop key="hibernate.hbm2ddl.auto">update...