小番茄程序员 ©免责声明

文章标签 Java RSA 文章分类 后端技术 阅读数 28

@免责声明:本文转载来自互联网,不代表本网站的观点和立场。 如果你觉得好,欢迎分享此网址给你的朋友。

Java中可以使用RSA算法进行加密、解密和签名校验。下面是使用RSA进行加密、解密和签名校验的示例代码:

  1. 密钥生成:

    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.NoSuchAlgorithmException;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    
    public class RSAKeyGenerator {
        public static KeyPair generateKeyPair() throws NoSuchAlgorithmException {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(2048); // 指定密钥长度为2048位
            return keyPairGenerator.generateKeyPair();
        }
    
        public static void main(String[] args) throws NoSuchAlgorithmException {
            KeyPair keyPair = generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
    
            System.out.println("Public Key: " + publicKey);
            System.out.println("Private Key: " + privateKey);
        }
    }
    

    运行以上代码,将生成一对RSA公私钥。

  2. 加密解密:

    import javax.crypto.Cipher;
    import java.nio.charset.StandardCharsets;
    import java.security.*;
    import java.util.Base64;
    
    public class RSAEncryptDecrypt {
        public static String encrypt(String plainText, PublicKey publicKey) throws Exception {
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(encryptedBytes);
        }
    
        public static String decrypt(String encryptedText, PrivateKey privateKey) throws Exception {
            byte[] encryptedBytes = Base64.getDecoder().decode(encryptedText);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
            return new String(decryptedBytes, StandardCharsets.UTF_8);
        }
    
        public static void main(String[] args) throws Exception {
            KeyPair keyPair = RSAKeyGenerator.generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
    
            String plainText = "Hello, RSA!";
            String encryptedText = encrypt(plainText, publicKey);
            String decryptedText = decrypt(encryptedText, privateKey);
    
            System.out.println("Plain Text: " + plainText);
            System.out.println("Encrypted Text: " + encryptedText);
            System.out.println("Decrypted Text: " + decryptedText);
        }
    }
    

    运行以上代码,将输出明文、加密后的密文和解密后的明文。

  3. 签名校验:

    import java.nio.charset.StandardCharsets;
    import java.security.*;
    
    public class RSASignatureVerification {
        public static byte[] sign(String plainText, PrivateKey privateKey) throws Exception {
            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initSign(privateKey);
            signature.update(plainText.getBytes(StandardCharsets.UTF_8));
            return signature.sign();
        }
    
        public static boolean verify(String plainText, byte[] signatureBytes, PublicKey publicKey) throws Exception {
            Signature signature = Signature.getInstance("SHA256withRSA");
            signature.initVerify(publicKey);
            signature.update(plainText.getBytes(StandardCharsets.UTF_8));
            return signature.verify(signatureBytes);
        }
    
        public static void main(String[] args) throws Exception {
            KeyPair keyPair = RSAKeyGenerator.generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
    
            String plainText = "Hello, RSA!";
            byte[] signature = sign(plainText, privateKey);
            boolean isVerified = verify(plainText, signature, publicKey);
    
            System.out.println("Plain Text: " + plainText);
            System.out.println("Signature: " + Base64.getEncoder().encodeToString(signature));
            System.out.println("Verification Result: " + isVerified);
        }
    }
    

    运行以上代码,将输出明文、签名和校验结果。

以上是使用RSA进行加密解密和签名校验的Java示例代码。请注意,RSA算法需要正确处理密钥的生成、存储和保管,同时保证传输的安全性。

本文地址:https://www.meishiadd.com/java/15.html

相关文章

友情链接

Copyright © 2021-2023 MEISHIADD.COM 版权所有 京ICP备14024137号