Java RSA加密(二)--Base64输出

Java RSA加密(二)--Base64输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
RSA是一种非对称加密算法,用于加密和解密数据。在Java中,可以使用Java内置的RSA算法库来实现RSA加密和解密,并使用Base64进行数据的编码和解码。 首先,你需要生成RSA密钥对。可以使用`KeyPairGenerator`类来生成密钥对,然后将公钥和私钥分别保存起来。 ```java import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Base64; public class RSAExample { public static void main(String[] args) throws NoSuchAlgorithmException { // 生成RSA密钥对 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048); KeyPair keyPair = keyPairGenerator.generateKeyPair(); // 获取公钥和私钥 PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // 将公钥和私钥转换为Base64字符串 String publicKeyBase64 = Base64.getEncoder().encodeToString(publicKey.getEncoded()); String privateKeyBase64 = Base64.getEncoder().encodeToString(privateKey.getEncoded()); System.out.println("公钥: " + publicKeyBase64); System.out.println("私钥: " + privateKeyBase64); } } ``` 接下来,你可以使用生成的公钥进行数据的加密,使用私钥进行数据的解密。 ```java import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.InvalidKeySpecException; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.Base64; import javax.crypto.Cipher; public class RSAExample { public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeySpecException { // 公钥和私钥的Base64字符串 String publicKeyBase64 = "your-public-key-base64"; String privateKeyBase64 = "your-private-key-base64"; // 将Base64字符串转换为公钥和私钥 byte[] publicKeyBytes = Base64.getDecoder().decode(publicKeyBase64); byte[] privateKeyBytes = Base64.getDecoder().decode(privateKeyBase64); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyBytes)); PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyBytes)); // 加密数据 String plaintext = "Hello, RSA!"; byte[] ciphertext = encrypt(publicKey, plaintext.getBytes()); // 解密数据 byte[] decryptedTextBytes = decrypt(privateKey, ciphertext); String decryptedText = new String(decryptedTextBytes); System.out.println("加密后的数据: " + Base64.getEncoder().encodeToString(ciphertext)); System.out.println("解密后的数据: " + decryptedText); } // 使用公钥进行数据加密 public static byte[] encrypt(PublicKey publicKey, byte[] plaintext) { try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(plaintext); } catch (Exception e) { e.printStackTrace(); } return null; } // 使用私钥进行数据解密 public static byte[] decrypt(PrivateKey privateKey, byte[] ciphertext) { try { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(ciphertext); } catch (Exception e) { e.printStackTrace(); } return null; } } ``` 以上代码演示了如何使用Java中的RSA算法和Base64进行加密和解密。请将`your-public-key-base64`和`your-private-key-base64`替换为实际的公钥和私钥的Base64字符串。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值