Java AES/DES加密&解密

2017-05-15  本文已影响834人  FX_SKY

DES

DES加密&解密代码如下:


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.Charset;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 */
public class DES {
    public static final Charset UTF8 = Charset.forName("UTF-8");
    public static final String DES_ALGORITHM = "DES";

    public static byte[] encrypt(String data, String key) throws Exception {
        return encrypt(data.getBytes(UTF8), key.getBytes(UTF8));
    }
    public static byte[] encrypt(String data, byte[] key) throws Exception {
        return encrypt(data.getBytes(UTF8), key);
    }
    public static byte[] encrypt(byte[] data, String key) throws Exception {
        return encrypt(data, key.getBytes(UTF8));
    }
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecretKey secretKey = generateSecretKey(key);
        Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(String data, String key) throws Exception {
        return decrypt(data.getBytes(UTF8), key.getBytes(UTF8));
    }
    public static byte[] decrypt(String data, byte[] key) throws Exception {
        return decrypt(data.getBytes(UTF8), key);
    }
    public static byte[] decrypt(byte[] data, String key) throws Exception {
        return decrypt(data, key.getBytes(UTF8));
    }
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        SecretKey secretKey = generateSecretKey(key);
        Cipher cipher = Cipher.getInstance(DES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

    private static SecretKey generateSecretKey(byte[] key) throws NoSuchAlgorithmException {
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(key);
        KeyGenerator kg = KeyGenerator.getInstance(DES_ALGORITHM);
        kg.init(secureRandom);
        return kg.generateKey();
    }
}

AES

AES加密&解密代码如下:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.security.Key;

/**
 * ${DESCRIPTION}
 *
 * @author Ricky Fung
 */
public class AES {

    public static final String KEY_ALGORITHM = "AES";
    public static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    public static final Charset UTF8 = Charset.forName("UTF-8");

    public static byte[] encrypt(String data, String key) throws Exception{
        return encrypt(data.getBytes(UTF8), key.getBytes(UTF8));
    }
    public static byte[] encrypt(String data, byte[] key) throws Exception{
        return encrypt(data.getBytes(UTF8), key);
    }
    public static byte[] encrypt(byte[] data, String key) throws Exception{
        return encrypt(data, key.getBytes(UTF8));
    }
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception{
        Key k = genSecretKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, k);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(String data, String key) throws Exception {
        return decrypt(data.getBytes(UTF8), key.getBytes(UTF8));
    }
    public static byte[] decrypt(String data, byte[] key) throws Exception {
        return decrypt(data.getBytes(UTF8), key);
    }
    public static byte[] decrypt(byte[] data, String key) throws Exception {
        return decrypt(data, key.getBytes(UTF8));
    }
    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        Key k = genSecretKey(key);
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, k);
        return cipher.doFinal(data);
    }

    /**AES only supports key sizes of 16, 24 or 32 bytes*/
    public static Key genSecretKey(byte[] key) throws Exception{
        if(key.length==16 || key.length==24 || key.length==32){
            return new SecretKeySpec(key, KEY_ALGORITHM);
        }
        throw new IllegalArgumentException("AES only supports key sizes of 16, 24 or 32 bytes");
    }
}
上一篇下一篇

猜你喜欢

热点阅读