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)java
[java] view plain copy算法
- 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";
-
- /**
- * 使用DES_ECB方法進行加密
- * @param content 需加密內容
- * @param key 加密的密鑰
- * @param mode 加密仍是解密
- * @return
- * @throws Exception
- */
- 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);
- }
-
- /**
- * 使用3DES方法進行加密
- * @param content 需加密內容
- * @param key 加密的密鑰
- * @param mode 加密仍是解密
- * @return
- * @throws Exception
- */
- 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);
- }
-
- /**
- * 使用DES_CBC方法進行加密
- * @param content 需加密內容
- * @param key 加密的密鑰
- * @param mode 加密仍是解密
- * @return
- * @throws Exception
- */
- 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)));
- }
-
- /**
- * 使用3DES_CBC方法進行加密
- * @param content 需加密內容
- * @param key 加密的密鑰
- * @param mode 加密仍是解密
- * @return
- * @throws Exception
- */
- 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);
- }
-
- /**
- * CBC的MAC加密
- * @param content
- * @param mode
- * @return
- * @throws Exception
- */
- 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);
- mac.doFinal(macText, 0);
- return macText;
- }
-
- /**
- * CBC的MAC加密
- * @param content
- * @param mode
- * @return
- * @throws Exception
- */
- public byte[] ShFftCryptByDessdsCbc(byte[] content, int mode) throws Exception {
- byte[] ks1 = HexUtils.fromHex(new String(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(new DESedeKeySpec(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(new String(content)));
- }
-
- public byte[] mac(byte[] content) throws Exception {
- int len;
- byte plainData[];
- byte encryptedData[];
- len = (content.length / 8 + (content.length % 8 != 0 ? 1 : 0)) * 8;
- plainData = new byte[len];
- encryptedData = new byte[8];
- Arrays.fill(plainData, (byte) 32);
- System.arraycopy(content, 0, plainData, 0, content.length);
- SecureRandom sr = new SecureRandom();
- DESKeySpec dks = new DESKeySpec(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;
- }
-
- /**
- * 根據傳入參數進行空格填充
- * @param content 須要填充的內容
- * @param block 填充的分塊大小
- * @return
- */
- public byte[] Padding(byte[] content, int block) {
- int contentLength = content.length;
- int mod = contentLength % block;
- if (mod != 0) {
- int size = contentLength + block - mod;
- // String s = new String(content);
- // StringUtil.alignLeft(s, size, " ");
- byte[] s = new byte[size];
- System.arraycopy(content, 0, s, 0, content.length);
- for (int i = content.length; i < size; i++) {
- s[i] = 32;
- }
- return s;
- }
- return content;
- }
-
- /**
- * 根據傳入參數進行空格填充
- * @param content 須要填充的內容
- * @param block 填充的分塊大小
- * @return
- */
- public String Padding(String content, int block) {
- int contentLength = content.length();
- int mod = contentLength % block;
- if (mod != 0) {
- int size = contentLength + block - mod;
- String s = new String(content);
- StringUtil.alignLeft(s, size, " ");
- return s;
- }
- return content;
- }
-
- /**
- * 打印byte數據
- * @param bs
- */
- public void println(byte[] bs) {
- for (byte b : bs) {
- System.out.print(b + " ");
- }
- System.out.println();
- }
-
- /**
- * 打印無符號byte數組
- * @param bs
- */
- public void printlnByte(byte[] bs) {
- for (byte b : bs) {
- if (b < 0) {
- System.out.print((int) b + 256 + " ");
- } else {
- System.out.print(b + " ");
- }
- }
- System.out.println();
- }
-
- /**
- * 以16進制形式打印BYTE數組
- * @param bs
- */
- public void printlnByteInt16(byte[] bs) {
- for (byte b : bs) {
- System.out.print(Integer.toHexString((int) b) + " ");
- }
- System.out.println();
- }
-
- /**
- * 將byte數組按1個byte分解爲2個字符
- * @param bytes
- * @return
- */
- public String dumpBytes(byte[] bytes) {
- int i;
- StringBuffer sb = new StringBuffer();
- 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);
- }
- return sb.toString().toUpperCase();
- //return new BASE64Encoder().encode(bytes);
- }
-
- // 一下程序將每2位16進制整數組裝成一個字節
- private String hexString = "0123456789ABCDEF";
-
- public byte[] decode(String bytes) {
- ByteArrayOutputStream baos = new ByteArrayOutputStream(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))));
- return baos.toByteArray();
- }
-
- public byte[] getKey() {
- return key;
- }
-
- public void setKey(byte[] key) {
- this.key = key;
- }
-
- public byte[] getIvs() {
- return ivs;
- }
-
- public void setIvs(byte[] ivs) {
- this.ivs = ivs;
- }
-
- }