java rsautil_RSA非对称算法Java工具类RSAUtil

这是一个Java实现的RSA非对称加密算法工具类RSAUtil,包含公钥和私钥的加密、解密、签名及验证等功能。通过公钥和私钥进行数据加解密,并提供了初始化密钥对、获取公钥和私钥的方法。
摘要由CSDN通过智能技术生成

RSA非对称算法Java工具类RSAUtil(公钥和私钥需要自行生成)

import java.nio.charset.StandardCharsets;

import java.security.Key;

import java.security.KeyFactory;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.Signature;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

import java.util.HashMap;

import java.util.Map;

import javax.crypto.Cipher;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

/**

* RSA非对称算法

*

* @author chenxy

*/

public final class RSAUtil {

/**

* 私钥解密

*

* @param data 需解密的数据

* @param privateKey 私钥

* @return

* @throws Exception

*/

public static byte[] decryptByPrivateKey(byte[] data, String privateKey) throws Exception {

byte[] keyBytes = new BASE64Decoder().decodeBuffer(privateKey);

PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyBytes);

KeyFactory keyFactory = KeyFactory.getInstance("RSA");

Key key = keyFactory.generatePrivate(pkcs8EncodedKeySpec);

Cipher cipher = Cipher.getInstance

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值