DES 3DES Blowfish对称加密

package com.test.security;



import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class DESUtil {

static {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}

public static final String Algorithm_DES = "DES";

public static final String Algorithm_3DES = "DESede";

public static final String Algorithm_Blowfish = "Blowfish";

/***
* 创建key
*
* @param path
* @return
*/
public static Key createKey(String path, String type) {
Key key = null;
try {
KeyGenerator kg = KeyGenerator.getInstance(type);
kg.init(new SecureRandom());
key = kg.generateKey();
if (path != null) {
FileOutputStream fos = new FileOutputStream(path);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(key);
}
return key;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 加密
*
* @param key
* @param data
* @param type
* @return
*/
public static String encrypt(Key key, String data, String type) {
Cipher enCipher;
try {
enCipher = Cipher.getInstance(type);
enCipher.init(Cipher.ENCRYPT_MODE, key);
byte[] pasByte = enCipher.doFinal(data.getBytes("UTF-8"));
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(pasByte);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/**
* 解密
*
* @param key
* @param data
* @param type
* @return
*/
public static String dencrypt(Key key, String data, String type) {
Cipher deCipher;
try {
deCipher = Cipher.getInstance(type);
deCipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
return new String(pasByte, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/***
* 创建key
*
* @param path
* @return
*/
public static Key createKey(String path) {
Key key = null;
try {
KeyGenerator kg = KeyGenerator.getInstance("DES");
kg.init(new SecureRandom());
key = kg.generateKey();
if (path != null) {
FileOutputStream fos = new FileOutputStream(path);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(key);
}
return key;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/***
* 加密
*
* @param key
* @param data
* @return
*/
public static String encrypt(Key key, String data) {
Cipher enCipher;
try {
enCipher = Cipher.getInstance("DES");
enCipher.init(Cipher.ENCRYPT_MODE, key);
byte[] pasByte = enCipher.doFinal(data.getBytes("UTF-8"));
BASE64Encoder base64Encoder = new BASE64Encoder();
return base64Encoder.encode(pasByte);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

/***
* 解密
*
* @param key
* @param data
* @return
*/
public static String dencrypt(Key key, String data) {
Cipher deCipher;
try {
deCipher = Cipher.getInstance("DES");
deCipher.init(Cipher.DECRYPT_MODE, key);
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
return new String(pasByte, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static void main(String[] a) {
Key k = DESUtil.createKey(null);
String ab = "asdfasdf安科技时代将房间卡刷卡金东方卡卡的伤口就放假啊速度加快恢复更快回家ask将获得国际卡阿卡sdk金发科技";
String a1 = DESUtil.encrypt(k, ab);
System.out.println(a1);
String a2 = DESUtil.dencrypt(k, a1);
System.out.println(a2);
System.out.println();
Key k2=DESUtil.createKey(null, Algorithm_3DES);
String b1=DESUtil.encrypt(k2, ab, Algorithm_3DES);
System.out.println(b1);
String b2=DESUtil.dencrypt(k2, b1, Algorithm_3DES);
System.out.println(b2);
System.out.println();

Key k3=DESUtil.createKey(null, Algorithm_Blowfish);
String c1=DESUtil.encrypt(k3, ab, Algorithm_Blowfish);
System.out.println(c1);
String c2=DESUtil.dencrypt(k3, c1, Algorithm_Blowfish);
System.out.println(c2);
System.out.println();
}
}

你可能感兴趣的:(对称加密)