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

如何在Java中使用函数实现加密解密操作

发布时间:2023-07-02 14:41:52

在Java中,可以使用函数实现加密和解密操作。下面将针对常见的加密解密算法进行介绍。

1. 对称加密算法

对称加密算法使用相同的密钥进行加密和解密操作。常见的对称加密算法有DES、AES等。以下是使用AES算法进行加密和解密的函数示例:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;

public class SymmetricEncryption {

    private static final String ALGORITHM = "AES";
    private static final String KEY = "mySecretKey12345";

    public static String encrypt(String data) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static String decrypt(String encryptedData) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = Base64.getDecoder().decode(encryptedData);
        byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
        return new String(decryptedBytes);
    }

    public static void main(String[] args) throws Exception {
        String originalData = "Hello World!";
        String encryptedData = encrypt(originalData);
        String decryptedData = decrypt(encryptedData);

        System.out.println("Original Data: " + originalData);
        System.out.println("Encrypted Data: " + encryptedData);
        System.out.println("Decrypted Data: " + decryptedData);
    }
}

2. 非对称加密算法

非对称加密算法使用一对密钥进行加密和解密操作,包括公钥和私钥。常见的非对称加密算法有RSA、DSA等。以下是使用RSA算法进行加密和解密的函数示例:

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

public class AsymmetricEncryption {

    private static final String ALGORITHM = "RSA";

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

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

    public static void main(String[] args) throws Exception {
        String originalData = "Hello World!";

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        byte[] encryptedData = encrypt(originalData.getBytes(), publicKey);
        byte[] decryptedData = decrypt(encryptedData, privateKey);

        System.out.println("Original Data: " + originalData);
        System.out.println("Encrypted Data: " + new String(encryptedData));
        System.out.println("Decrypted Data: " + new String(decryptedData));
    }
}

3. 哈希算法

哈希算法可以将任意长度的数据映射为固定长度的摘要,常见的哈希算法有MD5、SHA1等。以下是使用MD5算法进行加密的函数示例:

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

public class HashFunction {

    private static final String ALGORITHM = "MD5";

    public static String encrypt(String data) throws Exception {
        MessageDigest messageDigest = MessageDigest.getInstance(ALGORITHM);
        byte[] encryptedBytes = messageDigest.digest(data.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    public static void main(String[] args) throws Exception {
        String originalData = "Hello World!";
        String encryptedData = encrypt(originalData);

        System.out.println("Original Data: " + originalData);
        System.out.println("Encrypted Data: " + encryptedData);
    }
}

以上是三种常见的加密解密操作的实现例子。在实际应用中,根据需求选择合适的加密算法,并确保密钥的机密性和安全性,以进行加密和解密操作。