📄 cryptutil.java
字号:
/****************************************************************************
* Package : com.ecSolutions.ecAppServer.util
* File : CryptUtil.java
* Create Date : 2007-7-20
* Author : Steven Chen
*
* Copyright(C) 2006 ecSolutions(shanghai) Co.,Limited.All Rights Reserved.
*
***************************************************************************/
package com.ecSolutions.ecAppServer.util;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import java.security.MessageDigest;
/**
*
* @author Steven Chen
* @version $Id$
*/
public class CryptUtil {
// 默认使用3DES加密
private static String algorithm = "DESede";
/**
* 根据密钥对明文进行加密,返回密文
* @param plaintext byte[] 明文
* @param key byte[] 密钥
* @return byte[] 密文
* @throws Exception 加密过程中出现的各种异常
*/
public static byte[] encrypt(byte[] plaintext, byte[] key) throws Exception {
try {
SecureRandom sr = new SecureRandom();
KeyGenerator keygen = KeyGenerator.getInstance(algorithm);
SecretKey deskey = keygen.generateKey();
/*
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKey keySpec = keyFactory.generateSecret(dks);
*/
//Cipher对象实际完成加密操作
Cipher cipher = Cipher.getInstance(algorithm);
//用密钥初始化Cipher对象
cipher.init(Cipher.ENCRYPT_MODE, deskey);
//执行加密操作
byte[] cryptotext = cipher.doFinal(plaintext);
return cryptotext;
} catch (InvalidKeyException e) {
throw new Exception("Invalid Key Exception:" + e.toString());
} catch (NoSuchAlgorithmException e) {
throw new Exception("No Such Algorithm Exception:" + e.toString());
} catch (BadPaddingException e) {
throw new Exception("Bad Padding Exception:" + e.toString());
}
}
/**
* 根据密钥对密文进行加密,返回明文
* @param cryptotext byte[] 密文
* @param key byte[] 密钥
* @return byte[] 明文
* @throws Exception 解密过程中出现的各种异常
*/
public static byte[] decrypt(byte[] cryptotext, byte[] key) throws Exception {
try {
SecureRandom sr = new SecureRandom();
DESedeKeySpec dks = new DESedeKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
SecretKey keySpec = keyFactory.generateSecret(dks);
//Cipher对象实际完成解密操作
Cipher cipher = Cipher.getInstance(algorithm);
//用密钥初始化Cipher对象
cipher.init(Cipher.DECRYPT_MODE, keySpec,sr);
//执行解密操作
byte[] plaintext = cipher.doFinal(cryptotext);
return plaintext;
} catch (InvalidKeyException e) {
throw new Exception("Invalid Key Exception:" + e.toString());
} catch (NoSuchAlgorithmException e) {
throw new Exception("No Such Algorithm Exception:" + e.toString());
} catch (IllegalBlockSizeException e) {
throw new Exception("Illegal BlockSize Exception:" + e.toString());
} catch (BadPaddingException e) {
throw new Exception("Bad Padding Exception:" + e.toString());
}
}
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 final static String encryptMD5(String source) {
if (source == null) {
source = "";
}
String result = "";
try {
result = encrypt(source, "MD5");
} catch (NoSuchAlgorithmException ex) {
ex.printStackTrace();
}
return result;
}
/**
* Encrypt string
*/
public final static String encrypt(String source, String algorithm) throws NoSuchAlgorithmException {
byte[] resByteArray = encrypt(source.getBytes(), algorithm);
return toHexString(resByteArray);
}
public final static byte[] encrypt(byte[] source, String algorithm) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance(algorithm);
md.reset();
md.update(source);
return md.digest();
}
public final static String toHexString(byte[] res) {
StringBuffer sb = new StringBuffer(res.length << 1);
for (int i = 0; i < res.length; i++) {
String digit = Integer.toHexString(0xFF & res[i]);
if (digit.length() == 1) {
digit = '0' + digit;
}
sb.append(digit);
}
return sb.toString().toUpperCase();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -