Java加密解密工具

引言

随着信息技术的发展,数据的安全性和隐私保护变得越来越重要。在数据传输和存储过程中,对敏感信息的加密和解密是非常关键的一环。Java作为一种强大的编程语言,提供了丰富的加密解密工具类和算法库,使我们可以轻松地实现数据的加密和解密操作。

本文将介绍一些常见的Java加密解密工具类和算法,并提供相应的代码示例。

对称加密算法

在对称加密算法中,使用相同的密钥进行加密和解密操作。常见的对称加密算法有DES、AES和RC4等。

DES加密解密

DES(Data Encryption Standard,数据加密标准)是一种对称加密算法。Java提供了javax.crypto包来支持DES加密解密操作。

以下是使用DES算法进行加密解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class DESUtils {
    private static final String ALGORITHM = "DES";

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(byte[] encryptedData, byte[] key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(encryptedData);
    }
}

AES加密解密

AES(Advanced Encryption Standard,高级加密标准)是一种常用的对称加密算法。Java中,通过javax.crypto包提供了AES的支持。

以下是使用AES算法进行加密解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class AESUtils {
    private static final String ALGORITHM = "AES";

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(byte[] encryptedData, byte[] key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(encryptedData);
    }
}

RC4加密解密

RC4是一种流密码算法,也是一种对称加密算法。Java中,可以通过javax.crypto包使用RC4算法进行加密解密操作。

以下是使用RC4算法进行加密解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class RC4Utils {
    private static final String ALGORITHM = "RC4";

    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(byte[] encryptedData, byte[] key) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(key, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(encryptedData);
    }
}

非对称加密算法

在非对称加密算法中,使用一对密钥进行加密和解密操作,包括公钥和私钥。常见的非对称加密算法有RSA和DSA等。

RSA加密解密

RSA(Rivest-Shamir-Adleman)是一种常用的非对称加密算法,它基于大数分解难题。Java中,通过java.security包提供了RSA算法的支持。

以下是使用RSA算法进行加密解密的示例代码:

import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.PrivateKey;
import java.security.PublicKey;
import javax.crypto.Cipher;

public class RSAUtils {
    private static final String ALGORITHM = "RSA";

    public static Key