1. 程式人生 > 實用技巧 >Java加密與解密之對稱加密演算法

Java加密與解密之對稱加密演算法

概述

採用單鑰密碼系統的加密方法,同一個金鑰可以同時用作資訊的加密和解密,這種加密方法稱為對稱加密,也稱為單金鑰加密。在對稱加密演算法中,DES演算法最具有代表性,DESede是DES演算法的變種,AES演算法則作為DES演算法的替代者。

DES

DES(Data Encryption Standard),即資料加密標準,是一種使用金鑰加密的塊演算法,1977年被美國聯邦政府的國家標準局確定為聯邦資料處理標準(FIPS),並授權在非密級政府通訊中使用,隨後該演算法在國際上廣泛流傳開來。

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class DesUtil {

    /**
     * DES加密
     * @param content 待加密資料
     * @param key 金鑰
     * @return
     * @throws Exception
     */
    public static String desEncrypt(String content, String key) throws Exception {
        //指定加密演算法、加密模式、填充模式
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        //建立加密規則:指定key和加密型別
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DES");
        //指定加密模式為加密,指定加密規則
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        //呼叫加密方法
        byte[] result = cipher.doFinal(content.getBytes());
        //用Base64編碼
        return new String(Base64.getEncoder().encode(result));
    }

    /**
     * DES解密
     * @param content 待解密資料
     * @param key 金鑰
     * @return
     * @throws Exception
     */
    public static String desDecrypt(String content, String key) throws Exception {
        //Base64解碼
        byte[] result = Base64.getDecoder().decode(content);
        //指定加密演算法、加密模式、填充模式
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        //建立加密規則:指定key和加密型別
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DES");
        //指定加密模式為解密,指定加密規則
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return new String(cipher.doFinal(result));
    }

    public static void main(String[] args) throws Exception {
        //key要8位,不然會報錯:java.security.InvalidKeyException: Wrong key size
        String key = "12345678";
        //待加密資料
        String content = "對稱加密演算法";

        //加密
        System.out.println(desEncrypt(content, key));//qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor

        //解密
        System.out.println(desDecrypt("qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor", key));//對稱加密演算法
    }
}

DESede

DESede是由DES改進後的一種對稱加密演算法,針對其金鑰長度偏短和迭代次數偏少等問題做了相應改進,提高了安全強度。


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class DesedeUtil {

    /**
     * Desede加密
     * @param content 待加密資料
     * @param key 金鑰
     * @return
     * @throws Exception
     */
    public static String desEncrypt(String content, String key) throws Exception {
        //指定加密演算法、加密模式、填充模式
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        //建立加密規則:指定key和加密型別
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DESede");
        //指定加密模式為加密,指定加密規則
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        //呼叫加密方法
        byte[] result = cipher.doFinal(content.getBytes());
        //用Base64編碼
        return new String(Base64.getEncoder().encode(result));
    }

    /**
     * Desede解密
     * @param content 待解密資料
     * @param key 金鑰
     * @return
     * @throws Exception
     */
    public static String desDecrypt(String content, String key) throws Exception {
        //Base64解碼
        byte[] result = Base64.getDecoder().decode(content);
        //指定加密演算法、加密模式、填充模式
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        //建立加密規則:指定key和加密型別
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "DESede");
        //指定加密模式為解密,指定加密規則
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return new String(cipher.doFinal(result));
    }

    public static void main(String[] args) throws Exception {
        //key要24位,不然會報錯:java.security.InvalidKeyException: Wrong key size
        String key = "123456781234567812345678";
        //待加密資料
        String content = "對稱加密演算法";

        //加密
        System.out.println(desEncrypt(content, key));//qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor

        //解密
        System.out.println(desDecrypt("qDhh3hjbd+/TESXcV0YxC4ArDlFR1Mor", key));//對稱加密演算法
    }
}

AES

AES(Advanced Encryption Standard),即高階加密標準,在密碼學中又稱Rijndael加密法,是美國聯邦政府採用的一種區塊加密標準。這個標準用來替代原先的DES,已經被多方分析且廣為全世界所使用。


import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class AesUtil {

    /**
     * aes加密
     * @param content 待加密資料
     * @param key 金鑰
     * @return
     * @throws Exception
     */
    public static String aesEncrypt(String content, String key) throws Exception {
        //指定加密演算法
        Cipher cipher = Cipher.getInstance("AES");
        //建立加密規則:指定key和加密型別
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        //指定加密模式為加密,指定加密規則
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        //呼叫加密方法
        byte[] result = cipher.doFinal(content.getBytes());
        //用Base64編碼
        return new String(Base64.getEncoder().encode(result));
    }

    /**
     * aes解密
     * @param content 待解密資料
     * @param key 金鑰
     * @return
     * @throws Exception
     */
    public static String aesDecrypt(String content, String key) throws Exception {
        //Base64解碼
        byte[] result = Base64.getDecoder().decode(content);
        //指定加密演算法
        Cipher cipher = Cipher.getInstance("AES");
        //建立加密規則:指定key和加密型別
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
        //指定加密模式為解密,指定加密規則
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return new String(cipher.doFinal(result));
    }

    public static void main(String[] args) throws Exception {
        //key要16/24/32位,不然會報錯:java.security.InvalidKeyException: Wrong key size
        String key = "12345678123456781234567812345678";
        String content = "對稱加密演算法";

        //加密
        System.out.println(aesEncrypt(content, key));//yrdeR6atwBX0yeXzudk/al6q8K61gyPylX7GfwsKP9w=

        //解密
        System.out.println(aesDecrypt("yrdeR6atwBX0yeXzudk/al6q8K61gyPylX7GfwsKP9w=", key));
    }
}

參考:Java加密與解密的藝術