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

java中DES加密解密

程序员文章站 2024-03-01 23:48:40
废话不多说,直接奉上代码: 代码一 package com.eabax.plugin.yundada.utils; import java.io.ioexce...

废话不多说,直接奉上代码:

代码一

package com.eabax.plugin.yundada.utils;
import java.io.ioexception;
import java.security.invalidkeyexception;
import java.security.nosuchalgorithmexception;
import java.security.securerandom;
import java.security.spec.invalidkeyspecexception;
import javax.crypto.badpaddingexception;
import javax.crypto.cipher;
import javax.crypto.illegalblocksizeexception;
import javax.crypto.nosuchpaddingexception;
import javax.crypto.secretkey;
import javax.crypto.secretkeyfactory;
import javax.crypto.spec.deskeyspec;
import org.apache.commons.codec.binary.base64;
import sun.misc.base64decoder;
public class desencrypthelper {
  private final static string des = "des";
  /**
   * 生成密钥
   * @param employeecode
   */
  public static string getdeskey(string encryptstr){
    if (!cachemanager.getcache().containskey("encryptkey_"+encryptstr)) {
      cachemanager.getcache().put("encryptkey_"+encryptstr, encryptstr+"tablemiyaokey");
    }
    string key = (string) cachemanager.getcache().get("encryptkey_"+encryptstr);
    return key;
  }
   /**
   * description 根据键值进行解密
   * @param data
   * @param key 加密键byte数组
   * @return
   * @throws ioexception
   * @throws exception
   */
  public static string decrypt(string data, string key) throws ioexception,
      exception {
    if (data == null)
      return null;
    base64decoder decoder = new base64decoder();
    byte[] buf = decoder.decodebuffer(data);
    byte[] bt = decrypt(buf,key.getbytes());
    return new string(bt);
  }
  /**
   * 对字符串加密 
   * @param str
   * @return
   * @throws invalidkeyexception
   * @throws illegalblocksizeexception
   * @throws badpaddingexception
   * @throws invalidkeyspecexception
   * @throws nosuchalgorithmexception
   * @throws nosuchpaddingexception
   */
  public static string getencryptstr(string str,string encryptstr) throws invalidkeyexception,
      illegalblocksizeexception, badpaddingexception,
      invalidkeyspecexception, nosuchalgorithmexception,
      nosuchpaddingexception {
    //获取key
    string key = getdeskey(encryptstr);
    //获取密钥
    secretkeyfactory factory = secretkeyfactory.getinstance("des");
    deskeyspec keyspec = new deskeyspec(key.getbytes());
    secretkey deskey = factory.generatesecret(keyspec);
    // cipher负责完成加密或解密工作
    cipher c = cipher.getinstance("des");
    // 根据密钥,对cipher对象进行初始化,decrypt_mode表示加密模式 
    c.init(cipher.encrypt_mode, deskey);
    byte[] src = str.getbytes();
    // 该字节数组负责保存加密的结果
    byte[] cipherbyte = c.dofinal(src);
    string enstr = new string(base64.encodebase64(cipherbyte));
    return enstr;
  }
   /**
   * description 根据键值进行解密
   * @param data
   * @param key 加密键byte数组
   * @return
   * @throws exception
   */
  private static byte[] decrypt(byte[] data, byte[] key) throws exception {
    // 生成一个可信任的随机数源
    securerandom sr = new securerandom();
    // 从原始密钥数据创建deskeyspec对象
    deskeyspec dks = new deskeyspec(key);
    // 创建一个密钥工厂,然后用它把deskeyspec转换成secretkey对象
    secretkeyfactory keyfactory = secretkeyfactory.getinstance(des);
    secretkey securekey = keyfactory.generatesecret(dks);
    // cipher对象实际完成解密操作
    cipher cipher = cipher.getinstance(des);
    // 用密钥初始化cipher对象
    cipher.init(cipher.decrypt_mode, securekey, sr);
    return cipher.dofinal(data);
  }
}

代码二

package com.sinosoft.olyvem.common;

import java.security.securerandom;

import javax.crypto.cipher;
import javax.crypto.secretkey;
import javax.crypto.secretkeyfactory;
import javax.crypto.spec.deskeyspec;

import sun.misc.base64encoder;

public class des ...{
  private byte[] deskey;

  public des(byte[] deskey) ...{
    this.deskey = deskey;
  }

  public byte[] doencrypt(byte[] plaintext) throws exception ...{
    // des算法要求有一个可信任的随机数源
    securerandom sr = new securerandom();
    byte rawkeydata[] = deskey;/**//* 用某种方法获得密匙数据 */
    // 从原始密匙数据创建deskeyspec对象
    deskeyspec dks = new deskeyspec(rawkeydata);
    // 创建一个密匙工厂,然后用它把deskeyspec转换成
    // 一个secretkey对象
    secretkeyfactory keyfactory = secretkeyfactory.getinstance("des");
    secretkey key = keyfactory.generatesecret(dks);
    // cipher对象实际完成加密操作
    cipher cipher = cipher.getinstance("des");
    // 用密匙初始化cipher对象
    cipher.init(cipher.encrypt_mode, key, sr);
    // 现在,获取数据并加密
    byte data[] = plaintext;/**//* 用某种方法获取数据 */
    // 正式执行加密操作
    byte encrypteddata[] = cipher.dofinal(data);
    return encrypteddata;
  }

  public byte[] dodecrypt(byte[] encrypttext) throws exception ...{
    // des算法要求有一个可信任的随机数源
    securerandom sr = new securerandom();
    byte rawkeydata[] = deskey; /**//* 用某种方法获取原始密匙数据 */
    // 从原始密匙数据创建一个deskeyspec对象
    deskeyspec dks = new deskeyspec(rawkeydata);
    // 创建一个密匙工厂,然后用它把deskeyspec对象转换成
    // 一个secretkey对象
    secretkeyfactory keyfactory = secretkeyfactory.getinstance("des");
    secretkey key = keyfactory.generatesecret(dks);
    // cipher对象实际完成解密操作
    cipher cipher = cipher.getinstance("des");
    // 用密匙初始化cipher对象
    cipher.init(cipher.decrypt_mode, key, sr);
    // 现在,获取数据并解密
    byte encrypteddata[] = encrypttext;/**//* 获得经过加密的数据 */
    // 正式执行解密操作
    byte decrypteddata[] = cipher.dofinal(encrypteddata);
    return decrypteddata;
  }

  public static void main(string[] args) throws exception ...{
    string key = "ftpxpass";
    string value = "olympic";
    base64encoder base64encoder = new base64encoder();

    des desencrypt = new des(key.getbytes());
    byte[] encrypttext = desencrypt.doencrypt(value.getbytes());
    //system.out.println("doencrypt  -  " + tohexstring(encrypttext));
    system.out.println("doencrypt  -  "
        + base64encoder.encode(encrypttext));
    byte[] decrypttext = desencrypt.dodecrypt("r9ngyckatdo=".getbytes());
    system.out.println("dodecrypt  -  " + new string(decrypttext));
    //system.out.println("dodecrypt  -  " + tohexstring(decrypttext));

  }

  public static string tohexstring(byte[] value) ...{
    string newstring = "";
    for (int i = 0; i < value.length; i++) ...{
      byte b = value[i];
      string str = integer.tohexstring(b);
      if (str.length() > 2) ...{
        str = str.substring(str.length() - 2);
      }
      if (str.length() < 2) ...{
        str = "0" + str;
      }
      newstring += str;
    }
    return newstring.touppercase();
  }

}

以上就是本文关于des加密解密的代码了,希望对大家学习java有所帮助。