浅尝Java中RSA加解密

本文详细介绍并展示了如何在Java中实现RSA加密算法的具体步骤,包括生成密钥对、使用私钥加密及公钥解密的过程,并通过示例代码验证其正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


非常不错的对称 与 非对称加密的文章 :http://www.cnblogs.com/jfzhu/p/4020928.html


--------JAVA中RSA


java中已经提供了对称加解密的工具类,只要熟悉对应的API就可使用

public class RSAUtil {

    private static final String PRIVATE_KEY = "privateKey";
    private static final String PUBLIC_KEY = "publicKey";


    public static Map<String, byte[]> generateKey() throws NoSuchAlgorithmException {
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(1024);
        KeyPair kp = keyGen.generateKeyPair();
        RSAPrivateKey privateKey = (RSAPrivateKey) kp.getPrivate();
        RSAPublicKey publicKey = (RSAPublicKey) kp.getPublic();
        Map<String, byte[]> map = new HashMap<>();
        byte[] privBytes = privateKey.getEncoded();
        byte[] pubBytes = publicKey.getEncoded();
        map.put(PRIVATE_KEY, privBytes);
        map.put(PUBLIC_KEY, pubBytes);
        return map;
    }

    public static byte[] encryptByPrivateKey(byte[] content, byte[] key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(key);
        Key privateKey = kf.generatePrivate(spec);
        Cipher cipher = Cipher.getInstance(kf.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return cipher.doFinal(content);
    }

    public static byte[] encryptByPublicKey(byte[] content, byte[] key) throws Exception {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec spec = new X509EncodedKeySpec(key);
        Key privateKey = kf.generatePublic(spec);
        Cipher cipher = Cipher.getInstance(kf.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, privateKey);
        return cipher.doFinal(content);
    }

    public static String decryptByPrivateKey(byte[] content, byte[] key) throws Exception {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(key);
        Key privateKey = kf.generatePrivate(spec);
        Cipher cipher = Cipher.getInstance(kf.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return new String(cipher.doFinal(content), "UTF-8");
    }

    public static String decryptByPublicKey(byte[] content, byte[] key) throws Exception {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        X509EncodedKeySpec spec = new X509EncodedKeySpec(key);
        Key privateKey = kf.generatePublic(spec);
        Cipher cipher = Cipher.getInstance(kf.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return new String(cipher.doFinal(content), "UTF-8");
    }

    public static void main(String[] args) throws Exception {
        Map<String, byte[]> map = generateKey();
        String content = "嘿鸭哦";
        byte[] encryptedByPrivateKey = encryptByPrivateKey(content.getBytes("UTF-8"), map.get(PRIVATE_KEY));
        String decryptedByPublicKey = decryptByPublicKey(encryptedByPrivateKey, map.get(PUBLIC_KEY));
        System.out.println(decryptedByPublicKey);

        byte[] encryptedByPublicKey = encryptByPublicKey(content.getBytes("UTF-8"), map.get(PUBLIC_KEY));
        String decrptedByPrivateKey = decryptByPrivateKey(encryptedByPublicKey, map.get(PRIVATE_KEY));
        System.out.println(decrptedByPrivateKey);
    }
}


做了之后发现无论对于加解密,涉及私钥的都是用PKCS8EncodedKeySpec

公钥都是用 X509EncodedKeySpec。

具体类头说明也说的比较详细了。


看了公司的公钥,最终发送出去还进行了一层加密



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值