对内容进行加解密的实例

package com.yonge.messagedigest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;

public class SecurityUtil {

    private final static String ENCRYPT_ALGORITHM    = "AES";

    private final static String SECRET_KEY_FILE_NAME = "secret.key";

    /**
     * 生成加解密的密钥
     * @param algorithm
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public static SecretKey generateSecretKey(String algorithm) throws NoSuchAlgorithmException,
                                                               IOException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
        SecretKey secretKey = keyGenerator.generateKey();
        //保存到文件中
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = new FileOutputStream(new File(SECRET_KEY_FILE_NAME));
            oos = new ObjectOutputStream(fos);
            oos.writeObject(secretKey);
        } finally {
            if (fos != null) {
                fos.close();
            }
            if (oos != null) {
                oos.close();
            }
        }

        return secretKey;
    }

    /**
     * 生成加解密的密钥
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public static SecretKey generateSecretKey() throws NoSuchAlgorithmException, IOException {
        return generateSecretKey(ENCRYPT_ALGORITHM);
    }

    /**
     * 加密内容,并返回
     * @param data
     * @param algorithm
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] encrypt(byte[] data, String algorithm) throws NoSuchAlgorithmException,
                                                               IOException, ClassNotFoundException,
                                                               NoSuchPaddingException,
                                                               InvalidKeyException,
                                                               IllegalBlockSizeException,
                                                               BadPaddingException {
        SecretKey secretKey = getSecretKeyFromLocal();
        if (secretKey == null) {
            secretKey = generateSecretKey(algorithm);
        }
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);

        return cipher.doFinal(data);
    }

    /**
     * 加密内容,并返回
     * @param data
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] encrypt(byte[] data) throws NoSuchAlgorithmException, IOException,
                                             ClassNotFoundException, NoSuchPaddingException,
                                             InvalidKeyException, IllegalBlockSizeException,
                                             BadPaddingException {
        return encrypt(data, ENCRYPT_ALGORITHM);
    }

    /**
     * 解密内容,并返回
     * @param data
     * @param algorithm
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] decrypt(byte[] data, String algorithm) throws NoSuchAlgorithmException,
                                                               IOException, ClassNotFoundException,
                                                               NoSuchPaddingException,
                                                               InvalidKeyException,
                                                               IllegalBlockSizeException,
                                                               BadPaddingException {
        SecretKey secretKey = getSecretKeyFromLocal();
        if (secretKey == null) {
            secretKey = generateSecretKey(algorithm);
        }
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);

        return cipher.doFinal(data);
    }

    /**
     * 解密内容,并返回
     * @param data
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] decrypt(byte[] data) throws NoSuchAlgorithmException, IOException,
                                             ClassNotFoundException, NoSuchPaddingException,
                                             InvalidKeyException, IllegalBlockSizeException,
                                             BadPaddingException {
        return decrypt(data, ENCRYPT_ALGORITHM);
    }

    /**
     * 获取密钥对象
     * @return
     * @throws NoSuchAlgorithmException
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static SecretKey getSecretKeyFromLocal() throws NoSuchAlgorithmException, IOException,
                                                   ClassNotFoundException {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        SecretKey secretKey = null;
        try {
            //加载私钥文件
            fis = new FileInputStream(new File(SECRET_KEY_FILE_NAME));
            ois = new ObjectInputStream(fis);
            //读取对象
            secretKey = (SecretKey) ois.readObject();
        } finally {
            //关闭流
            if (fis != null) {
                fis.close();
            }
            if (ois != null) {
                ois.close();
            }
        }
        return secretKey;
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值