【Android工具类】比DES加密更安全的算法——3DES加密算法
2015-11-12 17:59
501 查看
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992
在前面的文章里面,我们讨论了DES算法,同时也明白了如何才能保证不同平台下的加密和解密结果的一致性。但是DES作为出现了很长时间的一种加密算法,随着计算机运算能力的加强,DES加密容易被暴力破解,其安全性变得有点低。于是,为了增强数据的安全性,3DES算法就应运而生了。
3DES,顾名思义,就是对DES加密算法的改进,3DES通过对每个数据进行3次DES加密,从而降低被破解的可能性。
如果我们要使用3DES加密,需要以下几个步骤
①传入共同约定的密钥(keyBytes)以及算法(Algorithm),来构建SecretKey密钥对象
SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
②根据算法实例化Cipher对象。它负责加密/解密
Cipher c1 = Cipher.getInstance(Algorithm);
③传入加密/解密模式以及SecretKey密钥对象,实例化Cipher对象
c1.init(Cipher.ENCRYPT_MODE, deskey);
④传入字节数组,调用Cipher.doFinal()方法,实现加密/解密,并返回一个byte字节数组
c1.doFinal(src);
具体的代码实现过程如下
package com.qust.rollcallstudent.utils;
import java.security.InvalidAlgorithmParameterException;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Locale;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
*
* @ClassName: com.qust.rollcallstudent.utils.DESUtil
* @Description: DES加密解密工具包
* @author zhaokaiqiang
* @date 2014-11-13 下午8:40:56
*
*/
public class DESUtil {
public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
/**
* DES算法,加密
*
* @param data
* 待加密字符串
* @param key
* 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
* @throws InvalidAlgorithmParameterException
* @throws Exception
*/
public static String encode(String key, String data) {
if (data == null)
return null;
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
AlgorithmParameterSpec paramSpec = iv;
cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
byte[] bytes = cipher.doFinal(data.getBytes());
return byte2String(bytes);
} catch (Exception e) {
e.printStackTrace();
return data;
}
}
/**
* DES算法,解密
*
* @param data
* 待解密字符串
* @param key
* 解密私钥,长度不能够小于8位
* @return 解密后的字节数组
* @throws Exception
* 异常
*/
public static String decode(String key, String data) {
if (data == null)
return null;
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
AlgorithmParameterSpec paramSpec = iv;
cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
return new String(cipher.doFinal(byte2hex(data.getBytes())));
} catch (Exception e) {
e.printStackTrace();
return data;
}
}
/**
* 二行制转字符串
*
* @param b
* @return
*/
private static String byte2String(byte[] b) {
StringBuilder hs = new StringBuilder();
String stmp;
for (int n = 0; b != null && n < b.length; n++) {
stmp = Integer.toHexString(b
& 0XFF);
if (stmp.length() == 1)
hs.append('0');
hs.append(stmp);
}
return hs.toString().toUpperCase(Locale.CHINA);
}
/**
* 二进制转化成16进制
*
* @param b
* @return
*/
private static byte[] byte2hex(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException();
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
}
在前面的文章里面,我们讨论了DES算法,同时也明白了如何才能保证不同平台下的加密和解密结果的一致性。但是DES作为出现了很长时间的一种加密算法,随着计算机运算能力的加强,DES加密容易被暴力破解,其安全性变得有点低。于是,为了增强数据的安全性,3DES算法就应运而生了。
3DES,顾名思义,就是对DES加密算法的改进,3DES通过对每个数据进行3次DES加密,从而降低被破解的可能性。
如果我们要使用3DES加密,需要以下几个步骤
①传入共同约定的密钥(keyBytes)以及算法(Algorithm),来构建SecretKey密钥对象
SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);
②根据算法实例化Cipher对象。它负责加密/解密
Cipher c1 = Cipher.getInstance(Algorithm);
③传入加密/解密模式以及SecretKey密钥对象,实例化Cipher对象
c1.init(Cipher.ENCRYPT_MODE, deskey);
④传入字节数组,调用Cipher.doFinal()方法,实现加密/解密,并返回一个byte字节数组
c1.doFinal(src);
具体的代码实现过程如下
package com.qust.rollcallstudent.utils;
import java.security.InvalidAlgorithmParameterException;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;
import java.util.Locale;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
/**
*
* @ClassName: com.qust.rollcallstudent.utils.DESUtil
* @Description: DES加密解密工具包
* @author zhaokaiqiang
* @date 2014-11-13 下午8:40:56
*
*/
public class DESUtil {
public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
/**
* DES算法,加密
*
* @param data
* 待加密字符串
* @param key
* 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
* @throws InvalidAlgorithmParameterException
* @throws Exception
*/
public static String encode(String key, String data) {
if (data == null)
return null;
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
AlgorithmParameterSpec paramSpec = iv;
cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
byte[] bytes = cipher.doFinal(data.getBytes());
return byte2String(bytes);
} catch (Exception e) {
e.printStackTrace();
return data;
}
}
/**
* DES算法,解密
*
* @param data
* 待解密字符串
* @param key
* 解密私钥,长度不能够小于8位
* @return 解密后的字节数组
* @throws Exception
* 异常
*/
public static String decode(String key, String data) {
if (data == null)
return null;
try {
DESKeySpec dks = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
// key的长度不能够小于8位字节
Key secretKey = keyFactory.generateSecret(dks);
Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
AlgorithmParameterSpec paramSpec = iv;
cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
return new String(cipher.doFinal(byte2hex(data.getBytes())));
} catch (Exception e) {
e.printStackTrace();
return data;
}
}
/**
* 二行制转字符串
*
* @param b
* @return
*/
private static String byte2String(byte[] b) {
StringBuilder hs = new StringBuilder();
String stmp;
for (int n = 0; b != null && n < b.length; n++) {
stmp = Integer.toHexString(b
& 0XFF);
if (stmp.length() == 1)
hs.append('0');
hs.append(stmp);
}
return hs.toString().toUpperCase(Locale.CHINA);
}
/**
* 二进制转化成16进制
*
* @param b
* @return
*/
private static byte[] byte2hex(byte[] b) {
if ((b.length % 2) != 0)
throw new IllegalArgumentException();
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
}
相关文章推荐
- 谷歌 Project Zero 团队宣布新政策,漏洞披露前将有完整的 90 天缓冲期
- 春节长假安全手册
- 用ASP编写的加密和解密类
- 地震避险自救常识
- 路由器安全有关的目录
- 打造个性_安全的电脑系统图文教程2第1/2页
- 路由器内的安全认证
- VBS脚本加密/解密VBS脚本(简易免杀版1.1)
- 加强php的安全之一
- http www安全必备知识
- BAT加密工具 EncryBat 非编译型bat批处理加密方案与代码
- C#对称加密(AES加密)每次生成的结果都不同的实现思路和代码实例
- SQLServer 2008中的代码安全(一) 存储过程加密与安全上下文
- 实例讲解SQL Server加密功能
- C#实现对文件进行加密解密的方法
- C#实现数据包加密与解密实例详解