我可以直接从文件中读取公钥和私钥,因为文件流将其转换为PublicKey和PrivateKey对象。但如果以字符串形式加载,则失败。
工作代码
public static RSAPrivateKey getPrivateKey(String filename) throws IOException, GeneralSecurityException {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
IOUtils.toByteArray(new FileInputStream(filename)));
return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec);
}
public static RSAPublicKey getPublicKey(String filename) throws IOException, GeneralSecurityException {
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
IOUtils.toByteArray(new FileInputStream(filename)));
return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec);
}
public static void main(String[] args) throws JOSEException, ParseException, IOException, GeneralSecurityException {
String publicKeyPathName = "../public";
String privateKeyPathName = "../private";
/// loading from file
RSAPublicKey publicKey = new MYClass().getPublicKey(publicKeyPathName);
RSAPrivateKey privateKey = new MYClass().getPrivateKey(privateKeyPathName);
}
public static RSAPrivateKey parsePrivateKey(String content) throws IOException, GeneralSecurityException {
content = content.replaceAll("\\n", "").replace("-----BEGIN RSA PRIVATE KEY-----", "")
.replace("-----END RSA PRIVATE KEY-----", "");
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(content.getBytes(StandardCharsets.UTF_8));
return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(pkcs8EncodedKeySpec);
}
public static RSAPublicKey parsePublicKey(String content) throws IOException, GeneralSecurityException {
content = content.replaceAll("\\n", "").replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "");
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(content.getBytes(StandardCharsets.UTF_8));
return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec);
}
public String getPublicKey() {
return "-----BEGIN PUBLIC KEY-----\n"
+ "MIIBIjANBgkqhkiG9w0BAQ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w7wL+R5ofov4T+0vB3ST+aztphp2G4\n"
+ "-----END RSA PRIVATE KEY-----";
}
public static void main(String[] args) throws JOSEException, ParseException, IOException, GeneralSecurityException {
String publicKeyString = new MyClass().getPublicKey();
RSAPublicKey publicKey = new MyClass().parsePublicKey(publicKeyString);
String privateKeyString = new MyClass().getPrivateKey();
RSAPrivateKey privateKey = new
MyClass().parsePrivateKey(privateKeyString);
}
线程“main”java.security.spec.invalidkeyspection:java.security.invalidkeyexception:sun.security.rsa.rsakeyfactory.engineGeneratePublic(rsakeyfactory.java:205)在j.myclass.parsePublickey(myclass.java:223)在java.security.rsa.rsakeyfactory.generatePublic(keyfactory.java:205)在在sun.security.rsa.rsapublickeyimpl.(rsapublickeyimpl.java:84).在sun.security.rsa.rsakeyfactory.generatePublic(rsakeyfactory.java:298).在sun.security.rsa.rsakeyfactory.engineGeneratePublic(rsakeyfactory.java:201).解码(x509key.java:403).还有3个
我的习惯是,
从数据库中读取一个键并进行转换。
您使用content.getBytes(StandardCharsets.utf_8)
而不是base64来解码PEM页眉和页脚行中的文本(从-----
)。
如何使用作为字符串提供的公共和私有RSA密钥进行加密和解密。因为我正在使用RSACryptoServiceProvider,它需要XML格式,所以是否有可能使用提供的字符串。谢谢。
我一直在阅读一些RSA文献和堆栈溢出问题,但我没有得到明确的答案。 仅给定一个RSA私钥模和私钥指数,这是我所拥有的全部(也足够用于所有密码相关操作),我能得到相关的公钥模和公钥指数吗? 另外,我是否可以仅用这两个参数获得私钥的编码形式?我在java中尝试了以下方法(java不是实际的请求),但是支持它的OpenSSL引擎失败,错误为:04000090:RSA例程:openSSL_internal
有没有人可以粘贴一些用于存储私钥和公钥的示例代码。 非常感谢!
出于明显的安全原因,我需要用RSA私钥和公钥加密和解密用户的PIN码,我找到了工作解决方案,看起来像: 一切正常,但在本例中,键对不是静态的,每次都会生成新值,但我需要使用相同的键,它们表示为字符串变量: 有没有办法将这些变量强制转换为PublicKey和PrivateKey类?
从这里我指出了自己如何获得公钥,并使用EC_POINT_bn2point,而不是hex2point,根据OpenSSL源代码,它在内部执行BN_hex2bn。 那么,为什么EC_POINT_bn2point返回null呢?我正在认真考虑重新编译OpenSSL,并放一些调试例程来找出它失败的原因。
我有一个公钥和一个私钥,还有一个字符串,我想要解密。 公钥的格式如下: 私钥的格式如下: 我要解密的字符串已经使用公钥加密,然后我需要使用私钥解密它。