博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DES加密+Base64转换
阅读量:6275 次
发布时间:2019-06-22

本文共 4900 字,大约阅读时间需要 16 分钟。

hot3.png

package httpVisit.action; import javax.crypto.Cipher;import javax.crypto.SecretKey;import javax.crypto.SecretKeyFactory;import javax.crypto.spec.DESKeySpec;import javax.crypto.spec.IvParameterSpec; import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder; /** * @author:Heweipo * @version 1.00 *  */public class DesCoderUtil {     private static byte[] keys = { 1, -1, 1, -1, 1, -1, 1, -1 };     private static String key = "leagsoft";     public static String getKey() {        return key;    }     public static void setKey(String key) {        DesCoderUtil.key = key;    }     /**     *      * 

* 对password进行MD5加密 * @param source * @return * @return byte[] * author: Heweipo */ public static byte[] getMD5(byte[] source) { byte tmp[] = null; try { java.security.MessageDigest md = java.security.MessageDigest .getInstance("MD5"); md.update(source); tmp = md.digest(); } catch (Exception e) { e.printStackTrace(); } return tmp; } /** * *

* 采用JDK内置类进行真正的加密操作 * @param byteS * @param password * @return * @return byte[] * author: Heweipo */ private static byte[] encryptByte(byte[] byteS, byte password[]) { byte[] byteFina = null; try {// 初始化加密/解密工具 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(keys); cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv); byteFina = cipher.doFinal(byteS); } catch (Exception e) { throw new RuntimeException(e); } return byteFina; } /** * *

* 采用JDK对应的内置类进行解密操作 * @param byteS * @param password * @return * @return byte[] * author: Heweipo */ private static byte[] decryptByte(byte[] byteS, byte password[]) { byte[] byteFina = null; try {// 初始化加密/解密工具 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(keys); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byteFina = cipher.doFinal(byteS); } catch (Exception e) { throw new RuntimeException(e); } return byteFina; } /** * *

* Des加密strMing,然后base64转换 * @param strMing * @param md5key * @return * @return String * author: Heweipo */ public static String encryptStr(String strMing, byte md5key[]) { byte[] byteMi = null; byte[] byteMing = null; String strMi = ""; try { byteMing = strMing.getBytes("utf-8"); byteMi = encryptByte(byteMing, md5key); BASE64Encoder base64Encoder = new BASE64Encoder(); strMi = base64Encoder.encode(byteMi); } catch (Exception e) { throw new RuntimeException(e); } finally { byteMing = null; byteMi = null; } return strMi; } /** * *

* Base64转换strMi,然后进行des解密 * @param strMi * @param md5key * @return * @return String * author: Heweipo */ public static String decryptStr(String strMi, byte md5key[]) { byte[] byteMing = null; String strMing = ""; try { BASE64Decoder decoder = new BASE64Decoder(); byteMing = decoder.decodeBuffer(strMi); byteMing = decryptByte(byteMing, md5key); strMing = new String(byteMing); } catch (Exception e) { throw new RuntimeException(e); } finally { byteMing = null; } return strMing; } public static void main(String[] args) { String[] keys = {"leagsoft","leagsoftpo","leagsoftiu","leagsoftyy","leagsoftew","leagsoftmm"}; for(String key : keys){ String data = "我是learn_more,who are you?"; try{ System.out.println(DesCoderUtil.encryptStr(data, DesCoderUtil.getMD5(key.getBytes("utf-8")))); System.out.println("--------------"); System.out.println(DesCoderUtil.decryptStr(DesCoderUtil.encryptStr(data, DesCoderUtil.getMD5(key.getBytes("utf-8"))), DesCoderUtil.getMD5(key.getBytes("utf-8")))); }catch(Exception e){ e.printStackTrace(); } } } }

转载于:https://my.oschina.net/weiweiblog/blog/477323

你可能感兴趣的文章
spring jpa 配置详解
查看>>
IOE,为什么去IOE?
查看>>
java 用反射简单应用,将Object简单转换成map
查看>>
Storm中的Worker
查看>>
dangdang.ddframe.job中页面修改表达式后进行检查
查看>>
Web基础架构:负载均衡和LVS
查看>>
Linux下c/c++相对路径动态库的生成与使用
查看>>
SHELL实现跳板机,只允许用户执行少量允许的命令
查看>>
SpringBoot 整合Redis
查看>>
nodejs安装以及环境配置(很好的node安装和配置文章,少走很多弯路)
查看>>
2014上半年大片早知道
查看>>
Android 6.0指纹识别App开发案例
查看>>
ios runtime基础知识
查看>>
正文提取算法
查看>>
Arcgis Engine(ae)接口详解(8):临时元素(element)
查看>>
大数据技术核心之ETL
查看>>
轻松学PHP
查看>>
Android android.support.design包
查看>>
CheckBox:屏蔽setChecked方法对OnCheckedChangeListener的影
查看>>
java线程池
查看>>