JAVA RSA密钥对的生成以及加解密

该博客介绍了如何使用Java实现RSA密钥对的生成以及加密解密过程。通过KeyPairGenerator生成RSA密钥对,然后使用Base64编码将密钥转换为字符串。在加密和解密方法中,分别使用PublicKey和PrivateKey进行操作,实现了数据的安全传输。

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

RSA密钥对的生成:

public class KeyPairGenUtil {

    /**
     * 指定加密算法为RSA
     */
    private static final String ALGORITHM = "RSA";
    /**
     * 密钥长度,用来初始化
     */
    private static final int KEYSIZE = 1024;

    /**
     * 生成密钥对
     *
     * @throws Exception
     */
    public static void generateKeyPair() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGORITHM);

        keyPairGenerator.initialize(KEYSIZE);

        /** 生成密匙对 */
        KeyPair keyPair = keyPairGenerator.generateKeyPair();

        /** 得到公钥 */
        Key publicKey = keyPair.getPublic();

        /** 得到私钥 */
        Key privateKey = keyPair.getPrivate();

        String publicKeyStr = new BASE64Encoder().encode(publicKey.getEncoded());
        String privateKeyStr = new BASE64Encoder().encode(privateKey.getEncoded());
        System.out.println("pub:" + publicKeyStr);
        System.out.println("pri:" + privateKeyStr);
    }

}

加密与解密

public class RsaUtils {
    private static final String ALGORITHM = "RSA";

    /**
     * 得到PublicKey
     * @param publicKeyString
     * @return
     * @throws Exception
     */
    public static PublicKey getPublicKeyFromStringValue(String publicKeyString) throws Exception {
        //base64 decode the key data to get a byte array (byte[])
        byte[] bytes = new BASE64Decoder().decodeBuffer(publicKeyString);
        //Create a new X509EncodedKeySpec using the byte array
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(bytes);
        //Get an instance of KeyFactory using KeyFactory.getInstance("RSA") assuming RSA here
        //call the method generatePublic(KeySpec) with the X509EncodedKeySpec
        PublicKey publicKey = KeyFactory.getInstance(ALGORITHM).generatePublic(x509EncodedKeySpec);
        //Result /should/ be a public key for your usage.
        return publicKey;
    }

    /**
     * 得到PrivateKey
     * @param privateKeyString
     * @return
     * @throws Exception
     */
    public static PrivateKey getPrivateKeyFromStringValue(String privateKeyString) throws Exception {
        //base64 decode the key data to get a byte array (byte[])
        byte[] bytes = new BASE64Decoder().decodeBuffer(privateKeyString);
        //Create a new X509EncodedKeySpec using the byte array
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(bytes);
        //Get an instance of KeyFactory using KeyFactory.getInstance("RSA") assuming RSA here
        //call the method generatePublic(KeySpec) with the X509EncodedKeySpec
        PrivateKey privateKey = KeyFactory.getInstance(ALGORITHM).generatePrivate(pkcs8EncodedKeySpec);
        //Result /should/ be a public key for your usage.
        return privateKey;
    }

    /**
     *
     * @param source
     * @param publicKey
     * @return
     * @throws Exception
     */
    public static String encrypt(String source, PublicKey publicKey) throws Exception {

        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        byte[] b = source.getBytes();

        byte[] b1 = cipher.doFinal(b);
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(b1);
    }

    /**
     *
     * @param encryptedContent
     * @param privateKey
     * @return
     * @throws Exception
     */
    public static String decrypt(String encryptedContent, PrivateKey privateKey) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] b1 = decoder.decodeBuffer(encryptedContent);
        byte[] b = cipher.doFinal(b1);
        return new String(b);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值