DES 加密、解密JAVA ISO ANDRIOD 通用
2018-03-09 09:47
218 查看
public class DesUtils { private final static String DES = "DES"; // 加密键byte数组 private final static String key = "20171225%API";// private final static Logger logger = LoggerFactory.getLogger(DesUtils.class); /** * Description 根据键值进行加密 * * @param data * @return * @throws Exception */ public static String encrypt(String data) throws EncryptExcption { try { byte[] bt = encrypt(data.getBytes("utf-8"), key.getBytes()); String strs = new BASE64Encoder().encode(bt); String str = Des.strEnc(data); return strs; } catch (Exception e) { throw new EncryptExcption("加密出错"); } } /** * 解密参数,返回JsonString * @param data * @return * @throws DecryptExcption */ public static String decrypt(String data) throws DecryptExcption { try { if (data == null) return null; BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf, key.getBytes()); return new String(bt, "UTF-8"); //String dec = Des.strDec(data); //return dec; } catch (Exception e) { logger.error(e.toString()); throw new DecryptExcption("解密出错"); } } /** * 解密参数,返回Object * @param data * @param cla * @return * @throws DecryptExcption */ public static Object decrypt(String data,Class cla) throws DecryptExcption { try { String s = DesUtils.decrypt(data); JSONObject j = JSONObject.fromObject(s); Object o = JSONObject.toBean(j, cla); return o; } catch (Exception e) { logger.error(e.toString()); throw new DecryptExcption("解密出错"); } } public static JSONArray decryptToJSONArry(String data,String qualifyList) throws DecryptExcption{ String s = DesUtils.decrypt(data); JSONObject jo = JSONObject.fromObject(s); String sObj=jo.getString(qualifyList); JSONArray j = JSONArray.fromObject(sObj); return j; } public static JSONObject decryptJSONObject(String data)throws DecryptExcption{ String s = DesUtils.decrypt(data); JSONObject jo = JSONObject.fromObject(s); return jo; } /** * 解密参数,返回Map * @param data * @return * @throws DecryptExcption */ public static HashMap decryptToMap(String data) throws DecryptExcption { try { String s = DesUtils.decrypt(data); JSONObject j = JSONObject.fromObject(s); HashMap<String, Object> map = (HashMap<String, Object>) JSONObject.toBean(j, HashMap.class); return map; } catch (Exception e) { throw new DecryptExcption("解密出错"); } } public static HashMap decryptToMaps(String data) throws DecryptExcption { try { String s = DesUtils.decrypt(data); JSONObject j = JSONObject.fromObject(s); HashMap<String, Object> map = (HashMap<String, Object>) JSONObject.toBean(j, HashMap.class); return map; } catch (Exception e) { throw new DecryptExcption("解密出错"); } } /** * json转map * @param data * @return * @throws DecryptExcption */ public static HashMap jsonToMap(String data) throws DecryptExcption { JSONObject j = JSONObject.fromObject(data); HashMap<String, Object> map = (HashMap<String, Object>) JSONObject.toBean(j, HashMap.class); return map; } /** * Description 根据键值进行加密 * @param data * @param key 加密键byte数组 * @return * @throws Exception */ private static byte[] encrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); } /** * Description 根据键值进行解密 * @param data * @param key 加密键byte数组 * @return * @throws Exception */ private static byte[] decrypt(byte[] data, byte[] key) throws Exception { // 生成一个可信任的随机数源 SecureRandom sr = new SecureRandom(); // 从原始密钥数据创建DESKeySpec对象 DESKeySpec dks = new DESKeySpec(key); // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance(DES); // 用密钥初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, sr); return cipher.doFinal(data); } /* public static String SHA1(String decript) { try { MessageDigest digest = MessageDigest .getInstance("SHA-1"); digest.update(decript.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); // 字节数组转换为 十六进制 数 for (int i = 0; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } */ /** * 解密参数,返回Map * @param data * @return * @throws DecryptExcption */ public static HashMap stringToMap(String data) throws DecryptExcption { try { JSONObject j = JSONObject.fromObject(data); HashMap<String, Object> map = (HashMap<String, Object>) JSONObject.toBean(j, HashMap.class); return map; } catch (Exception e) { throw new DecryptExcption("解析出错"); } } }
相关文章推荐
- Java加密解密快速入门上篇【包括MD5、BASE64、DES、RSA等算法】
- Java通过DES实现加密和解密
- Java中使用DES进行加密解密
- js前台加密 java后台解密(des)
- php 3DES|DES 加密解密(通用)
- java php DES 加密解密
- DES解密 - java加密 php解密
- Java和.net加密解密互通DES
- Java加密解密快速入门下篇【包括MD5、BASE64、DES、RSA等算法】
- android 和java平台通用的AES加密解密
- java与.NET通用的3DES加密解密方法
- Java加密解密快速入门下篇【包括MD5、BASE64、DES、RSA等算法】
- Java和C#交叉加密解密方法(DES)
- java 和 C# 通用加密解密
- 加密解密算法java实现(2)—DES
- OC与JAVA通用的AES加密解密
- C# 與JAVA 共用的DES 加密解密方法
- java加密解密___des的简单实现
- ◆JAVA加密解密-DES
- object-c 和 java 可以通用的DES散列加密