java中url加密处理

java中url加密处理packagetest importjava security Key importjava security SecureRandom importjavax crypto Cipher importjavax crypto KeyGenerator importsun misc BASE64Decode importsun misc BASE64Encode

package test; import java.security.Key; import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; public class ThreeDES { public static String crypt(String content,String password,int i){ try { KeyGenerator generator = KeyGenerator.getInstance("AES"); generator.init(new SecureRandom(password.getBytes())); Key key = generator.generateKey(); generator = null; if(i == 1){ return getEncString(content,key); } else if(i == 2){ return getDesString(content,key); } } catch (Exception e) { return null; } return null; } / * 加密String明文输入,String密文输出 * * @param strMing * @return */ private static String getEncString(String strMing,Key key) { byte[] byteMi = null; byte[] byteMing = null; String strMi = ""; BASE64Encoder base64en = new BASE64Encoder(); try { byteMing = strMing.getBytes("UTF8"); byteMi = getEncCode(byteMing,key); strMi = base64en.encode(byteMi); } catch (Exception e) { e.printStackTrace(); } finally { base64en = null; byteMing = null; byteMi = null; } return strMi; } / * 解密 以String密文输入,String明文输出 * * @param strMi * @return */ private static String getDesString(String strMi, Key key) { BASE64Decoder base64De = new BASE64Decoder(); byte[] byteMing = null; byte[] byteMi = null; String strMing = ""; try { byteMi = base64De.decodeBuffer(strMi); byteMing = getDesCode(byteMi,key); strMing = new String(byteMing, "UTF8"); } catch (Exception e) { e.printStackTrace(); } finally { base64De = null; byteMing = null; byteMi = null; } return strMing; } / * 加密以byte[]明文输入,byte[]密文输出 * * @param byteS * @return */ private static byte[] getEncCode(byte[] byteS,Key key) { byte[] byteFina = null; Cipher cipher; try { cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); byteFina = cipher.doFinal(byteS); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } / * 解密以byte[]密文输入,以byte[]明文输出 * * @param byteD * @return */ private static byte[] getDesCode(byte[] byteD,Key key) { Cipher cipher; byte[] byteFina = null; try { cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); byteFina = cipher.doFinal(byteD); } catch (Exception e) { e.printStackTrace(); } finally { cipher = null; } return byteFina; } public static void main(String[] args) { System.out.println(ThreeDES.crypt("bindMobile=&fenjihao=107", "bbbbb", 1)); System.out.println(ThreeDES.crypt("GT+F0fcFNGiq73/+FaX9pK9n9zqxwqz9sZ7MQdSp1BxWJXWn7EwnvniQpAOaGi0W", "bbbbb", 2)); } }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233373.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 生活小感慨

    生活琐事开一篇文来记录生活2021/12/9阻挡我使我停滞不前的,是浮躁的心2021/8/16我也并不希望我的生活是一成不变的.2021/8/13挑战软肋2021/8/5感谢每一个编

    2021年12月13日
    42
  • python模块之psutil详解

    python模块之psutil详解python模块之psutil详解一、psutil模块:1.psutil是一个跨平台库(http://pythonhosted.org/psutil/)能够轻松实现获取系统运行的进程和系统利用率(包括CPU、内存、磁盘、网络等)信息。它主要用来做系统监控,性能分析,进程管理。它实现了同等命令行工具提供的功能,如ps、top、lsof、netstat、ifconfig、who、df、kill…

    2022年6月12日
    35
  • 头歌c语言实训作业题解

    头歌c语言实训作业题解头歌c语言实训作业题解顺序结构程序设计1.加法运算2.不使用第3个变量,实现两个数的对调3.用宏定义常量4.数字分离5.计算总成绩和平均成绩6.求三角形面积7.立体几何计算题8.计算两个正整数的最大公约数选择结构程序设计选择结构程序设计进阶顺序结构程序设计1.加法运算本关任务:写一个加法程序,输入整数a,b,输出他们的和。解题代码:#include<stdio.h> intmain(void) { inta,b,c;//Pleaseinputa

    2022年5月12日
    43
  • jmap的使用以及内存溢出分析

    jmap的使用以及内存溢出分析jmap 的使用以及内存溢出分析 jmap java 内存映像工具 jmap MemoryMapfor 命令用于生成堆转储快照 一般称为 heapdump 或 dump 文件 还有几种方式获取 dump 文件 使用 JVM 参数选项 XX HeapDumpOnOu 参数 可以让虚拟机在 OOM 异常出现之后自动生成 dump 文件 通过 XX HeapDumpPath path 设置 dump 文件路径 有时候 dump 文件比较大的时候可能无法自动导出 这时候就需要使用 jmap dump 手动导

    2025年12月6日
    4
  • Spring Data JPA插入

    Spring Data JPA插入save 方法是面向业务的 每次插入都需要验证当前数据是否为新数据 Transactiona SextendsT Ssave Sentity if this entityInform isNew entity this em persist entity SextendsT

    2025年8月25日
    4
  • 内容大合集_十三大内容

    内容大合集_十三大内容文顶顶大神http://www.cnblogs.com/wendingding/p/3805088.html碎片知识大合集http://www.cnblogs.com/wujy/p/457161

    2022年8月5日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号