国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java使用des加密解密示例分享

2019-11-26 15:41:41
字體:
來源:轉載
供稿:網友

復制代碼 代碼如下:

import java.security.Key;
import java.security.SecureRandom;
import java.security.spec.AlgorithmParameterSpec;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class Des
{
    public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
    private static Log log = LogFactory.getLog(Des.class);

    /**
     * DES算法,加密
     *
     * @param data 待加密字符串
     * @param key  加密私鑰,長度不能夠小于8位
     * @return 加密后的字節數組,一般結合Base64編碼使用
     * @throws CryptException 異常
     */
    public static String encode(String key,String data) throws Exception
    {
        return encode(key, data.getBytes());
    }
    /**
     * DES算法,加密
     *
     * @param data 待加密字符串
     * @param key  加密私鑰,長度不能夠小于8位
     * @return 加密后的字節數組,一般結合Base64編碼使用
     * @throws CryptException 異常
     */
    public static String encode(String key,byte[] data) throws Exception
    {
        try
        {
      DESKeySpec dks = new DESKeySpec(key.getBytes());

      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            //key的長度不能夠小于8位字節
            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            IvParameterSpec iv = new IvParameterSpec("********".getBytes());
            AlgorithmParameterSpec paramSpec = iv;
            cipher.init(Cipher.ENCRYPT_MODE, secretKey,paramSpec);

            byte[] bytes = cipher.doFinal(data);
            return Base64.encode(bytes);
        } catch (Exception e)
        {
            throw new Exception(e);
        }
    }

    /**
     * DES算法,解密
     *
     * @param data 待解密字符串
     * @param key  解密私鑰,長度不能夠小于8位
     * @return 解密后的字節數組
     * @throws Exception 異常
     */
    public static byte[] decode(String key,byte[] data) throws Exception
    {
        try
        {
         SecureRandom sr = new SecureRandom();
      DESKeySpec dks = new DESKeySpec(key.getBytes());
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            //key的長度不能夠小于8位字節
            Key secretKey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
            IvParameterSpec iv = new IvParameterSpec("********".getBytes());
            AlgorithmParameterSpec paramSpec = iv;
            cipher.init(Cipher.DECRYPT_MODE, secretKey,paramSpec);
            return cipher.doFinal(data);
        } catch (Exception e)
        {
//         e.printStackTrace();
            throw new Exception(e);
        }
    }

    /**
     * 獲取編碼后的值
     * @param key
     * @param data
     * @return
     * @throws Exception
     * @throws Exception
     */
    public static String decodeValue(String key,String data) throws Exception
    {
     byte[] datas;
     String value = null;

     datas = decode(key, Base64.decode(data));

  value = new String(datas);
  if (value.equals("")){
   throw new Exception();
  }
     return value;
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湟中县| 绿春县| 洛南县| 咸丰县| 疏附县| 丰县| 泉州市| 商南县| 凉城县| 治多县| 晴隆县| 阿坝| 上蔡县| 潢川县| 利辛县| 普兰店市| 余干县| 嵊泗县| 昌图县| 锦州市| 玛曲县| 湖北省| 马边| 宝山区| 西乡县| 浮山县| 四会市| 祁连县| 陇南市| 武邑县| 池州市| 呈贡县| 凤凰县| 英德市| 东安县| 松溪县| 定州市| 桃江县| 梁平县| 靖远县| 普兰店市|