欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-19 23:15:58
本文实例讲述了java实现url加密处理的方法。分享给大家供大家参考,具体如下: package test; import java.security.key;...

本文实例讲述了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:关于加密解密感兴趣的朋友还可以参考本站在线工具:

url网址16进制加密工具:

md5在线加密工具:
http://tools.jb51.net/password/createmd5password

迅雷、快车、旋风url加密/解密工具:

在线散列/哈希算法加密工具:

在线md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160加密工具:

在线sha1/sha224/sha256/sha384/sha512加密工具:

更多关于java算法相关内容感兴趣的读者可查看本站专题:《java数据结构与算法教程》、《java操作dom节点技巧总结》、《java文件与目录操作技巧汇总》和《java缓存操作技巧汇总

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

上一篇:

下一篇: