加密工具类
昨天的笔记中,包含了CryptoUtils和RSAUtils,今天顺便贴一下两个对称加密的工具类:
DESUtils
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.spec.InvalidKeySpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
public class DESUtils extends CryptoUtils {
private final static String KEY_ALGORITHM = "DES";
public static String encrypt(String plainText, String key) {
try {
SecretKey securekey = getSecretKey(key);
Cipher cipher = getCipher(KEY_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
byte[] bt = cipher.doFinal(plainText.getBytes());
return encodeToString(bt);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
}
}
public static String decrypt(String encryptText, String key) {
try {
SecretKey securekey = getSecretKey(key);
Cipher cipher = getCipher(KEY_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, securekey, random);
byte[] datas = cipher.doFinal(decodeFromString(encryptText));
return new String(datas);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
}
}
private static SecretKey getSecretKey(String key) throws InvalidKeyException {
try {
byte[] datas = key.getBytes();
DESKeySpec dks = new DESKeySpec(datas);
Provider provider = getProvider();
SecretKeyFactory keyFactory = null;
if (null == provider) {
keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM);
} else {
keyFactory = SecretKeyFactory.getInstance(KEY_ALGORITHM, provider);
}
SecretKey securekey = keyFactory.generateSecret(dks);
return securekey;
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeySpecException e) {
throw new RuntimeException(e);
}
}
}
AESUtils
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.Provider;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class AESUtils extends CryptoUtils {
private static final String KEY_ALGORITHM = "AES";
public static String encrypt(String plantText, String key) {
try {
Cipher cipher = getCipher(KEY_ALGORITHM);
SecretKey secretKey = getSecretKey(key);
byte[] data = plantText.getBytes();
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] result = cipher.doFinal(data);
return encodeToString(result);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
}
}
public static String decrypt(String encryptText, String key) {
try {
Cipher cipher = getCipher(KEY_ALGORITHM);
SecretKey secretKey = getSecretKey(key);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] data = decodeFromString(encryptText);
byte[] result = cipher.doFinal(data);
return new String(result);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
} catch (IllegalBlockSizeException e) {
throw new RuntimeException(e);
} catch (BadPaddingException e) {
throw new RuntimeException(e);
}
}
private static SecretKey getSecretKey(final String key) {
try {
Provider provider = getProvider();
KeyGenerator kg = null;
if (null == provider) {
kg = KeyGenerator.getInstance(KEY_ALGORITHM);
} else {
kg = KeyGenerator.getInstance(KEY_ALGORITHM, provider);
}
kg.init(128, new SecureRandom(key.getBytes()));
SecretKey secretKey = kg.generateKey();
return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);// 转换为AES专用密钥
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
}
拣尽寒枝不肯栖,寂寞沙洲冷。
郴江幸自绕郴山,为谁流下潇湘去?
欲将心事付瑶琴,知音少,弦断有谁听?
倩何人,唤取红巾翠袖,揾英雄泪!
零落成泥碾作尘,只有香如故!
郴江幸自绕郴山,为谁流下潇湘去?
欲将心事付瑶琴,知音少,弦断有谁听?
倩何人,唤取红巾翠袖,揾英雄泪!
零落成泥碾作尘,只有香如故!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!