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

如何编写Java函数来对字符串进行加密和解密?

发布时间:2023-07-03 02:52:46

在Java中,可以使用各种加密算法来对字符串进行加密和解密操作。下面将介绍两种常用的加密算法:对称加密和非对称加密。

1. 对称加密:

对称加密是指加密和解密使用相同的秘钥。Java提供了多种对称加密算法,常见的有DES、AES等。下面是一个使用AES算法对字符串进行加密和解密的示例代码:

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

public class SymmetricEncryption {

    public static String encrypt(String text, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES");
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(text.getBytes());
        return Base64.getEncoder().encodeToString(encrypted);
    }

    public static String decrypt(String encryptedText, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("AES");
        SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] original = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        return new String(original);
    }

    public static void main(String[] args) throws Exception {
        String plainText = "Hello, World!";
        String key = "ThisIsASecretKey";

        String encryptedText = encrypt(plainText, key);
        System.out.println("Encrypted Text: " + encryptedText);

        String decryptedText = decrypt(encryptedText, key);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}

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 {

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

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

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

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();

        byte[] encryptedText = encrypt(plainText, publicKey);
        System.out.println("Encrypted Text: " + new String(encryptedText));

        String decryptedText = decrypt(encryptedText, privateKey);
        System.out.println("Decrypted Text: " + decryptedText);
    }
}

以上是使用对称加密和非对称加密算法进行字符串加密和解密的示例代码,你可以根据实际需求选择合适的加密算法和秘钥长度。