1. 程式人生 > >DES/ECB/PKCS5Padding 加密解密演算法

DES/ECB/PKCS5Padding 加密解密演算法

    /*
* 加密
*/
    public static String EncryptString(String strText, String sKey) {
        // MD5加密
     //   String md5s = EncryptMD5.getMD5(strText);
        try {
            SecureRandom random = new SecureRandom();
            byte[] bkey  =(sKey.substring(0,8)).getBytes();
            DESKeySpec deskey = new DESKeySpec(bkey);
            // 建立一個金鑰工廠,然後用它把DESKeyspec轉換成
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(deskey);
            // cipher物件實際完成加密操作
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");

           // cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            // 用金鑰初化Cipher物件
            cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
            // 現在,獲取資料並加密
         //   // 正式執行加密操作
           // String str = md5s + strText;
            byte[] t = strText.getBytes("UTF-8");
            byte[] bResult = cipher.doFinal(t);
            // 1、加密完byte[] 後,需要將加密了的byte[] 轉換成base64儲存,如:
            // BASE64Encoder base64encoder = new BASE64Encoder();
            // String encode=base64encoder.encode(bytes);
            return base64Eecoder.encode(bResult);

        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
    * 解密
    */
    public static String DecryptString(String strText, String sKey) {
        // DES演算法要求有一個可信任的隨機數源
        SecureRandom random = new SecureRandom();
        // 建立一個DesKeySpec物件
        byte[] bkey = (sKey.substring(0,8)).getBytes();
        DESKeySpec desKey = null;
        try {
            desKey = new DESKeySpec(bkey);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        // 建立一個金鑰工廠
        SecretKeyFactory keyFactory = null;
        try {
            keyFactory = SecretKeyFactory.getInstance("DES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        // 將DESKeySpec物件轉換成SecretKey物件
        SecretKey secreKey = null;
        try {
            secreKey = keyFactory.generateSecret(desKey);
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
        // Cipher物件實際完成解密操作
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("DES");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        }
        // 用金鑰初始化Cipher物件
        try {
            cipher.init(Cipher.DECRYPT_MODE, secreKey, random);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        // 真正開始解密
        // 2、解密前,需要將加密後的字串從base64轉回來再解密,如:
        // BASE64Decoder base64decoder = new BASE64Decoder();
        // byte[] encodeByte = base64decoder.decodeBuffer(s);

        byte[] encodeByte;
        byte[] b;
        try {
            encodeByte = base64Decoder.decodeBuffer(new String( strText.getBytes(),"UTF-8"));
            b = cipher.doFinal(encodeByte);
            String s = new String(b, "UTF-8");
            return s;

        } catch (IOException e) {
            e.printStackTrace();
        }catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }