您的位置:首页 > 编程语言 > Java开发

JAVA 常用加密方法

2014-02-12 23:15 246 查看

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?

importjava.io.ByteArrayOutputStream;

importjava.security.SecureRandom;

import java.util.Arrays;

importjavax.crypto.Cipher;

importjavax.crypto.SecretKey;

importjavax.crypto.SecretKeyFactory;

importjavax.crypto.spec.DESKeySpec;

importjavax.crypto.spec.DESedeKeySpec;

importjavax.crypto.spec.IvParameterSpec;

importjavax.crypto.spec.SecretKeySpec;

importorg.bouncycastle.crypto.BlockCipher;

importorg.bouncycastle.crypto.Mac;

importorg.bouncycastle.crypto.engines.DESEngine;

importorg.bouncycastle.crypto.macs.CBCBlockCipherMac;

importorg.bouncycastle.crypto.params.KeyParameter;

importcom.alibaba.common.lang.StringUtil;

importcom.huateng.commons.lang.convert.HexUtils;

public class ShfftDes {

//验证用密钥

privatebyte[] key ="000000000000000000000000".getBytes();

// privatebyte[] key = Hex.decode("00000000");

privatebyte[] ivs = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0};

privatestatic final StringDES_EDE ="DESede/ECB/NoPadding"; //定义 加密算法,可用DES,DESede,Blowfish //keybyte为加密密钥,长度为24字节 //src为被加密的数据缓冲区(源)

privatestatic final String DES_EDE_CBC ="DESede/CBC/NoPadding"; //定义 加密算法,可用DES,DESede,Blowfish //keybyte为加密密钥,长度为24字节 //src为被加密的数据缓冲区(源)

privatestatic final StringDES_CBC = "DES/CBC/NoPadding";

privatestatic final StringDES_ECB = "DES/ECB/PKCS5Padding";

publicbyte[] CryptByDes(byte[] content, int mode) throws Exception{

Cipher cipher =Cipher.getInstance(DES_ECB);

SecretKeyFactory keyFactory =SecretKeyFactory.getInstance("DES");

SecretKey secretKey = keyFactory.generateSecret(newDESKeySpec(key));

cipher.init(mode,secretKey);

returncipher.doFinal(content);

}

publicbyte[] CryptBy3Des(byte[] content, int mode) throws Exception{

Cipher cipher =Cipher.getInstance(DES_EDE);

SecretKey secretKey = new SecretKeySpec(key,"DESede");

cipher.init(mode,secretKey);

returncipher.doFinal(content);

}

publicbyte[] CryptByDesCbc(byte[] content, int mode) throws Exception{

Cipher cipher =Cipher.getInstance(DES_CBC);

SecretKey secureKey = new SecretKeySpec(key,"DES");

IvParameterSpec iv = newIvParameterSpec(ivs);

cipher.init(mode, secureKey,iv);

return cipher.doFinal(HexUtils.fromHex(newString(content)));

}

publicbyte[] CryptBy3DesCbc(byte[] content, int mode) throws Exception{

Cipher cipher =Cipher.getInstance(DES_EDE_CBC);

SecretKey secureKey = new SecretKeySpec(key,"DESede");

IvParameterSpec iv = newIvParameterSpec(ivs);

cipher.init(mode, secureKey,iv);

returncipher.doFinal(content);

}

publicbyte[] CryptByDesCbcMac(byte[] content) throws Exception{

BlockCipher engine = newDESEngine();

Mac mac = new CBCBlockCipherMac(engine,64);

byte[] macText = newbyte[engine.getBlockSize()];

mac.init(newKeyParameter(key));

mac.update(Padding(content, 64), 0,content.length);

mac.update(content, 0,content.length);

mac.doFinal(macText, 0);

return macText;

}

publicbyte[] ShFftCryptByDessdsCbc(byte[] content, int mode) throwsException {

byte[] ks1 = HexUtils.fromHex(newString(key));

byte[] ks = new byte[24];

System.arraycopy(ks1, 0, ks, 0,ks1.length);

System.arraycopy(ks1, 0, ks, ks1.length,8);

Cipher cipher =Cipher.getInstance(DES_EDE_CBC);

SecretKeyFactory keyFactory =null;

keyFactory =SecretKeyFactory.getInstance("DESede");

SecretKey secretKey =null;

secretKey = keyFactory.generateSecret(newDESedeKeySpec(ks));

IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0,0, 0, 0, 0 });

cipher.init(mode, secretKey,iv);

return cipher.doFinal(HexUtils.fromHex(newString(content)));

}

publicbyte[] mac(byte[] content) throws Exception{

int len;

byte plainData[];

byte encryptedData[];

len = (content.length / 8 + (content.length % 8 != 0 ? 1 : 0)) *8;

plainData = newbyte[len];

encryptedData = newbyte[8];

Arrays.fill(plainData, (byte)32);

System.arraycopy(content, 0, plainData, 0,content.length);

SecureRandom sr = newSecureRandom();

DESKeySpec dks = newDESKeySpec(key);

SecretKeyFactory keyFactory =null;

keyFactory =SecretKeyFactory.getInstance("DES");

SecretKey secretKey =keyFactory.generateSecret(dks);

Cipher cipher =Cipher.getInstance("DES/CBC/NoPadding");

IvParameterSpec iv = new IvParameterSpec(new byte[] { 0, 0, 0, 0,0, 0, 0, 0 });

cipher.init(1, secretKey, iv,sr);

System.arraycopy(cipher.doFinal(plainData), len - 8, encryptedData,0, 8);

return encryptedData;

}

publicbyte[] Padding(byte[] content, int block){

int contentLength =content.length;

int mod = contentLength %block;

if (mod != 0) {

int size = contentLength + block -mod;

// String s = newString(content);

// StringUtil.alignLeft(s, size, "");

byte[] s = newbyte[size];

System.arraycopy(content, 0, s, 0,content.length);

for (int i = content.length; i < size; i++){

s[i] = 32;

}

return s;

}

return content;

}

publicString Padding(String content, int block){

int contentLength =content.length();

int mod = contentLength %block;

if (mod != 0) {

int size = contentLength + block -mod;

String s = newString(content);

StringUtil.alignLeft(s, size, "");

return s;

}

return content;

}

public voidprintln(byte[] bs) {

for (byte b : bs) {

System.out.print(b + "");

}

System.out.println();

}

public voidprintlnByte(byte[] bs) {

for (byte b : bs) {

if (b < 0){

System.out.print((int) b + 256 + "");

} else {

System.out.print(b + "");

}

}

System.out.println();

}

public voidprintlnByteInt16(byte[] bs){

for (byte b : bs) {

System.out.print(Integer.toHexString((int) b) + "");

}

System.out.println();

}

publicString dumpBytes(byte[] bytes){

int i;

StringBuffer sb = newStringBuffer();

for (i = 0; i < bytes.length; i++){

int n = bytes[i] >= 0 ? bytes[i] : 256 +bytes[i];

String s =Integer.toHexString(n);

if (s.length() < 2){

s = "0" + s;

}

if (s.length() > 2){

s = s.substring(s.length() -2);

}

sb.append(s);

}

returnsb.toString().toUpperCase();

//return newBASE64Encoder().encode(bytes);

}

//一下程序将每2位16进制整数组装成一个字节

privateString hexString ="0123456789ABCDEF";

publicbyte[] decode(String bytes){

ByteArrayOutputStream baos = newByteArrayOutputStream(bytes.length() /2);

for (int i = 0; i < bytes.length(); i +=2)

baos.write((hexString.indexOf(bytes.charAt(i))<< 4 |hexString.indexOf(bytes

.charAt(i + 1))));

returnbaos.toByteArray();

}

publicbyte[] getKey() {

return key;

}

public voidsetKey(byte[] key) {

this.key = key;

}

publicbyte[] getIvs() {

return ivs;

}

public voidsetIvs(byte[] ivs) {

this.ivs = ivs;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: