程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java中DES加密解密

java中DES加密解密

編輯:關於JAVA

java中DES加密解密。本站提示廣大學習愛好者:(java中DES加密解密)文章只能為提供參考,不一定能成為您想要的結果。以下是java中DES加密解密正文


空話不多說,直接送上代碼:


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);
    }
}

以上就是本文關於DES加密解密的代碼了,願望對年夜家進修java有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved