Java中3DES的加密与解密
2013-11-11 00:22
507 查看
3DES加密与解密的Java实现
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/** * 3DES工具类 */
public class DESUtils {
/**
* 加密
* @param inStr 需要加密的内容
* @param secretKey 密钥
* @return 加密后的数据
*/
public static String encrypt(String inStr, String secretKey) {
SecretKey deskey = new SecretKeySpec(secretKey.getBytes(), "DESede");
Cipher cipher;
String outStr = null;
try {
cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
outStr = byte2hex(cipher.doFinal(inStr.getBytes()));
} catch (Exception e) {
outStr = "default";
e.printStackTrace();
}
return outStr;
}
/**
* 解密
* @param inStr 需要解密的内容
* @param secretKey 密钥
* @return 解密后的数据
*/
public static String decrypt(String inStr, String secretKey)
{
SecretKey deskey = new SecretKeySpec(secretKey.getBytes(), "DESede");
Cipher cipher; String outStr = null;
try {
cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, deskey);
outStr = new String(cipher.doFinal(hex2byte(inStr)));
} catch (Exception e) {
outStr = "default";
e.printStackTrace();
}
return outStr;
}
/**
* 转化为16进制字符串方法
*
* @param digest 需要转换的字节组
* @return 转换后的字符串
*/
private static String byte2hex(byte[] digest) {
StringBuffer hs = new StringBuffer();
String stmp = "";
for (int n = 0; n < digest.length; n++) {
stmp = Integer.toHexString(digest
& 0XFF);
if (stmp.length() == 1) {
hs.append("0" + stmp);
} else {
hs.append(stmp);
}
}
return hs.toString().toUpperCase();
}
/**
* 十六进转二进制
* @param hexStr 待转换16进制字符串
* @return 二进制字节组
*/
public static byte[] hex2byte(String hexStr){
if (hexStr == null)
return null;
hexStr = hexStr.trim();
int len = hexStr.length();
if (len == 0 || len % 2 == 1)
return null;
byte[] digest = new byte[len / 2];
try {
for (int i = 0; i < hexStr.length(); i += 2) {
digest[i / 2] = (byte) Integer.decode("0x" + hexStr.substring(i, i + 2)).intValue();
} return digest;
} catch (Exception e) {
return null;
}
}
}
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/** * 3DES工具类 */
public class DESUtils {
/**
* 加密
* @param inStr 需要加密的内容
* @param secretKey 密钥
* @return 加密后的数据
*/
public static String encrypt(String inStr, String secretKey) {
SecretKey deskey = new SecretKeySpec(secretKey.getBytes(), "DESede");
Cipher cipher;
String outStr = null;
try {
cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, deskey);
outStr = byte2hex(cipher.doFinal(inStr.getBytes()));
} catch (Exception e) {
outStr = "default";
e.printStackTrace();
}
return outStr;
}
/**
* 解密
* @param inStr 需要解密的内容
* @param secretKey 密钥
* @return 解密后的数据
*/
public static String decrypt(String inStr, String secretKey)
{
SecretKey deskey = new SecretKeySpec(secretKey.getBytes(), "DESede");
Cipher cipher; String outStr = null;
try {
cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.DECRYPT_MODE, deskey);
outStr = new String(cipher.doFinal(hex2byte(inStr)));
} catch (Exception e) {
outStr = "default";
e.printStackTrace();
}
return outStr;
}
/**
* 转化为16进制字符串方法
*
* @param digest 需要转换的字节组
* @return 转换后的字符串
*/
private static String byte2hex(byte[] digest) {
StringBuffer hs = new StringBuffer();
String stmp = "";
for (int n = 0; n < digest.length; n++) {
stmp = Integer.toHexString(digest
& 0XFF);
if (stmp.length() == 1) {
hs.append("0" + stmp);
} else {
hs.append(stmp);
}
}
return hs.toString().toUpperCase();
}
/**
* 十六进转二进制
* @param hexStr 待转换16进制字符串
* @return 二进制字节组
*/
public static byte[] hex2byte(String hexStr){
if (hexStr == null)
return null;
hexStr = hexStr.trim();
int len = hexStr.length();
if (len == 0 || len % 2 == 1)
return null;
byte[] digest = new byte[len / 2];
try {
for (int i = 0; i < hexStr.length(); i += 2) {
digest[i / 2] = (byte) Integer.decode("0x" + hexStr.substring(i, i + 2)).intValue();
} return digest;
} catch (Exception e) {
return null;
}
}
}
相关文章推荐
- Java进行3DES加密解密
- JAVA和C# 3DES加密解密
- JAVA和C# 3DES加密解密
- c#加密,java解密(3DES加密)
- iOS 3DES加密 和 java 3DES 解密
- Java中3DES加密解密调用示例
- java 3DES加密、解密
- C#中可以与java进行互相转换的3des的加密和解密
- Java 3DES加密解密(Commons.Codec Base64)
- Java中3DES加密解密调用示例
- Android 3DES加密解密(JAVA和C#)
- JAVA和C# 3DES加密解密
- 3DES加密,java后台,安卓,iOS三端加密解密
- java密码的加密与解密(3des)
- websphere中应用java 3des加密解密
- JAVA基础 浅谈3DES加密解密
- JAVA和C# 3DES加密解密
- c#加密后用java解密(采用3DES加密)