java ca加密_CA数字加密解密Demo

该博客提供了一个Java RSA加密、解密、数字签名及验证的完整示例。通过KeyPairGenerator生成RSA密钥对,使用公钥加密、私钥解密,以及私钥生成数字签名和公钥验证签名的过程。
摘要由CSDN通过智能技术生成

package aisin.text;

import com.google.common.collect.Maps;

import sun.misc.BASE64Decoder;

import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;

import java.io.UnsupportedEncodingException;

import java.security.*;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

import java.util.Map;

/**

* Created by xiang.li on 2015/3/3.

* RSA 加解密工具类

*/

public class test {

/**

* 定义加密方式

*/

private final static String KEY_RSA = "RSA";

/**

* 定义签名算法

*/

private final static String KEY_RSA_SIGNATURE = "MD5withRSA";

/**

* 定义公钥算法

*/

private final static String KEY_RSA_PUBLICKEY = "RSAPublicKey";

/**

* 定义私钥算法

*/

private final static String KEY_RSA_PRIVATEKEY = "RSAPrivateKey";

/**

* 初始化密钥

* @return

*/

public static Map init() {

Map map = null;

try {

KeyPairGenerator generator = KeyPairGenerator.getInstance(KEY_RSA);

generator.initialize(1024);

KeyPair keyPair = generator.generateKeyPair();

// 公钥

RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();

// 私钥

RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();

// 将密钥封装为map

map = Maps.newHashMap();

map.put(KEY_RSA_PUBLICKEY, publicKey);

map.put(KEY_RSA_PRIVATEKEY, privateKey);

} catch (NoSuchAlgorithmException e) {

e.printStackTrace();

}

return map;

}

/**

* 用私钥对信息生成数字签名

* @param data 加密数据

* @param privateKey 私钥

* @return

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值