| | |
| | | package org.yeshi.utils.encrypt;
|
| | |
|
| | | import java.security.InvalidKeyException;
|
| | | import java.security.Key;
|
| | | import java.security.NoSuchAlgorithmException;
|
| | | import java.security.spec.AlgorithmParameterSpec;
|
| | | import java.security.spec.InvalidKeySpecException;
|
| | |
|
| | | import javax.crypto.Cipher;
|
| | | import javax.crypto.SecretKeyFactory;
|
| | | import javax.crypto.spec.DESKeySpec;
|
| | | import javax.crypto.spec.IvParameterSpec;
|
| | |
|
| | | import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
|
| | |
|
| | | public class DESUtil {
|
| | | /**
|
| | | * [加密]DES-->BASE64-->密文
|
| | | * |
| | | * [解密]BASE64->DES-->明文
|
| | | * |
| | | * 秘钥和向量要双方约定一致
|
| | | * |
| | | * DES加密的私钥,必须是8位长的字符串
|
| | | */
|
| | | private static AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现
|
| | | private static Key key = null;
|
| | |
|
| | | static {
|
| | |
|
| | | }
|
| | |
|
| | | public static String encode(String data, String descKeyBase64, String desIVBase64) {
|
| | | try {
|
| | | DESKeySpec keySpec = new DESKeySpec(Base64.decode(descKeyBase64));
|
| | | // 设置密钥参数
|
| | | iv = new IvParameterSpec(Base64.decode(desIVBase64));// 设置向量
|
| | | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
|
| | | key = keyFactory.generateSecret(keySpec);// 得到密钥对象
|
| | | System.out.println(key.toString());
|
| | | } catch (InvalidKeyException e) {
|
| | | e.printStackTrace();
|
| | | } catch (NoSuchAlgorithmException e) {
|
| | | e.printStackTrace();
|
| | | } catch (InvalidKeySpecException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | String result = "";
|
| | | try {
|
| | | Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher
|
| | | enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量
|
| | | byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8"));
|
| | | result = Base64.encode(pasByte);
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return result;
|
| | | }
|
| | |
|
| | | public static String decode(String data, String descKeyBase64, String desIVBase64) {
|
| | | try {
|
| | | DESKeySpec keySpec = new DESKeySpec(Base64.decode(descKeyBase64));
|
| | | // 设置密钥参数
|
| | | iv = new IvParameterSpec(Base64.decode(desIVBase64));// 设置向量
|
| | | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂
|
| | | key = keyFactory.generateSecret(keySpec);// 得到密钥对象
|
| | | System.out.println(key.toString());
|
| | | } catch (InvalidKeyException e) {
|
| | | e.printStackTrace();
|
| | | } catch (NoSuchAlgorithmException e) {
|
| | | e.printStackTrace();
|
| | | } catch (InvalidKeySpecException e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | |
|
| | | String result = "";
|
| | | Cipher deCipher;
|
| | | try {
|
| | | deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
|
| | | deCipher.init(Cipher.DECRYPT_MODE, key, iv);
|
| | | byte[] pasByte = deCipher.doFinal(Base64.decode(data));
|
| | | result = new String(pasByte, "UTF-8");
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | return result;
|
| | |
|
| | | }
|
| | | package org.yeshi.utils.encrypt; |
| | | |
| | | import java.security.InvalidKeyException; |
| | | import java.security.Key; |
| | | import java.security.NoSuchAlgorithmException; |
| | | import java.security.spec.AlgorithmParameterSpec; |
| | | import java.security.spec.InvalidKeySpecException; |
| | | |
| | | import javax.crypto.Cipher; |
| | | import javax.crypto.SecretKeyFactory; |
| | | import javax.crypto.spec.DESKeySpec; |
| | | import javax.crypto.spec.IvParameterSpec; |
| | | |
| | | import com.sun.org.apache.xerces.internal.impl.dv.util.Base64; |
| | | |
| | | public class DESUtil { |
| | | /** |
| | | * [加密]DES-->BASE64-->密文 |
| | | * |
| | | * [解密]BASE64->DES-->明文 |
| | | * |
| | | * 秘钥和向量要双方约定一致 |
| | | * |
| | | * DES加密的私钥,必须是8位长的字符串 |
| | | */ |
| | | private static AlgorithmParameterSpec iv = null;// 加密算法的参数接口,IvParameterSpec是它的一个实现 |
| | | private static Key key = null; |
| | | |
| | | static { |
| | | |
| | | } |
| | | |
| | | public static String encode(String data, String descKeyBase64, String desIVBase64) { |
| | | try { |
| | | DESKeySpec keySpec = new DESKeySpec(Base64.decode(descKeyBase64)); |
| | | // 设置密钥参数 |
| | | iv = new IvParameterSpec(Base64.decode(desIVBase64));// 设置向量 |
| | | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂 |
| | | key = keyFactory.generateSecret(keySpec);// 得到密钥对象 |
| | | System.out.println(key.toString()); |
| | | } catch (InvalidKeyException e) { |
| | | e.printStackTrace(); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | e.printStackTrace(); |
| | | } catch (InvalidKeySpecException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | String result = ""; |
| | | try { |
| | | Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");// 得到加密对象Cipher |
| | | enCipher.init(Cipher.ENCRYPT_MODE, key, iv);// 设置工作模式为加密模式,给出密钥和向量 |
| | | byte[] pasByte = enCipher.doFinal(data.getBytes("utf-8")); |
| | | result = Base64.encode(pasByte); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | } |
| | | |
| | | public static String decode(String data, String descKeyBase64, String desIVBase64) { |
| | | try { |
| | | DESKeySpec keySpec = new DESKeySpec(Base64.decode(descKeyBase64)); |
| | | // 设置密钥参数 |
| | | iv = new IvParameterSpec(Base64.decode(desIVBase64));// 设置向量 |
| | | SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");// 获得密钥工厂 |
| | | key = keyFactory.generateSecret(keySpec);// 得到密钥对象 |
| | | System.out.println(key.toString()); |
| | | } catch (InvalidKeyException e) { |
| | | e.printStackTrace(); |
| | | } catch (NoSuchAlgorithmException e) { |
| | | e.printStackTrace(); |
| | | } catch (InvalidKeySpecException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | String result = ""; |
| | | Cipher deCipher; |
| | | try { |
| | | deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); |
| | | deCipher.init(Cipher.DECRYPT_MODE, key, iv); |
| | | byte[] pasByte = deCipher.doFinal(Base64.decode(data)); |
| | | result = new String(pasByte, "UTF-8"); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return result; |
| | | |
| | | } |
| | | } |