JAVA常用加解密工具类
程序员文章站
2024-03-14 14:25:34
...
Des
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.Key;
public class DesUtil {
private static final String SECRET_KEY_TYPE = "DES";
private static final String ECB_MOB = "DES/ECB/PKCS5Padding";
private static final String CHAESET_NAME = "UTF-8";
private static Key getKey(String password) throws Exception{
byte[] DESkey = password.getBytes(CHAESET_NAME);
DESKeySpec keySpec = new DESKeySpec(DESkey);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_KEY_TYPE);
return keyFactory.generateSecret(keySpec);
}
public static String encode(String data, String password) throws Exception {
Cipher enCipher = Cipher.getInstance(ECB_MOB);
Key key = getKey(password);
enCipher.init(Cipher.ENCRYPT_MODE, key);
byte[] pasByte = enCipher.doFinal(data.getBytes(CHAESET_NAME));
return Base64.encodeBase64String(pasByte);
}
public static String decode(String data, String password) throws Exception {
Cipher deCipher = Cipher.getInstance(ECB_MOB);
Key key = getKey(password);
deCipher.init(Cipher.DECRYPT_MODE, key);
byte[] pasByte = deCipher.doFinal(Base64.decodeBase64(data.getBytes(CHAESET_NAME)));
return new String(pasByte, CHAESET_NAME);
}
}
AES
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import cn.hutool.core.codec.Base64;
public class AESUtil {
/**
* **算法
*/
private static final String ALGORITHM = "AES";
/**
* 加解密算法/工作模式/填充方式
*/
private static final String ALGORITHM_MODE_PADDING = "AES/ECB/PKCS7Padding";
/**
* AES加密
* @param data
* 加密内容
* @param password
* 加密密码
* @return
* @throws Exception
*/
public static String encryptData(String data, String password) throws Exception {
Security.addProvider(new BouncyCastleProvider());
// 创建密码器
Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
// 初始化为加密模式的密码
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(password));
// 加密
byte[] result = cipher.doFinal(data.getBytes());
return Base64.encode(result);
}
/**
* AES解密
* @param base64Data
* 解密内容
* @param password
* 解密密码
* @return
* @throws Exception
*/
public static String decryptData(String base64Data, String password) throws Exception {
Security.addProvider(new BouncyCastleProvider());
// 创建密码器
Cipher cipher = Cipher.getInstance(ALGORITHM_MODE_PADDING);
// 使用**初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(password));
// 执行操作
byte[] result = cipher.doFinal(Base64.decode(base64Data));
return new String(result, "utf-8");
}
/**
* 生成加密秘钥
* @return
*/
private static SecretKeySpec getSecretKey(String password) {
SecretKeySpec key = new SecretKeySpec(MD5.sign(password, "UTF-8").toLowerCase().getBytes(), ALGORITHM);
return key;
}
}
RSA
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RsaUtils {
private static final Logger logger = LoggerFactory.getLogger(RsaUtils.class);
/**
* RSA2签名
* @param content 待签名的字符串
* @param privateKey rsa私钥字符串
* @param charset 字符集编码
* @return 签名结果
*/
public static String rsaSign(String content, String privateKey, String charset) {
try {
PrivateKey priKey = getPrivateKeyFromPKCS8("RSA", new ByteArrayInputStream(privateKey.getBytes()));
Signature signature = Signature.getInstance("SHA256WithRSA");
signature.initSign(priKey);
if (StringUtils.isBlank(charset)) {
signature.update(content.getBytes());
} else {
signature.update(content.getBytes(charset));
}
byte[] signed = signature.sign();
return new String(Base64.encodeBase64(signed));
} catch (Exception e) {
logger.error("RSA签名异常:{}", e.getMessage(), e);
return null;
}
}
/**
* RSA2验签
*
* @param content 被签名的内容
* @param sign 签名后的结果
* @param publicKey rsa公钥
* @param charset 字符集编码
* @return 验签结果
*/
public static boolean doCheck(String content, String sign, String publicKey, String charset) {
try {
PublicKey pubKey = getPublicKeyFromX509("RSA", new ByteArrayInputStream(publicKey.getBytes()));
Signature signature = Signature.getInstance("SHA256WithRSA");
signature.initVerify(pubKey);
signature.update(getContentBytes(content, charset));
return signature.verify(Base64.decodeBase64(sign.getBytes()));
} catch (Exception e) {
logger.error("RSA验签异常:{}", e.getMessage(), e);
return false;
}
}
/**
*
* 获取私钥对象
* @param algorithm 签名方式
* @param ins 私钥流
* @return
* @throws Exception
*/
private static PrivateKey getPrivateKeyFromPKCS8(String algorithm, InputStream ins) throws Exception {
if (ins == null || StringUtils.isEmpty(algorithm)) {
return null;
}
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
byte[] encodedKey = readText(ins, "utf-8", true).getBytes();
encodedKey = Base64.decodeBase64(encodedKey);
return keyFactory.generatePrivate(new PKCS8EncodedKeySpec(encodedKey));
}
/**
*
* 获取公钥对象
* @param algorithm 签名方式
* @param ins 公钥流
* @return
* @throws NoSuchAlgorithmException
*/
private static PublicKey getPublicKeyFromX509(String algorithm, InputStream ins) {
try {
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
StringWriter writer = new StringWriter();
io(new InputStreamReader(ins), writer, true, true);
byte[] encodedKey = writer.toString().getBytes();
// 先base64解码
encodedKey = Base64.decodeBase64(encodedKey);
return keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
} catch (InvalidKeySpecException e) {
logger.error("公钥签名InvalidKeySpecException异常:{}", e.getMessage(), e);
return null;
} catch (NoSuchAlgorithmException e) {
logger.error("公钥签名NoSuchAlgorithmException异常:{}", e.getMessage(), e);
return null;
}
}
/**
*
* 获取字符串对应编码的字节
* @param content 字符串内容
* @param charset 字符集编码
* @return
* @throws UnsupportedEncodingException
*/
private static byte[] getContentBytes(String content, String charset) throws UnsupportedEncodingException {
if (StringUtils.isEmpty(charset)) {
return content.getBytes();
}
return content.getBytes(charset);
}
/**
*
* 将指定输入流的所有文本全部读出到一个字符串中
* @param in 输入流
* @param charset 字符集编码
* @param closeIn 是否关闭流
* @return
* @throws IOException
*/
private static String readText(InputStream in, String charset, boolean closeIn) throws IOException {
Reader reader = charset == null ? new InputStreamReader(in) : new InputStreamReader(in, charset);
return readText(reader, closeIn);
}
/**
*
* 将指定<code>Reader</code>的所有文本全部读出到一个字符串中
* @param in 输入流
* @param closeIn 是否关闭流
* @return
* @throws IOException
*/
private static String readText(Reader in, boolean closeIn) throws IOException {
StringWriter out = new StringWriter();
io(in, out, closeIn, true);
return out.toString();
}
/**
*
* 从输入流读取内容,写入到输出流中
* @param in 输入流
* @param out 输出流
* @param closeIn 是否关闭流
* @param closeOut 是否关闭流
* @throws IOException
*/
private static void io(Reader in, Writer out, boolean closeIn, boolean closeOut) {
int bufferSize = 8192 >> 1;
char[] buffer = new char[bufferSize];
int amount;
try {
while ((amount = in.read(buffer)) >= 0) {
out.write(buffer, 0, amount);
}
out.flush();
} catch (Exception e) {
logger.error("从输入流读取内容,写入到输出流中异常:{}", e.getMessage(), e);
} finally {
if (closeIn) {
try {
in.close();
} catch (IOException e) {
logger.error("从输入流读取内容,写入到输出流中异常:{}", e.getMessage(), e);
}
}
if (closeOut) {
try {
out.close();
} catch (IOException e) {
logger.error("从输入流读取内容,写入到输出流中异常:{}", e.getMessage(), e);
}
}
}
}
}
上一篇: SHA 消息摘要算法应用详解
下一篇: java 加解密工具类