- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我可以直接从文件中读取公钥和私钥,因为文件流将其转换为公钥和私钥对象。但如果作为字符串加载它会失败。
工作代码
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"
+ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAswPxKZWJEQ0RVtiaoFIU"
+ "jndalEp2sZF87Cy9IxW1FxxcfDmZRlkMDe+5X1lzpOjFHP8mBZa8vhMjSKbJY2wt"
+ "MyxckbBoTjAIefi34KuD0Q6j9LRt1r1iQ+nMYezk5N83ruqyRHtB4a8AOkw3iE2z"
+ "zA74uAkCzPiYUrAES1I9mVbpxJZf2Ej0px6drkd4OQ2aPnwR13gWTnmqFWV+Xkl6"
+ "WapMArewaAUXU9zDqjn4ZpNCkgx6m4CK7PDt+VgYUi+sjI0WzPIb0FMDTYSe1iX6"
+ "mfIabvoetxUmtyQgu4RfHIiOF0no6VEPh31of2B+KZX2OjHKjyKBGUxUkDLm+J2J"
+ "CwIDAQAB\n"
+ "-----END PUBLIC KEY-----";
}
public String getPrivateKey() {
return "-----BEGIN RSA PRIVATE KEY-----\n"
+ "MIIEowIBAAKCAQEAswPxKZWJEQ0RVtiaoFIUjndalEp2sZF87Cy9IxW1FxxcfDmZ"
+ "RlkMDe+5X1lzpOjFHP8mBZa8vhMjSKbJY2wtMyxckbBoTjAIefi34KuD0Q6j9LRt"
+ "1r1iQ+nMYezk5N83ruqyRHtB4a8AOkw3iE2zzA74uAkCzPiYUrAES1I9mVbpxJZf"
+ "2Ej0px6drkd4OQ2aPnwR13gWTnmqFWV+Xkl6WapMArewaAUXU9zDqjn4ZpNCkgx6"
+ "m4CK7PDt+VgYUi+sjI0WzPIb0FMDTYSe1iX6mfIabvoetxUmtyQgu4RfHIiOF0no"
+ "6VEPh31of2B+KZX2OjHKjyKBGUxUkDLm+J2JCwIDAQABAoIBAFj59p4SoyQEeL/P"
+ "vVs+MNV24WCfektb4tmIiyGrIm0wPY/JGTPsAp/SrTKFRYOL5p04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KuzwEfP7NgeLZdtqjiZBCZiLoP8SRm68FJS"
+ "2UpGX4K7rQhx66H6UwXHylBfush56Z8Y9g7PYel83sy+4bqjod1a5TqNBJMkhDQi"
+ "DrQmDIfpFoRYcOyB4+BeF/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);
}
我尝试不删除第一行,并尝试将文件读取为 string 。他们都没有工作。告诉我正确的使用方法。
我收到的错误是
Exception in thread "main" java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:205) at java.security.KeyFactory.generatePublic(KeyFactory.java:334) at j.MyClass.parsePublicKey(MyClass.java:136) at j.MyClass.main(MyClass.java:223) Caused by: java.security.InvalidKeyException: invalid key format at sun.security.x509.X509Key.decode(X509Key.java:387) at sun.security.x509.X509Key.decode(X509Key.java:403) at sun.security.rsa.RSAPublicKeyImpl.(RSAPublicKeyImpl.java:84) at sun.security.rsa.RSAKeyFactory.generatePublic(RSAKeyFactory.java:298) at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:201) ... 3 more
我的用例是,
从数据库读取 key 并进行转换。
最佳答案
您正在使用 content.getBytes(StandardCharsets.UTF_8)
而不是 Base 64 解码 PEM 页眉和页脚行中的文本(以 -----
开头) >).
关于java - 如何解析内存字符串以获取私钥和公钥 RSA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42207104/
我是一名优秀的程序员,十分优秀!