java的RSA加密解密示例

RSA算法是一种非对称加密算法,公钥和私钥都可以用于加密和解密操作。在RSA算法中,公钥用于加密数据,私钥用于解密数据。

具体来说,使用公钥加密的数据只能使用相应的私钥进行解密。而使用私钥加密的数据则可以使用相应的公钥进行解密。

这种非对称性使得RSA算法非常适合用于加密通信和数字签名等场景,其中公钥通常用于加密传输的数据,而私钥用于解密接收到的数据。这样,只有持有私钥的一方能够解密加密的数据,从而确保了数据的机密性和完整性。

虽然公钥相同,但是每次加密后的密文是不一样的。

package xin.students.exam;

import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class EncryptionUtils {
    //公钥
    private static String publicKeyStr;
    //私钥
    private static String privateKeyStr;

    static {
        try {
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
            keyPairGenerator.initialize(1024);
            KeyPair keyPair = keyPairGenerator.generateKeyPair();
            PublicKey publicKey = keyPair.getPublic();
            PrivateKey privateKey = keyPair.getPrivate();
            publicKeyStr = Base64.getEncoder().encodeToString(publicKey.getEncoded());
            privateKeyStr = Base64.getEncoder().encodeToString(privateKey.getEncoded());
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    private static String encryptFun(String plainText, String publicKeyStr) throws Exception {
        byte[] bytes = Base64.getDecoder().decode(publicKeyStr);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(keySpec);

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] cipherText = cipher.doFinal(plainText.getBytes());
        return Base64.getEncoder().encodeToString(cipherText);
    }

    private static String decryptFun(String cipherText, String privateKeyStr) throws Exception {
        byte[] bytes = Base64.getDecoder().decode(privateKeyStr);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey privateKey = keyFactory.generatePrivate(keySpec);

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] plainText = cipher.doFinal(Base64.getDecoder().decode(cipherText));
        return new String(plainText);
    }

    //用于加密
    public static String encrypt(String plainText, String publicKeyStr) throws Exception {
        return EncryptionUtils.encryptFun(plainText, publicKeyStr);
    }

    //用于解密
    public static String decrypt(String cipherText, String privateKeyStr) throws Exception {
        return EncryptionUtils.decryptFun(cipherText, privateKeyStr);
    }

    public static void main(String[] args) throws Exception {
        String plainText = "李义新";
        // Encrypt the plain text using the public key
        String encrypted = EncryptionUtils.encrypt(plainText, EncryptionUtils.publicKeyStr);
        System.out.println("Encrypted Text: " + encrypted);

        // Decrypt the encrypted text using the private key
        String decrypted = EncryptionUtils.decrypt(encrypted, EncryptionUtils.privateKeyStr);
        System.out.println("Decrypted Text: " + decrypted);
    }
}

 

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值