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

Java中的加密解密函数及实现方法

发布时间:2023-06-12 06:39:14

Java中有很多加密解密函数和实现方法,常用的有对称加密和非对称加密两种方式。

一、对称加密

对称加密是加密和解密使用相同的密钥的加密方式。Java中常用的对称加密算法有DES、3DES、AES等。

1. DES加密解密

DES是一种分组密码,每个分组64位(8字节),密钥长度56位(共8字节),即DES密钥为8字节(需要将密钥补齐到64位),加密后的数据也是64位一个分组。DES加密解密的代码如下:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESUtil {
    /**
     * DES加密
     *
     * @param data      待加密数据
     * @param secretKey 密钥
     * @return 加密后的数据
     * @throws Exception 加密异常
     */
    public static byte[] encrypt(byte[] data, byte[] secretKey) throws Exception {
        DESKeySpec desKeySpec = new DESKeySpec(secretKey);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = secretKeyFactory.generateSecret(desKeySpec);

        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(data);
    }

    /**
     * DES解密
     *
     * @param data      待解密数据
     * @param secretKey 密钥
     * @return 解密后的数据
     * @throws Exception 解密异常
     */
    public static byte[] decrypt(byte[] data, byte[] secretKey) throws Exception {
        DESKeySpec desKeySpec = new DESKeySpec(secretKey);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = secretKeyFactory.generateSecret(desKeySpec);

        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(data);
    }
}

2. 3DES加密解密

3DES是对DES算法的改进,使用3个不同的长度为8位的密钥,对数据进行3次加密,因此密钥长度为24位(共3个8字节),3DES加密解密的代码如下:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;

public class DESedeUtil {
    /**
     * 3DES加密
     *
     * @param data      待加密数据
     * @param secretKey 密钥
     * @return 加密后的数据
     * @throws Exception 加密异常
     */
    public static byte[] encrypt(byte[] data, byte[] secretKey) throws Exception {
        DESedeKeySpec desedeKeySpec = new DESedeKeySpec(secretKey);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = secretKeyFactory.generateSecret(desedeKeySpec);

        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(data);
    }

    /**
     * 3DES解密
     *
     * @param data      待解密数据
     * @param secretKey 密钥
     * @return 解密后的数据
     * @throws Exception 解密异常
     */
    public static byte[] decrypt(byte[] data, byte[] secretKey) throws Exception {
        DESedeKeySpec desedeKeySpec = new DESedeKeySpec(secretKey);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DESede");
        SecretKey key = secretKeyFactory.generateSecret(desedeKeySpec);

        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(data);
    }
}

3. AES加密解密

AES是一种高级加密标准,在对称加密中已经成为主流算法。密钥长度分别为128位、192位和256位,AES加密解密的代码如下:

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

public class AESUtil {
    /**
     * AES加密
     *
     * @param data      待加密数据
     * @param secretKey 密钥
     * @return 加密后的数据
     * @throws Exception 加密异常
     */
    public static byte[] encrypt(byte[] data, byte[] secretKey) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }

    /**
     * AES解密
     *
     * @param data      待解密数据
     * @param secretKey 密钥
     * @return 解密后的数据
     * @throws Exception 解密异常
     */
    public static byte[] decrypt(byte[] data, byte[] secretKey) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        return cipher.doFinal(data);
    }
}

二、非对称加密

非对称加密算法使用公钥加密、私钥解密,或私钥加密、公钥解密,常用的非对称加密算法有RSA和DSA。

1. RSA加密解密

RSA是一种常用的非对称加密算法,其安全性基于大数分解的困难性,密钥长度一般为1024位、2048位或4096位。RSA加密解密的代码如下:

`java

import javax.crypto.Cipher;

import java.security.Key;

import java.security.KeyFactory;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

public class RSAUtil {

/**

* 生成RSA公钥私钥

*

* @return 公钥私钥

* @throws Exception 异常

*/

public static KeyPair generateRSAKeyPair() throws Exception {

KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");

keyPairGenerator.initialize(1024);

return keyPairGenerator.genKeyPair();

}

/**

* RSA加密

*

* @param data 待加密数据

* @param publicKey 公钥

* @return 加密后的数据

* @throws Exception 异常

*/

public static byte[] encrypt(byte[] data, Key publicKey) throws Exception {

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

cipher.init(Cipher.ENCRYPT_MODE, publicKey);

return cipher.doFinal(data);

}

/**

* RSA解密

*

* @param data 待解密数据

* @param privateKey 私钥

* @return 解密后的数据

* @throws Exception 异常

*/

public static byte[] decrypt(byte[] data, Key privateKey) throws Exception {

Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

cipher.init(Cipher.DECRYPT_MODE, privateKey);

return cipher.doFinal(data);

}

/**

* 获取公钥对象

*

* @param publicKey 公钥字节数组

* @return 公钥对象

* @throws Exception 异常

*/

public static RSAPublicKey getRSAPublicKey(byte[] publicKey) throws Exception {

X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKey);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

return (RSAPublicKey) keyFactory.generatePublic(publicKeySpec);

}

/**

* 获取私钥对象

*

* @param privateKey 私钥字节数组

* @return 私钥对象

* @throws Exception 异常

*/

public static RSAPrivateKey getRSAPrivateKey(byte[] privateKey) throws Exception {

PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKey);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

return (RSAPrivateKey) keyFactory.generatePrivate(privateKeySpec);