Java非对称加密主要使用公钥和私钥进行加密和解密操作。以下是一个简单的示例,展示了如何使用RSA算法进行非对称加密和解密:

1. 首先,生成一对公钥和私钥:


import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
public class RSAKeyPairGenerator {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PublicKey publicKey = keyPair.getPublic();
        PrivateKey privateKey = keyPair.getPrivate();
        System.out.println("公钥: " + publicKey);
        System.out.println("私钥: " + privateKey);
    }
}

2. 使用公钥进行加密:

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
public class RSAEncryption {
    public static void main(String[] args) throws Exception {
        String plainText = "这是一个待加密的字符串";
        String publicKeyString = "这里填写你的公钥";
        PublicKey publicKey = getPublicKey(publicKeyString);
        byte[] encryptedBytes = encrypt(plainText, publicKey);
        String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
        System.out.println("加密后的文本: " + encryptedText);
    }
    private static PublicKey getPublicKey(String publicKeyString) throws Exception {
        byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyString);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(keySpec);
    }
    private 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(StandardCharsets.UTF_8));
    }
}

3. 使用私钥进行解密:

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.PrivateKey;
import java.util.Base64;
public class RSADecryption {
    public static void main(String[] args) throws Exception {
        String encryptedText = "这里填写加密后的文本";
        String privateKeyString = "这里填写你的私钥";
        PrivateKey privateKey = getPrivateKey(privateKeyString);
        String decryptedText = decrypt(encryptedText, privateKey);
        System.out.println("解密后的文本: " + decryptedText);
    }
    private static PrivateKey getPrivateKey(String privateKeyString) throws Exception {
        byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyString);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(keySpec);
    }
    private static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
        return new String(decryptedBytes, StandardCharsets.UTF_8);
    }
}

注意:请将`publicKeyString`和`privateKeyString`替换为实际的公钥和私钥。


 您阅读本篇文章共花了: