---
title: "Java中的非对称加密算法RSA实现"
author: "小网客"
date: "2018-08-01T11:14:09Z"
tags: []
categories: ["编程语言"]
slug: "java-rsa"
draft: false
lastmod: "2018-08-01T11:14:09Z"
id: 2427886
url: "https://snv.iteye.com/blog/2427886"
---

需求：  
实现RSA非对称加密算法  
实现：  
public static class RSACoder {  
    public static final String KEY\_ALGORITHM = "RSA";  
    private static final int KEY\_SIZE = 512;  
    private static final String PUBLIC\_KEY = "RSAPublicKey";  
    private static final String PRIVATE\_KEY = "RSAPrivateKey";

    public static Map&lt;String, Object&gt; initKey() throws Exception {  
        KeyPairGenerator keyPairGenerator = KeyPairGenerator  
                .getInstance(KEY\_ALGORITHM);  
        keyPairGenerator.initialize(KEY\_SIZE);  
        KeyPair keyPair = keyPairGenerator.generateKeyPair();  
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();  
        RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();  
        Map&lt;String, Object&gt; keyMap = new HashMap&lt;String, Object&gt;();  
        keyMap.put(PUBLIC\_KEY, publicKey);  
        keyMap.put(PRIVATE\_KEY, privateKey);  
        return keyMap;

    }

    public static byte\[] encryptByPrivateKey(byte\[] data, byte\[] key)  
            throws Exception {  
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);  
        KeyFactory keyFactory = KeyFactory.getInstance(KEY\_ALGORITHM);  
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);  
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
        cipher.init(Cipher.ENCRYPT\_MODE, privateKey);  
        return cipher.doFinal(data);  
    }

    public static byte\[] encryptByPublicKey(byte\[] data, byte\[] key)  
            throws Exception {  
        KeyFactory keyFactory = KeyFactory.getInstance(KEY\_ALGORITHM);  
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);  
        PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);  
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
        cipher.init(Cipher.ENCRYPT\_MODE, pubKey);  
        return cipher.doFinal(data);  
    }

    public static byte\[] decryptByPrivateKey(byte\[] data, byte\[] key)  
            throws Exception {  
        PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);  
        KeyFactory keyFactory = KeyFactory.getInstance(KEY\_ALGORITHM);  
        PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);  
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
        cipher.init(Cipher.DECRYPT\_MODE, privateKey);  
        return cipher.doFinal(data);  
    }

    public static byte\[] decryptByPublicKey(byte\[] data, byte\[] key)  
            throws Exception {  
        KeyFactory keyFactory = KeyFactory.getInstance(KEY\_ALGORITHM);  
        X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);  
        PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);  
        Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());  
        cipher.init(Cipher.DECRYPT\_MODE, pubKey);  
        return cipher.doFinal(data);  
    }

    public static byte\[] getPrivateKey(Map&lt;String, Object&gt; keyMap) {  
        Key key = (Key) keyMap.get(PRIVATE\_KEY);  
        return key.getEncoded();  
    }

    public static byte\[] getPublicKey(Map&lt;String, Object&gt; keyMap)  
            throws Exception {  
        Key key = (Key) keyMap.get(PUBLIC\_KEY);  
        return key.getEncoded();  
    }  
}

摘自:[www.xhuojia.com/zhuanlan/2029977315.html](http://www.xhuojia.com/zhuanlan/2029977315.html)