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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • C语言fread函数_C语言fread

    C语言fread函数_C语言freadc语言中fread函数C语言中的fread()函数(fread()functioninC)Prototype:原型:size_tfread(void*buffer,size_tlength,size_tcount,FILE*filename);Parameters:参数:void*buffer,size_tlength,si…

    2025年11月4日
    2
  • Socket无限SocketTimeoutException真凶–WLAN助手

    Socket无限SocketTimeoutException真凶–WLAN助手看到标题你可能不知道我说的是什么鬼东西,但是如果你有类似的经历的话,那么恭喜你,也恭喜我自己,终于解决这个问题了。用过小米、华为等手机的都知道,当我们连接上一个不能上网的WIFI时,系统都会友好的给出“此WLAN无法访问互联网,请更换网络/切换为移动数据网络”等类似的提示,今天我就说下本人在这里面遇到的坑。背景:有个Android项目需要连接硬件设备的WIFI,然后通过socket进行通信…

    2022年10月21日
    2
  • 基于ZigBee和STM32的智能家居控制系统的设计与实现(五)–终结篇

    基于ZigBee和STM32的智能家居控制系统的设计与实现(五)–终结篇说明首先祝贺自己顺利的完成了毕业答辩工作,想起整个过程还是挺让自己感动的。最后还被评为优秀毕业设计,虽然并没有什么luan用,但是,马上毕业了,还是挺让人怀念的。整个资料从第一篇博客说起就说会全部开源的,期间承蒙各位朋友的支持,给与资助,在此感谢了。虽然资料中私人信息删除了一些,但是不免有疏漏,所以有关个人信息还挺各位删除掉,小

    2022年4月8日
    44
  • 如何使用npm安装yarn

    如何使用npm安装yarn先安装node,链接请看:https://blog.csdn.net/Doudou_Mylove/article/details/105112400再使用npm安装yarn:[root@localhost~]#npminstall-gyarn/usr/local/nodejs/bin/yarn->/usr/local/nodejs/lib/node_modules/…

    2022年8月30日
    3
  • smalldatetime和datetime的差别

    smalldatetime和datetime的差别碰上了这件事,才学到教训,一直以为smalldatetime和datetime的差别只是在于时间范围:smalldatetime的有效时间范围1900/1/1~2079/6/6datetime的有效时间范围1753/1/1~9999/12/31所以我判断如果该值不用到太远的日期范围,就会使用smalldatetime。但我忽略了更关键的差别,那就是smalldatetime只精准到分,而datet…

    2022年5月19日
    31
  • LAMP架构简介与概述 及服务安装

    LAMP架构简介与概述 及服务安装1、LAMP平台概述(1)LAMP平台概述LAMP架构是目前成熟的企业网站应用模式之一,指的是协同工作的一整台系统和相关软件,能够提供动态web站点服务及其应用开发环境LAMP是一个缩写词,具体包括Linux操作系统,Apache网站服务器,MySQL数据库服务器,PHP(或perl,Python)网页编程语言(2)构建LAMP平台顺序在构建LAMP平台时,各组件的安装顺序依次为Linux,Apache,MySQL,PHP其中Apache和MySQL的安装并没有严格的顺序要求,而PH

    2022年10月16日
    3

发表回复

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

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