經常使用的加密算法--對稱加密

wKiom1gEbU7Rk8J9AAAgh7lHOtQ358.png

對稱加密是最快速、最簡單的一種加密方式,加密與解密用的是相同的密鑰。對稱加密有不少種算法,因爲它效率很高,因此被普遍使用在不少加密協議的核心當中。java

對稱加密一般使用的是相對較小的密鑰,通常小於256 bit。由於密鑰越大,加密越強,但加密與解密的過程越慢。算法

常見的對稱加密算法:DES算法、3DES算法 、AES算法數組

特色:算法公開,計算量小,加密速度快,加密效率高。其安全性主要依賴於祕鑰的安全性。加密的時候使用的密鑰只有一個。安全


DES算法ide

對稱加密算法,明文按照64位進行分組,密鑰長64位,可是實際上只用56位參與DES運算,第八、1六、2四、3二、40、4八、5六、64位是校驗位,使得每一個密鑰都有一個奇數位。分組後的明文和56位的密鑰按位替代或交換的方法造成密文。加密

   /**
     * DES 生成密鑰
     * @return
     * @throws Exception
     */
    public static String genKeyDES() throws Exception {
        KeyGenerator keyGen = KeyGenerator.getInstance("DES");
        keyGen.init(56); //產生祕鑰爲56位
        SecretKey key = keyGen.generateKey();
        String base64Str = byte2base64(key.getEncoded());
        return base64Str;
    }
    /**
     *  DES 將字符串祕鑰轉換成SecretKey對象
     * @param base64Key
     * @return
     * @throws Exception
     */
    public static SecretKey loadKeyDES(String base64Key) throws Exception {
        byte[] bytes = base642byte(base64Key);
        SecretKey key = new SecretKeySpec(bytes, "DES");
        return key;
    }
    /**
     *DES 使用生成祕鑰對其進行對稱加密
     * @param source 加密的字節數組
     * @param key  祕鑰
     * @return
     * @throws Exception
     */
    public static byte[] encryptDES(byte[] source,SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(source);
        return bytes;
    }
    /**
     * DES使用生成祕鑰對其進行解密
     * @param source
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptDES(byte[] source,SecretKey key) throws Exception{
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(source);
        return bytes;
    }
    /**
     * base64 編碼
     * @param base64
     * @return
     * @throws IOException
     */
    private static byte[] base642byte(String base64) throws IOException {
         BASE64Decoder bs = new BASE64Decoder();
         return bs.decodeBuffer(base64); 
    }

    /**
     * base64 解碼
     * @param bytes
     * @return
     */
    private static String byte2base64(byte[] bytes) {
        BASE64Encoder bse = new BASE64Encoder();
        return bse.encode(bytes);
    }

AES算法spa

全世界所使用,對稱加密算法中最流行的算法之一。設計有三個密鑰長度(128,192,256位),比DES加密算法更加的安全設計

/**
     * AES 獲取公鑰
     * @return
     * @throws Exception
     */
    public static String genKeyAES() throws Exception {
        KeyGenerator keyGenerator= KeyGenerator.getInstance("AES");
        keyGenerator.init(128);
        SecretKey key = keyGenerator.generateKey();
        String base64Str = byte2base64(key.getEncoded());
        return base64Str;
    }
    /**
     * AES 生成SecretKey對象的祕鑰
     * @param base64Key
     * @return
     * @throws Exception
     */
    public static SecretKey loadKeyAES(String base64Key) throws Exception {
        byte[] bytes = base642byte(base64Key);
        SecretKey key = new SecretKeySpec(bytes, "AES");
        return key;
    }
    /**
     * AES  數據加密
     * @param source
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] encryptAES(byte[] source,SecretKey key) throws Exception{
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(source);
        return bytes;
    }
    /**
     * AES 數據解密
     * @param source
     * @param key
     * @return
     * @throws Exception
     */
    public static byte[] decryptAES(byte[] source,SecretKey key) throws Exception{
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] bytes = cipher.doFinal(source);
        return bytes;
    }
相關文章
相關標籤/搜索