rsa签名算法 java_JAVA中RSA签名算法实现

import java.security.InvalidKeyException;import java.security.Key;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.NoSuchAlgorith...
摘要由CSDN通过智能技术生成

import java.security.InvalidKeyException;

import java.security.Key;

import java.security.KeyFactory;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.NoSuchAlgorithmException;

import java.security.PrivateKey;

import java.security.PublicKey;

import java.security.Signature;

import java.security.SignatureException;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import java.security.spec.InvalidKeySpecException;

import java.security.spec.PKCS8EncodedKeySpec;

import java.security.spec.X509EncodedKeySpec;

import java.util.HashMap;

import java.util.Map;

import javax.crypto.BadPaddingException;

import javax.crypto.Cipher;

import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;

import org.apache.commons.codec.DecoderException;

import org.apache.commons.codec.binary.Hex;

/**

* Created by Lb on 2017/9/10.

*/

public class RSACoder {

//非对称加密算法

public static final String KEY_ALGORITHM = "RSA";

//数字签名 签名/验证算法

public static final String SIGNATURE_ALGORITHM = "SHA1withRSA";

//公钥

private static final String PUBLIC_KEY = "RSAPublicKey";

//私钥

private static final String PRIVATE_KEY = "RSAPrivateKey";

//RSA密钥长度默认1024位,密钥长度必须是64的倍数,范围在512-65536位之间

private static final int KEY_SIZE = 512;

/**

* 私钥解密

*/

public static byte[] decryptByPrivateKey(byte[] data, byte[] key)

throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {

//取得私钥

PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(key);

KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

//生成私钥

PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec);

//对数据解密

Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

cipher.init(Cipher.DECRYPT_MODE, privateKey);

return cipher.doFinal(data);

}

/**

* 公钥解密

*/

public static byte[] decryptByPublicKey(byte[] data, byte[] key)

t

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值