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

Java函数如何实现加密和解密

发布时间:2023-11-03 10:58:35

Java提供了丰富的加密和解密功能,常用的加密算法有对称加密算法和非对称加密算法。

对称加密算法是指加密和解密使用相同的密钥的算法。常用的对称加密算法有DES、AES等。下面是一个使用AES对称加密算法进行加密和解密的示例代码:

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

public class SymmetricEncryption {
    private static final String AES_ALGORITHM = "AES";

    // 使用AES对称加密算法加密数据
    public static String encrypt(String data, String key) throws Exception {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), AES_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

    // 使用AES对称加密算法解密数据
    public static String decrypt(String encryptedData, String key) throws Exception {
        Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), AES_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedData));
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        String data = "Hello, World!";
        String key = "ABCDEFGHIJKLMNOP";

        try {
            String encryptedData = encrypt(data, key);
            System.out.println("Encrypted data: " + encryptedData);

            String decryptedData = decrypt(encryptedData, key);
            System.out.println("Decrypted data: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

非对称加密算法是指加密和解密使用不同的密钥的算法。常用的非对称加密算法有RSA、DSA等。下面是一个使用RSA非对称加密算法进行加密和解密的示例代码:

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

public class AsymmetricEncryption {
    private static final String RSA_ALGORITHM = "RSA";

    // 使用RSA非对称加密算法加密数据
    public static byte[] encrypt(String data, PublicKey publicKey) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
    }

    // 使用RSA非对称加密算法解密数据
    public static String decrypt(byte[] encryptedData, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(encryptedData);
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }

    public static void main(String[] args) {
        String data = "Hello, World!";

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

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

            String decryptedData = decrypt(encryptedData, privateKey);
            System.out.println("Decrypted data: " + decryptedData);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

以上就是Java中实现加密和解密的示例代码,通过使用对称加密算法和非对称加密算法,可以对数据进行安全的加密和解密。