Java加密解密DESUtil、TripleDESUtil

hwl0420 8年前發布 | 3K 次閱讀 Java

[Java]代碼    

import java.security.Key;
import java.security.SecureRandom;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

public class DESUtil {

    /*
     * 
     * 注: 1、DES使用56位密鑰,以現代計算能力,24小時內即可被破解; 
     * 2、3DES(即Triple DES)是DES向AES過渡的加密算法(1999年,NIST將3-DES指定為過渡的加密標準)。使用3條56位的密鑰對 數據進行三次加密。
     * 3、DES算法的加密密鑰是根據用戶輸入的密碼生成的,該算法把64位密碼中的第8位、第16位、第24位、第32位、第40位、第48位、第56位、第64位作為奇偶校驗位,在計算密鑰時要忽略這8位.
     * 在generateKey()生成的隨機密鑰為8位(64bit)
     */

    public static final String ALGORITHM = "DES";
    public static final String TRANSFORMATION = "DES/ECB/PKCS5Padding";

    /**
     * 生成隨機密鑰
     * 
     * @return
     * @throws Exception
     */
    public static Key generateKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
        keyGenerator.init(new SecureRandom());
        Key key = keyGenerator.generateKey();
        return key;
    }

    /**
     * 生成固定密鑰
     * 
     * @param seed
     * @return
     * @throws Exception
     */
    public static Key generateKey(byte[] seed) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
        keyGenerator.init(new SecureRandom(seed));
        Key key = keyGenerator.generateKey();
        return key;
    }

    /**
     * 生成固定密鑰
     * 
     * @param password
     * @return
     * @throws Exception
     */
    public static Key generateKey(String password) throws Exception {
        return generateKey(password.getBytes());
    }

    /**
     * 執行加密
     * 
     * @param content
     * @param key       長度必須為8位,即64bit
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] content, byte[] key) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
        byte[] output = cipher.doFinal(content);
        return output;
    }

    /**
     * 執行加密
     * 
     * @param content
     * @param password
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] content, String password) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, generateKey(password));
        byte[] output = cipher.doFinal(content);
        return output;
    }

    /**
     * 執行解密
     * 
     * @param content
     * @param key       長度必須為8位,即64bit
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] content, byte[] key) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
        byte[] output = cipher.doFinal(content);
        return output;
    }

    /**
     * 執行解密
     * 
     * @param content
     * @param password
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] content, String password) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, generateKey(password));
        byte[] output = cipher.doFinal(content);
        return output;
    }

    public static void main(String[] args) throws Exception {

        System.out.println(Arrays.toString(encrypt("DES使用56位密鑰,以現代計算能力".getBytes(), "012345")));
        System.out.println(new String(decrypt(encrypt("DES使用56位密鑰,以現代計算能力".getBytes(), "012345"), "012345")));

        System.out.println(Arrays.toString(encrypt("DES使用56位密鑰,以現代計算能力".getBytes(), "01234567".getBytes())));
        System.out.println(new String(decrypt(encrypt("DES使用56位密鑰,以現代計算能力".getBytes(), "01234567".getBytes()), "01234567".getBytes())));

        /*
         * 控制臺輸出:
         *
         * [117, -109, -80, -75, 51, -86, -57, -109, -94, 58, 38, 94, 39, -107, -60, 65, -122, -7, -124, 2, -23, -30, -98, -64, 90, 26, 15, 82, 84, 102, -108, -3, -68, -4, 110, -86, -106, 19, -65, -110, 2, 15, 49, -79, -98, 38, -39, 6]
         * DES使用56位密鑰,以現代計算能力
         * [-6, 63, -15, 73, -28, 85, 125, 64, 6, 55, -63, 99, 114, 21, 108, -49, 19, 11, -15, -126, 36, -92, 62, 112, -40, 64, -102, 127, -94, -53, -89, 33, 72, -20, -126, -90, 105, -37, -68, -46, -61, -36, 5, -103, 27, 32, 84, 28]
         * DES使用56位密鑰,以現代計算能力
         */
    }

}

[Java]代碼    

import java.security.Key;
import java.security.SecureRandom;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;


public class TripleDESUtil {

    public static final String ALGORITHM = "DESede";
    public static final String TRANSFORMATION = "DESede/ECB/PKCS5Padding";

    /**
     * 生成隨機密鑰
     * 
     * @return
     * @throws Exception
     */
    public static Key generateKey() throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
        keyGenerator.init(new SecureRandom());
        Key key = keyGenerator.generateKey();
        return key;
    }

    /**
     * 生成固定密鑰
     * 
     * @param seed
     * @return
     * @throws Exception
     */
    public static Key generateKey(byte[] seed) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM);
        keyGenerator.init(new SecureRandom(seed));
        Key key = keyGenerator.generateKey();
        return key;
    }

    /**
     * 生成固定密鑰
     * 
     * @param password
     * @return
     * @throws Exception
     */
    public static Key generateKey(String password) throws Exception {
        return generateKey(password.getBytes());
    }

    /**
     * 執行加密
     * 
     * @param content
     * @param key       長度必須為8位,即64bit
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] content, byte[] key) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
        byte[] output = cipher.doFinal(content);
        return output;
    }

    /**
     * 執行加密
     * 
     * @param content
     * @param password
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] content, String password) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, generateKey(password));
        byte[] output = cipher.doFinal(content);
        return output;
    }

    /**
     * 執行解密
     * 
     * @param content
     * @param key       長度必須為8位,即64bit
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] content, byte[] key) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, ALGORITHM));
        byte[] output = cipher.doFinal(content);
        return output;
    }

    /**
     * 執行解密
     * 
     * @param content
     * @param password
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] content, String password) throws Exception {
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, generateKey(password));
        byte[] output = cipher.doFinal(content);
        return output;
    }

    public static void main(String[] args) throws Exception {

        System.out.println(Arrays.toString(encrypt("使用3條56位的密鑰對 數據進行三次加密。".getBytes(), "012345")));
        System.out.println(new String(decrypt(encrypt("使用3條56位的密鑰對 數據進行三次加密。".getBytes(), "012345"), "012345")));

        System.out.println(Arrays.toString(encrypt("使用3條56位的密鑰對 數據進行三次加密。".getBytes(), "012345670123456701234567".getBytes())));
        System.out.println(new String(decrypt(encrypt("使用3條56位的密鑰對 數據進行三次加密。".getBytes(), "012345670123456701234567".getBytes()), "012345670123456701234567".getBytes())));

        /*
         * 控制臺輸出:
         * 
         * [-46, 78, 30, 115, 9, -79, -59, 46, 8, 46, -1, -92, -53, -42, -86, 15, -8, -25, -18, 92, 100, -109, 68, -9, -42, 80, 42, 60, -62, -43, 41, 84, -114, 52, 92, -115, -92, 16, -15, 3, 36, -105, 69, 118, -126, 61, 81, 121, -99, -89, -67, 91, 70, 19, 85, 9]
         * 使用3條56位的密鑰對 數據進行三次加密。
         * [38, 108, 104, -124, 124, -73, -66, -121, -43, -41, -102, 74, -71, -98, 71, 4, 7, -50, 78, -28, 39, -103, 115, -93, -88, -107, -113, 89, -41, 55, -93, 111, -43, -120, -47, -50, -2, -104, 107, 105, 114, 45, 120, 40, 103, 64, 19, 60, 37, 18, 100, 71, 106, -66, 123, -66]
         * 使用3條56位的密鑰對 數據進行三次加密。
         */
    }

}
 本文由用戶 hwl0420 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!