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

使用Java函数进行加密解密操作。

发布时间:2023-05-20 01:29:46

加密和解密在信息安全领域是非常常见的操作,我们需要将敏感信息加密后存储在数据库或传输给其他系统,再将其解密以便使用,以此防止信息泄露。Java语言提供了丰富的加密解密函数,本篇文章将介绍Java语言的加密和解密函数,包括常用的对称加密算法和非对称加密算法。

1.对称加密算法

对称加密算法是指加密和解密使用同一种密钥的算法,在加密和解密的过程中,需要保证使用的密钥是安全的。对称加密算法的常见算法有DES、AES和DESede等。

(1)DES算法

Data Encryption Standard(数据加密标准)算法是一种对称加密算法,它使用56位的密钥对数据进行加密和解密。由于DES算法的密钥长度较短,易被破解,因此现在已经很少使用了。

下面是DES加密和解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.security.SecureRandom;
import org.apache.commons.codec.binary.Base64;

public class TestDES {
    private static final String ALGORITHM = "DES";
    private static final String CHARSET = "UTF-8";
    private static final String SECRET_KEY = "mysecretkey";

    // 对字符串进行加密操作
    public static String encrypt(String text) throws Exception {
        // 生成密钥
        SecretKey secretKey = generateKey(SECRET_KEY);
        // 加密器
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        // 加密
        byte[] encryptedBytes = cipher.doFinal(text.getBytes(CHARSET));
        // 转换为Base64编码字符串
        return new String(Base64.encodeBase64(encryptedBytes), CHARSET);
    }

    // 对字符串进行解密操作
    public static String decrypt(String text) throws Exception {
        // 生成密钥
        SecretKey secretKey = generateKey(SECRET_KEY);
        // 解密器
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        // 解码Base64字符串
        byte[] decodedBytes = Base64.decodeBase64(text.getBytes(CHARSET));
        // 解密
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes, CHARSET);
    }

    // 生成密钥
    private static SecretKey generateKey(String key) throws Exception {
        SecureRandom secureRandom = new SecureRandom(key.getBytes(CHARSET));
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(CHARSET));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return secretKeyFactory.generateSecret(desKeySpec);
    }

    public static void main(String[] args) throws Exception {
        String text = "Hello, world!";
        String encryptedText = encrypt(text);
        System.out.println("Encrypted text: " + encryptedText);
        String decryptedText = decrypt(encryptedText);
        System.out.println("Decrypted text: " + decryptedText);
    }
}

(2)AES算法

Advanced Encryption Standard(高级加密标准)算法是一种对称加密算法,该算法支持128位、192位和256位的密钥长度,比DES算法更加安全。AES算法的加密和解密过程与DES算法类似。

下面是AES加密和解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import org.apache.commons.codec.binary.Base64;

public class TestAES {
    private static final int KEY_SIZE = 128;
    private static final String ALGORITHM = "AES";
    private static final String CHARSET = "UTF-8";
    private static final String SECRET_KEY = "mysecretkey";

    // 对字符串进行加密操作
    public static String encrypt(String text) throws Exception {
        // 生成密钥
        SecretKey secretKey = generateKey(SECRET_KEY);
        // 加密器
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        // 加密
        byte[] encryptedBytes = cipher.doFinal(text.getBytes(CHARSET));
        // 转换为Base64编码字符串
        return new String(Base64.encodeBase64(encryptedBytes), CHARSET);
    }

    // 对字符串进行解密操作
    public static String decrypt(String text) throws Exception {
        // 生成密钥
        SecretKey secretKey = generateKey(SECRET_KEY);
        // 解密器
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        // 解码Base64字符串
        byte[] decodedBytes = Base64.decodeBase64(text.getBytes(CHARSET));
        // 解密
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes, CHARSET);
    }

    // 生成密钥
    private static SecretKey generateKey(String key) throws Exception {
        SecureRandom secureRandom = new SecureRandom(key.getBytes(CHARSET));
        SecretKeySpec secretKeySpec = new SecretKeySpec(secureRandom.generateSeed(KEY_SIZE / 8), ALGORITHM);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return secretKeyFactory.generateSecret(secretKeySpec);
    }

    public static void main(String[] args) throws Exception {
        String text = "Hello, world!";
        String encryptedText = encrypt(text);
        System.out.println("Encrypted text: " + encryptedText);
        String decryptedText = decrypt(encryptedText);
        System.out.println("Decrypted text: " + decryptedText);
    }
}

(3)DESede算法

Triple DES(三重DES)算法是一种对称加密算法,它对每个数据块应用DES算法三次,使用两个或者三个不同的密钥。Triple DES算法相比于DES算法更加安全,但是相应的加密解密速度较慢。

下面是Triple DES加密和解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.security.SecureRandom;
import org.apache.commons.codec.binary.Base64;

public class TestTripleDES {
    private static final String ALGORITHM = "DESede";
    private static final String CHARSET = "UTF-8";
    private static final String SECRET_KEY = "mysecretkeymysecretkeymysecretkey";

    // 对字符串进行加密操作
    public static String encrypt(String text) throws Exception {
        // 生成密钥
        SecretKey secretKey = generateKey(SECRET_KEY);
        // 加密器
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        // 加密
        byte[] encryptedBytes = cipher.doFinal(text.getBytes(CHARSET));
        // 转换为Base64编码字符串
        return new String(Base64.encodeBase64(encryptedBytes), CHARSET);
    }

    // 对字符串进行解密操作
    public static String decrypt(String text) throws Exception {
        // 生成密钥
        SecretKey secretKey = generateKey(SECRET_KEY);
        // 解密器
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        // 解码Base64字符串
        byte[] decodedBytes = Base64.decodeBase64(text.getBytes(CHARSET));
        // 解密
        byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        return new String(decryptedBytes, CHARSET);
    }

    // 生成密钥
    private static SecretKey generateKey(String key) throws Exception {
        DESedeKeySpec desedeKeySpec = new DESedeKeySpec(key.getBytes(CHARSET));
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return secretKeyFactory.generateSecret(desedeKeySpec);
    }

    public static void main(String[] args) throws Exception {
        String text = "Hello, world!";
        String encryptedText = encrypt(text);
        System.out.println("Encrypted text: " + encryptedText);
        String decryptedText = decrypt(encryptedText);
        System.out.println("Decrypted text: " + decryptedText);
    }
}

2.非对称加密算法

非对称加密算法是指加密和解密使用不同的密钥的算法,其中一个密钥被称为公钥,另一个密钥被称为私钥。公钥