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;
}
}
分享到:
相关推荐
RSA加密解密实例,环境eclipse + maven
一个加密解密源代码实例
.NET , RSA非对称加解密源码。让初学者理解非对称加密逻辑。
Base64加密解密实例
JAVA数据加密解密的实例+Java源码
包括java自定义加密解密实例,异或加密解密实例,AES加密解密,DES加密解密 对文件加密建议使用DES方式 技术学习交流:635278789
C# MD5加密解密实例,一个小程序,哈哈
AES加解密实例, 能够用于AES算法加密及解密
JS源码 - Base64加密解密实例演示程序类
delphi异或加密解密实例在delphi7.0下编译通过,加密解密过程简单,采用异或算法,是初学者的首选
php rsa加密解密实例,附带教程以及openssl windows安装包
VC++实现加、解密的dll实例,实现共享加密、解码函数,方便使用统一的加解密方案。
asp.net加密解密实例,内含一个加密解密类,只需要调用就可以了,非常清晰,供大家学习
Hash MD5 DES AES RSA加解密实例
将文本加密和解密的实例程序,
BouncyCastleOpenGPG加解密实例.zip
PHP中加密解密函数与DES加密解密实例_.docx
AES加解密前后端实例
RSA加密解密的程序,有兴趣的朋友可以看看
PHP中加密解密函数与DES加密解密实例__1.docx