日期:2014-05-16  浏览次数:20695 次

JAVA 常用加密方法

1.Base64
? 加密:org.apache.commons.codec.binary.Base64.encodeBase64(byte[] binaryData)
? 解密:org.apache.commons.codec.binary.Base64.decodeBase64(byte[] base64Data)
2.Md5
? 加密:org.apache.commons.codec.digest.md5Hex(byte[] data)
? 解密:无
3.DES(des-ecb,3des,des-cbc,cbc-mac)

view plaincopy to clipboardprint?
?import java.io.ByteArrayOutputStream;??
import java.security.SecureRandom;??
import java.util.Arrays;??
?
import javax.crypto.Cipher;??
import javax.crypto.SecretKey;??
import javax.crypto.SecretKeyFactory;??
import javax.crypto.spec.DESKeySpec;??
import javax.crypto.spec.DESedeKeySpec;??
import javax.crypto.spec.IvParameterSpec;??
import javax.crypto.spec.SecretKeySpec;??
?
import org.bouncycastle.crypto.BlockCipher;??
import org.bouncycastle.crypto.Mac;??
import org.bouncycastle.crypto.engines.DESEngine;??
import org.bouncycastle.crypto.macs.CBCBlockCipherMac;??
import org.bouncycastle.crypto.params.KeyParameter;??
?
import com.alibaba.common.lang.StringUtil;??
import com.huateng.commons.lang.convert.HexUtils;??
?
public class ShfftDes {??
??? //验证用密钥??
??? private byte[]????????????? key???????? = "000000000000000000000000".getBytes();??
?
??? //??? private byte[]????????????? key???????? = Hex.decode("00000000");??
?
??? private byte[]????????????? ivs???????? = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };??
?
??? private static final String DES_EDE???? = "DESede/ECB/NoPadding";?????????????? //定义 加密算法,可用 DES,DESede,Blowfish??????? //keybyte为加密密钥,长度为24字节??? //src为被加密的数据缓冲区(源)??
?
??? private static final String DES_EDE_CBC = "DESede/CBC/NoPadding";?????????????? //定义 加密算法,可用 DES,DESede,Blowfish??????? //keybyte为加密密钥,长度为24字节??? //src为被加密的数据缓冲区(源)??
?
??? private static final String DES_CBC???? = "DES/CBC/NoPadding";??
?
??? private static final String DES_ECB???? = "DES/ECB/PKCS5Padding";??
?
??? ?
??? public byte[] CryptByDes(byte[] content, int mode) throws Exception {??
??????? Cipher cipher = Cipher.getInstance(DES_ECB);??
??????? SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");??
??????? SecretKey secretKey = keyFactory.generateSecret(new DESKeySpec(key));??
??????? cipher.init(mode, secretKey);??
??????? return cipher.doFinal(content);??
??? }??
?
??? ?
??? public byte[] CryptBy3Des(byte[] content, int mode) throws Exception {??
??????? Cipher cipher = Cipher.getInstance(DES_EDE);??
??????? SecretKey secretKey = new SecretKeySpec(key, "DESede");??
??????? cipher.init(mode, secretKey);??
??????? return cipher.doFinal(content);??
??? }??
?
??? ?
??? public byte[] CryptByDesCbc(byte[] content, int mode) throws Exception {??
??????? Cipher cipher = Cipher.getInstance(DES_CBC);??
??????? SecretKey secureKey = new SecretKeySpec(key, "DES");??
??????? IvParameterSpec iv = new IvParameterSpec(ivs);??
??????? cipher.init(mode, secureKey, iv);??
??????? return cipher.doFinal(HexUtils.fromHex(new String(content)));??
??? }??
?
??? ?
??? public byte[] CryptBy3DesCbc(byte[] content, int mode) throws Exception {??
??????? Cipher cipher = Cipher.getInstance(DES_EDE_CBC);??
??????? SecretKey secureKey = new SecretKeySpec(key, "DESede");??
??????? IvParameterSpec iv = new IvParameterSpec(ivs);??
??????? cipher.init(mode, secureKey, iv);??
??????? return cipher.doFinal(content);??
??? }??
?
??? ?
??? public byte[] CryptByDesCbcMac(byte[] content) throws Exception {??
??????? BlockCipher engine = new DESEngine();??
??????? Mac mac = new CBCBlockCipherMac(engine, 64);??
??????? byte[] macText = new byte[engine.getBlockSize()];??
??????? mac.init(new KeyParameter(key));??
??????? mac.update(Padding(content, 64), 0, content.length);??
??????? mac.update(content, 0, content.length);??