前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java url加密_Java实现url加密处理的方法示例

java url加密_Java实现url加密处理的方法示例

作者头像
全栈程序员站长
发布2022-11-01 16:20:20
1.6K0
发布2022-11-01 16:20:20
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

本文实例讲述了Java实现url加密处理的方法。分享给大家供大家参考,具体如下:

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=13023130171&fenjihao=107”, “bbbbb”, 1));

System.out.println(ThreeDES.crypt(“GT+F0fcFNGiq73/+FaX9pK9n9zqxwqz9sZ7MQdSp1BxWJXWn7EwnvniQpAOaGi0W”, “bbbbb”, 2));

}

}

PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:http://tools.jb51.net/password/hash_md5_sha

希望本文所述对大家java程序设计有所帮助。

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年10月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档