DES加密和解密

/*
 * 文件名:DESCoder.java
 * 修改内容:新增
 */
package com.huawei.gts.impl.util.des;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.*;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * <p>功能简述</p>
 * <p>功能详述</p>
 * @author Lin Wen Xian KF74690
 * @version 1.0, 2013-1-22
 * @see
 * @since
*/
public class DESCoder
{
 // a weak key
    private final static BASE64Encoder base64encoder = new BASE64Encoder();
    private final static BASE64Decoder base64decoder = new BASE64Decoder();
    private static final String encoding = "UTF-8";//"GBK";
    private static final String sKey = "12345678901234567890123456789012";//"k34AAE4TAABMAQAA9HcAAIdCAADbRQAA";
    
    /**
     * 加密字符串
     */
    public static String ebotongEncrypto(String str)
    {
         String result = str;
         if (str != null && str.length() > 0)
         {
              try
              {
                   byte[] encodeByte = symmetricEncrypto(str.getBytes(encoding));
                   result = base64encoder.encode(encodeByte);
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
         }
         return result;
    }

    /**
     * 解密字符串  (物料价格把String变成Double)
     */
    public static double ebotongDecrypto(String str)
    {
         double result = 0;
         if (str != null && str.length() > 0)
         {
              try
              {
                   byte[] encodeByte = base64decoder.decodeBuffer(str);
            
                   byte[] decoder = symmetricDecrypto(encodeByte);
                   result = Double.parseDouble(new String(decoder, encoding));
                   
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
         }
         return result;
    }
    
    /**
     * 加密byte[]
     */
    public static byte[] ebotongEncrypto(byte[] str)
    {
         byte[] result = null;
         if (str != null && str.length > 0)
         {
              try
              {
                  System.out.println("str:"+str);
                   byte[] encodeByte = symmetricEncrypto(str);
                   System.out.println("encodeByte:"+encodeByte);
                   result = base64encoder.encode(encodeByte).getBytes();
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
         }
         return result;
    }

    /**
     * 解密byte[]
     */
    public static byte[] ebotongDecrypto(byte[] str)
    {
         byte[] result = null;
         if (str != null && str.length > 0)
         {
              try
              {
                   byte[] encodeByte = base64decoder.decodeBuffer(new String(str,encoding));
                   //byte[] encodeByte = base64decoder.decodeBuffer(new String(str));
                   byte[] decoder = symmetricDecrypto(encodeByte);
                   result = new String(decoder).getBytes(encoding);
                   result = new String(decoder).getBytes();
              }
              catch (Exception e)
              {
                  e.printStackTrace();
              }
         }
         return result;
    }
    
    /**
     * 对称加密字节数组并返回
     *
     * @param byteSource 需要加密的数据
     * @return           经过加密的数据
     * @throws Exception
     */
    public static byte[] symmetricEncrypto(byte[] byteSource) throws Exception
    {
         try
         {
              int mode = Cipher.ENCRYPT_MODE;
              SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
              byte[] keyData = sKey.getBytes();
              DESKeySpec keySpec = new DESKeySpec(keyData);
              Key key = keyFactory.generateSecret(keySpec);
              Cipher cipher = Cipher.getInstance("DES");
              cipher.init(mode, key);
        
              byte[] result = cipher.doFinal(byteSource);
              return result;
         }
         catch (Exception e)
         {
             throw e;
         }
         finally
         {
         }
    }

    /**
     * 对称解密字节数组并返回
     *
     * @param byteSource 需要解密的数据
     * @return           经过解密的数据
     * @throws Exception
     */
    public static byte[] symmetricDecrypto(byte[] byteSource) throws Exception
    {
         try
         {
              int mode = Cipher.DECRYPT_MODE;
              SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
              byte[] keyData = sKey.getBytes();
              DESKeySpec keySpec = new DESKeySpec(keyData);
              Key key = keyFactory.generateSecret(keySpec);
              Cipher cipher = Cipher.getInstance("DES");
              cipher.init(mode, key);
              byte[] result = cipher.doFinal(byteSource);
              return result;
         }
         catch (Exception e)
         {
             throw e;
         }
         finally
         {
         }
    }
    
    public static void main(String[] args)
    {
       /*start 加密 “你好”*/
        try
        {
            String dd=ebotongEncrypto("你好");
            System.out.println(dd); //输出为:QO2klVpoYT8=
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        /*end 加密 “你好”*/
        
        /*start 解密 为“你好”*/
        try
        {
            byte[] encodeByte = base64decoder.decodeBuffer("QO2klVpoYT8=");
            byte[] decoder = symmetricDecrypto(encodeByte);
            String dd= new String(decoder, encoding);
            System.out.println(dd); //输出为:你好
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        /*end 解密*/
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lijun_xiao2009

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值