public class RsaHelper extends Object
caiqy: 一般来说:网上的公钥都是pkcs8的,虽然选了pkcs1加密,但是还是给的pkcs8的公钥,所以不要再对公钥转了,转私钥就行
GitHub:https://github.com/xiangyuecn/RSA-java
https://github.com/xiangyuecn/RSA-java/blob/master/RSA_PEM.java 移植自:https://github.com/xiangyuecn/RSA-csharp/blob/master/RSA_PEM.cs
| Constructor and Description |
|---|
RsaHelper(byte[] modulus,
byte[] exponent,
byte[] dOrNull)
通过公钥指数和私钥指数构造一个PEM,会反推计算出P、Q但和原始生成密钥的P、Q极小可能相同
注意:所有参数首字节如果是0,必须先去掉
|
RsaHelper(byte[] modulus,
byte[] exponent,
byte[] d,
byte[] p,
byte[] q,
byte[] dp,
byte[] dq,
byte[] inverseQ)
通过全量的PEM字段数据构造一个PEM,除了模数modulus和公钥指数exponent必须提供外,其他私钥指数信息要么全部提供,要么全部不提供(导出的PEM就只包含公钥)
注意:所有参数首字节如果是0,必须先去掉
|
RsaHelper(RSAPublicKey publicKey,
RSAPrivateKey privateKeyOrNull)
通过公钥和私钥构造一个PEM
|
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
bigB(BigInteger bigx)
BigInt导出byte整数首字节>0x7F的会加0前导,保证正整数,因此需要去掉0
|
static BigInteger |
bigX(byte[] bigb)
转成正整数,如果是负数,需要加前导0转成正整数
|
static RsaHelper |
fromPem(String pem)
用PEM格式密钥对创建RSA,支持PKCS#1、PKCS#8格式的PEM
|
static RsaHelper |
fromXml(String xml)
将XML格式密钥转成PEM,支持公钥xml、私钥xml
|
RSAPrivateKey |
getRSAPrivateKey()
得到私钥Java对象
|
RSAPublicKey |
getRSAPublicKey()
得到公钥Java对象
|
boolean |
hasPrivate()
是否包含私钥
|
int |
keySize()
秘钥位数
|
String |
toPem(boolean convertToPublic,
boolean privateUsePKCS8,
boolean publicUsePKCS8)
将RSA中的密钥对转换成PEM格式
。
|
String |
toPemPKCS1(boolean convertToPublic)
将RSA中的密钥对转换成PEM PKCS#8格式
。
|
String |
toPemPKCS8(boolean convertToPublic)
将RSA中的密钥对转换成PEM PKCS#8格式
。
|
String |
toXml(boolean convertToPublic)
将RSA中的密钥对转换成XML格式
,如果convertToPublic含私钥的RSA将只返回公钥,仅含公钥的RSA不受影响
|
public RsaHelper(RSAPublicKey publicKey, @Nullable RSAPrivateKey privateKeyOrNull)
publicKey - 必须提供公钥privateKeyOrNull - 私钥可以不提供,导出的PEM就只包含公钥public RsaHelper(byte[] modulus,
byte[] exponent,
byte[] d,
byte[] p,
byte[] q,
byte[] dp,
byte[] dq,
byte[] inverseQ)
public RsaHelper(byte[] modulus,
byte[] exponent,
@Nullable
byte[] dOrNull)
modulus - 必须提供模数exponent - 必须提供公钥指数dOrNull - 私钥指数可以不提供,导出的PEM就只包含公钥public int keySize()
public boolean hasPrivate()
public RSAPublicKey getRSAPublicKey() throws Exception
Exceptionpublic RSAPrivateKey getRSAPrivateKey() throws Exception
Exceptionpublic static BigInteger bigX(byte[] bigb)
public static byte[] bigB(BigInteger bigx)
public static RsaHelper fromPem(String pem) throws Exception
Exceptionpublic String toPemPKCS1(boolean convertToPublic) throws Exception
Exceptionpublic String toPemPKCS8(boolean convertToPublic) throws Exception
Exceptionpublic String toPem(boolean convertToPublic, boolean privateUsePKCS8, boolean publicUsePKCS8) throws Exception
Exceptionpublic String toXml(boolean convertToPublic)
Copyright © 2024. All rights reserved.