Java Crypt加密解密

Crypt加密解密是一种常用的数据加密方式,可以有效保护数据的安全性。在Java中,提供了多种加密算法和工具类,使得加密解密操作变得简单而快捷。

加密解密算法

在Java中,常用的加密解密算法包括:

  1. 对称加密算法:加密和解密使用相同的密钥,例如DES、AES。
  2. 非对称加密算法:加密和解密使用不同的密钥,例如RSA。
  3. 散列函数:一种单向加密算法,不可逆,例如MD5、SHA256。

对称加密算法

对称加密算法是一种使用相同密钥进行加密和解密的算法。在Java中,常用的对称加密算法有DES和AES。

DES加密解密示例

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

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class DESUtil {

    public static String encrypt(String data, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedData, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES");
        DESKeySpec desKeySpec = new DESKeySpec(key.getBytes(StandardCharsets.UTF_8));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) throws Exception {
        String data = "Hello, World!";
        String key = "12345678";
        String encryptedData = encrypt(data, key);
        System.out.println("Encrypted Data: " + encryptedData);
        String decryptedData = decrypt(encryptedData, key);
        System.out.println("Decrypted Data: " + decryptedData);
    }
}

上述代码中,我们使用DES算法对字符串进行加密和解密。首先,我们需要指定加密算法为DES,并生成一个密钥。然后,我们使用密钥初始化加密和解密的过程。最后,使用加密后的数据进行解密操作,并将解密后的数据转化为字符串。

非对称加密算法

非对称加密算法使用不同的密钥进行加密和解密。在Java中,常用的非对称加密算法有RSA。

RSA加密解密示例

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

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;

public class RSAUtil {

    public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        return keyPairGenerator.generateKeyPair();
    }

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

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

    public static void main(String[] args) throws Exception {
        KeyPair keyPair = generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        String data = "Hello, World!";
        byte[] encryptedData = encrypt(data.getBytes(StandardCharsets.UTF_8), publicKey);
        System.out.println("Encrypted Data: " + new String(encryptedData, StandardCharsets.UTF_8));

        byte[] decryptedData = decrypt(encryptedData, privateKey);
        System.out.println("Decrypted Data: " + new String(decryptedData, StandardCharsets.UTF_8));
    }
}

上述代码中,我们使用RSA算法对字符串进行加密和解