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

Java函数之加密解密:实现字符串加密解密操作。

发布时间:2023-07-04 01:13:33

在Java中,可以使用多种算法来实现字符串的加密解密操作。其中最常用的是使用对称加密算法和非对称加密算法。

1. 对称加密算法:

对称加密算法使用相同的密钥来进行加密和解密操作。常见的对称加密算法有DES、3DES、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 plainText) throws Exception {
        SecretKeySpec secretKey = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedBytes = cipher.doFinal(plainText.getBytes());
        return Base64.getEncoder().encodeToString(encryptedBytes);
    }

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

    public static void main(String[] args) throws Exception {
        String plainText = "Hello, world!";
        String encryptedText = encrypt(plainText);
        System.out.println("Encrypted Text: " + encryptedText);
        String decryptedText = decrypt(encryptedText);
        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 java.security.Security;

import javax.crypto.Cipher;

public class AsymmetricEncryption {

    private static final String ALGORITHM = "RSA";

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

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

    public static void main(String[] args) throws Exception {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

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

        String plainText = "Hello, world!";
        byte[] encryptedBytes = encrypt(plainText.getBytes(), publicKey);
        System.out.println("Encrypted Text: " + new String(encryptedBytes));
        byte[] decryptedBytes = decrypt(encryptedBytes, privateKey);
        System.out.println("Decrypted Text: " + new String(decryptedBytes));
    }
}

使用对称加密算法和非对称加密算法可以实现字符串的加密解密操作,保护数据的安全性。具体选择哪种算法取决于应用的需求和安全要求。