EncryptUtils 加密工具类

一、加密算法

类型 用途 特点 示例 推荐使用 安全性评价 适用场景
对称加密 加密大量数据 加密解密速度快,密钥管理难 DES 老旧系统兼容性
3DES 有限制的老旧系统
AES (Rijndael) 大量数据加密,现代应用
SM4 (SMS4) 国密标准,国内应用
Blowfish 小规模应用
非对称加密 安全通信,数字签名 安全性高,但速度慢 RSA 数字签名,密钥交换
ECC (Elliptic Curve Cryptography) 密钥交换,移动设备
散列函数 数据完整性校验,密码存储 单向,不可逆 MD5 消息摘要(非安全性要求)
SHA-1 消息摘要(非安全性要求)
SHA-256 安全消息摘要,密码哈希
SM3 国密标准,国内应用
编码 编码二进制数据为文本形式 不是加密,可逆转换 Base64 N/A 传输二进制数据,邮件附件等

备注:

  • DES:由于密钥长度较短(56位),已经容易受到攻击,因此不再推荐使用。
  • 3DES:通过使用三倍的密钥长度来增强安全性,但在性能上不如AES。
  • AES:是目前最广泛使用的对称加密算法之一,支持128、192和256位密钥长度。
  • SM4:是中国国家密码管理局制定的一种分组密码算法,适用于国内应用。
  • RSA:是一种非常常见的非对称加密算法,广泛用于数字签名和密钥交换。
  • ECC:与RSA相比,在相同安全水平下可以使用更短的密钥,更适合移动设备。
  • MD5 和 SHA-1:由于已知的碰撞攻击,不再推荐用于安全性要求高的场合。
  • SHA-256:是目前较为安全的散列函数之一,适合用于密码存储和其他需要高强度散列的应用。
  • Base64:是一种编码方式,而不是加密算法,主要用于将二进制数据转换为可以在文本环境中安全传输的形式。

二、工具类展示

public class EncryptUtils {
    /**
     * 公钥
     */
    public static final String PUBLIC_KEY = "publicKey";
    /**
     * 私钥
     */
    public static final String PRIVATE_KEY = "privateKey";

    /**
     * Base64加密
     *
     * @param data 待加密数据
     * @return 加密后字符串
     */
    public static String encryptByBase64(String data) {
        return Base64.encode(data, StandardCharsets.UTF_8);
    }

    /**
     * Base64解密
     *
     * @param data 待解密数据
     * @return 解密后字符串
     */
    public static String decryptByBase64(String data) {
        return Base64.decodeStr(data, StandardCharsets.UTF_8);
    }

    /**
     * AES加密
     *
     * @param data     待解密数据
     * @param password 秘钥字符串
     * @return 加密后字符串, 采用Base64编码
     */
    public static String encryptByAes(String data, String password) {
        if (StrUtil.isBlank(password)) {
            throw new IllegalArgumentException("AES需要传入秘钥信息");
        }
        // aes算法的秘钥要求是16位、24位、32位
        int[
### Java 加密解密工具类示例 #### EncryptUtils 类实现 `EncryptUtils` 是一个自定义的 Java 工具类,用于封装常见的加密和解密算法,如 MD5、SHA-1、SHA-256、AES 和 DES 等。该工具类提供了简单易用的接口,使开发者能够方便地进行数据的加密、解密、签名和验证操作。 ```java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.security.MessageDigest; import java.util.Base64; public class EncryptUtils { private static final String ALGORITHM_AES = "AES"; private static final int KEY_SIZE = 128; // Key size can be 128, 192 or 256 bits depending on the algorithm and environment. /** * Generate a new secret key for AES encryption. */ public static SecretKey generateKey() throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM_AES); keyGen.init(KEY_SIZE); return keyGen.generateKey(); } /** * Convert string to Base64 encoded string using given key. */ public static String encrypt(String data, SecretKey secret) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM_AES); cipher.init(Cipher.ENCRYPT_MODE, secret); byte[] encryptedData = cipher.doFinal(data.getBytes()); return Base64.getEncoder().encodeToString(encryptedData); } /** * Decrypt Base64 encoded string back into original string using given key. */ public static String decrypt(String encryptedData, SecretKey secret) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM_AES); cipher.init(Cipher.DECRYPT_MODE, secret); byte[] decodedValue = Base64.getDecoder().decode(encryptedData); byte[] decryptedData = cipher.doFinal(decodedValue); return new String(decryptedData); } } ``` 此代码片段展示了如何创建 `EncryptUtils` 类并实现了基本的 AES 加密/解密功能[^2]。 #### 使用示例 下面是如何使用上述工具类来进行字符串的加密与解密: ```java try { // Step 1: Create an instance of SecretKey (only once per application lifecycle). SecretKey mySecretKey = EncryptUtils.generateKey(); // Store this somewhere safe as you will need it later for decryption! // Step 2: Use your generated key to perform encryption operations. String messageToBeEncrypted = "This is a test."; System.out.println("Original Message : " + messageToBeEncrypted); String encryptedMessage = EncryptUtils.encrypt(messageToBeEncrypted, mySecretKey); System.out.println("Encrypted Message: " + encryptedMessage); // Step 3: When needed, use the same key to reverse the process by performing decryption operation. String decryptedMessage = EncryptUtils.decrypt(encryptedMessage, mySecretKey); System.out.println("Decrypted Message: " + decryptedMessage); } catch (Exception e) { e.printStackTrace(); } ``` 这段程序首先生成了一个新的秘密钥,接着利用它对消息进行了加密处理,并最终成功恢复了原始的消息内容。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

syfjava

请博主喝杯蜜雪冰城

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值