欢迎访问宙启技术站
智能推送

Java加密与解密函数实现:MD5、AES、RSA等

发布时间:2023-06-18 09:00:04

在现代互联网时代,数据的传输变得越来越容易被窃取,为了保证数据的安全性,我们需要进行数据加密。Java作为一种广泛应用的编程语言,提供了很多加密和解密的函数,其中包括MD5、AES、RSA等算法。

MD5算法是一种常用的哈希函数,用于对一段数据进行不可逆的加密。在Java中,可以使用java.security.MessageDigest类进行MD5的加密和解密:

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class MD5Util {

    public static String encrypt(String str) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] bytes = md.digest(str.getBytes());
        return Base64.getEncoder().encodeToString(bytes);
    }

    public static boolean match(String str, String encrypted) throws NoSuchAlgorithmException {
        String result = encrypt(str);
        return result.equals(encrypted);
    }
}

在以上代码中,我们使用了java.security.MessageDigest类生成MD5的哈希值,并使用java.util.Base64类将结果编码为可读字符串。

AES算法是一种对称加密算法,用于对数据进行加密和解密。在Java中,可以使用javax.crypto包的Cipher类进行AES加密和解密:

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class AESUtil {

    private static final String KEY = "1234567812345678";
    private static final String IV = "1234567812345678";

    public static String encrypt(String str) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec);
        byte[] encrypted = cipher.doFinal(str.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String encrypted) throws Exception {
        SecretKeySpec keySpec = new SecretKeySpec(KEY.getBytes(StandardCharsets.UTF_8), "AES");
        IvParameterSpec ivSpec = new IvParameterSpec(IV.getBytes(StandardCharsets.UTF_8));
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
        byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(encrypted));
        return new String(decrypted);
    }
}

在以上代码中,我们使用javax.crypto包的Cipher类进行AES加密和解密,并使用Base64将结果编码为可读字符串。

RSA算法是一种非对称加密算法,用于对数据进行加密和解密。在Java中,可以使用java.security包的KeyPairGenerator类生成RSA密钥对,使用java.security包的Cipher类进行RSA加密和解密:

import javax.crypto.Cipher;
import java.security.*;

public class RSAUtil {

    public static KeyPair generateKey() throws NoSuchAlgorithmException {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048, new SecureRandom());
        return keyGen.generateKeyPair();
    }

    public static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data);
    }

    public static byte[] decrypt(byte[] data, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(data);
    }
}

在以上代码中,我们使用java.security包的KeyPairGenerator类生成RSA密钥对,并使用java.security包的Cipher类进行RSA加密和解密。

总之,Java提供了很多加密和解密的函数,开发者可以根据需要选择适合的算法来保证数据的安全性。