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

Java函数:如何实现加密和解密功能

发布时间:2023-08-01 13:23:19

在Java中,可以使用不同的加密算法来实现加密和解密功能。常见的加密算法有对称加密和非对称加密。

1. 对称加密:

对称加密算法使用相同的密钥进行加密和解密。常见的对称加密算法有DES、AES等。

- 加密:

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

public class SymmetricEncryption {
    public static void main(String[] args) throws Exception {
        String plainText = "Hello, world!"; // 待加密的明文
        String algorithm = "AES"; // 加密算法
        SecretKey secretKey = generateSecretKey(algorithm); // 生成密钥
        byte[] encryptedText = encrypt(plainText.getBytes(), secretKey, algorithm); // 加密
        System.out.println("密文:" + new String(encryptedText));
    }

    // 生成密钥
    public static SecretKey generateSecretKey(String algorithm) throws Exception {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
        return keyGenerator.generateKey();
    }

    // 加密
    public static byte[] encrypt(byte[] plainText, SecretKey secretKey, String algorithm) throws Exception {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        return cipher.doFinal(plainText);
    }
}

- 解密:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;

public class SymmetricDecryption {
    public static void main(String[] args) throws Exception {
        byte[] encryptedText = "密文".getBytes(); // 待解密的密文
        String algorithm = "AES"; // 加密算法
        SecretKey secretKey = getSecretKey(); // 获取密钥
        byte[] decryptedText = decrypt(encryptedText, secretKey, algorithm); // 解密
        System.out.println("明文:" + new String(decryptedText));
    }

    // 获取密钥
    public static SecretKey getSecretKey() throws Exception {
        // 密钥的获取方式要和加密时的方式一致
        return SymmetricEncryption.generateSecretKey("AES");
    }

    // 解密
    public static byte[] decrypt(byte[] encryptedText, SecretKey secretKey, String algorithm) throws Exception {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, secretKey);
        return cipher.doFinal(encryptedText);
    }
}

2. 非对称加密:

非对称加密算法使用公钥进行加密,私钥进行解密。常见的非对称加密算法有RSA、DSA等。

- 加密:

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

public class AsymmetricEncryption {
    public static void main(String[] args) throws Exception {
        String plainText = "Hello, world!"; // 待加密的明文
        String algorithm = "RSA"; // 加密算法
        KeyPair keyPair = generateKeyPair(algorithm); // 生成公私钥对
        byte[] encryptedText = encrypt(plainText.getBytes(), keyPair.getPublic(), algorithm); // 加密
        System.out.println("密文:" + new String(encryptedText));
    }

    // 生成公私钥对
    public static KeyPair generateKeyPair(String algorithm) throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
        return keyPairGenerator.generateKeyPair();
    }

    // 加密
    public static byte[] encrypt(byte[] plainText, PublicKey publicKey, String algorithm) throws Exception {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(plainText);
    }
}

- 解密:

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

public class AsymmetricDecryption {
    public static void main(String[] args) throws Exception {
        byte[] encryptedText = "密文".getBytes(); // 待解密的密文
        String algorithm = "RSA"; // 加密算法
        KeyPair keyPair = getKeyPair(); // 获取公私钥对
        byte[] decryptedText = decrypt(encryptedText, keyPair.getPrivate(), algorithm); // 解密
        System.out.println("明文:" + new String(decryptedText));
    }

    // 获取公私钥对
    public static KeyPair getKeyPair() throws Exception {
        // 密钥的获取方式要和加密时的方式一致
        return AsymmetricEncryption.generateKeyPair("RSA");
    }

    // 解密
    public static byte[] decrypt(byte[] encryptedText, PrivateKey privateKey, String algorithm) throws Exception {
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(encryptedText);
    }
}

以上是使用Java实现加密和解密功能的简单示例,可以根据实际需求进行调整和扩展。