首页 > 代码库 > Des加密类

Des加密类

需要导入Base64.jar包

import java.io.IOException;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import Decoder.BASE64Decoder;
import Decoder.BASE64Encoder;

public class Des {

   private final static String DES = "DES";

   public static void main(String[] args) throws Exception {
      String data = "http://www.mamicode.com/强大的黑马51期";
      String key = "wang!@#$%";
      System.err.println(encrypt(data, key));
//    System.err.println(decrypt(encrypt(data, key), key));
   }

   /**
    * Description 根据键值进行加密
    * @param data 
    * @param key  加密键byte数组
    * @return
    * @throws Exception
    */
   public static String encrypt(String data, String key) throws Exception {
      byte[] bt = encrypt(data.getBytes(), key.getBytes());
      String strs = new BASE64Encoder().encode(bt);
      return strs;
   }

   /**
    * Description 根据键值进行解密
    * @param data
    * @param key  加密键byte数组
    * @return
    * @throws IOException
    * @throws Exception
    */
   public static String decrypt(String data, String key) throws IOException, Exception {
      if (data =http://www.mamicode.com/= null)>

 

Des加密类