久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我們如何將字符串從 PEM 轉換為 DER 格式

How do we convert a String from PEM to DER format(我們如何將字符串從 PEM 轉換為 DER 格式)
本文介紹了我們如何將字符串從 PEM 轉換為 DER 格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

以以下格式發送字符串:

Have a String being sent from in the below format:

-----BEGIN RSA PUBLIC KEY-----
MIGHAoGBANAahj75ZIz9nXqW2H83nGcUao4wNyYZ9Z1kiNTUYQl7ob/RBmDzs5rY
mUahXAg0qyS7+a55eU/csShf5ATGzAXv+DDPcz8HrSTcHMEFpuyYooX6PrIZ07Ma
XtsJ2J4mhlySI5uOZVRDoaFY53MPQx5gud2quDz759IN/0gnDEEVAgED
-----END RSA PUBLIC KEY-----

如何從這個字符串構造一個 PublicKey 對象?已嘗試以下去掉頁眉頁腳和base64解碼緩沖區

How do i construct a PublicKey Object from this string ? Have tried the below Remove the header and footer and base64 decode the buffer

public static PublicKey getFromString(String keystr) throws Exception
  {
  //String S1= asciiToHex(keystr);
   byte[] keyBytes = new sun.misc.BASE64Decoder().decodeBuffer(keystr);
   X509EncodedKeySpec spec =
       new X509EncodedKeySpec(keyBytes);
     KeyFactory kf = KeyFactory.getInstance("RSA");
     return kf.generatePublic(spec);

  }

這會失敗,要么是無效的密鑰格式,要么會出現以下錯誤

This fails either as an invalid key format or will get below error

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: algid parse error, not a sequence
 at sun.security.rsa.RSAKeyFactory.engineGeneratePublic(RSAKeyFactory.java:188)
 at java.security.KeyFactory.generatePublic(KeyFactory.java:304)
 at PublicKeyReader.getFromString(PublicKeyReader.java:30)
 at Tst.main(Tst.java:36)

正在通過openSSL的API生成密鑰PEM_write_bio_RSAPublicKey(bio, rsa);

The Key is being generated thro the API of openSSL PEM_write_bio_RSAPublicKey(bio, rsa);

推薦答案

通過調用 PEM_write_bio_RSAPublicKey,只有密鑰模數和公共指數被編碼到輸出 PEM 數據中.但是 X509EncodedKeySpec 應該是這種 ASN.1 密鑰格式:

by calling PEM_write_bio_RSAPublicKey only the key modulus and public exponent are encoded into the output PEM data. However the X509EncodedKeySpec is expected this ASN.1 key format:

 SubjectPublicKeyInfo ::= SEQUENCE {
   algorithm AlgorithmIdentifier,
   subjectPublicKey BIT STRING }

您應該使用 PEM_write_bio_PUBKEY 函數,該函數使用 SubjectPublicKeyInfo 結構對公鑰進行編碼,正如 X509EncodedKeySpec

You should use the PEM_write_bio_PUBKEY function which encodes the public key using the SubjectPublicKeyInfo structure which as expected by X509EncodedKeySpec

解碼密鑰的另一種可能的解決方案.不幸的是,我認為不可能只使用標準 JDK API,但可以使用 Bouncycastle圖書館

An other possible solution to decode the key. Unfortunately I don't think it is possible to do only with the standard JDK API but it can be done with the Bouncycastle library

import org.bouncycastle.asn1.*;
import org.bouncycastle.asn1.x509.RSAPublicKeyStructure;

public static PublicKey getFromString(String keystr) throws Exception
{
  //String S1= asciiToHex(keystr);
   byte[] keyBytes = new sun.misc.BASE64Decoder().decodeBuffer(keystr);
   ASN1InputStream in = new ASN1InputStream(keyBytes);
   DERObject obj = in.readObject();
   RSAPublicKeyStructure pStruct = RSAPublicKeyStructure.getInstance(obj);
   RSAPublicKeySpec spec = new RSAPublicKeySpec(pStrcut.getModulus(), pStruct.getPublicExponent());
   KeyFactory kf = KeyFactory.getInstance("RSA");
   return kf.generatePublic(spec);
}

這篇關于我們如何將字符串從 PEM 轉換為 DER 格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 不卡视频一区二区三区 | 亚洲乱码一区二区三区在线观看 | 日韩中文在线观看 | 成人亚洲片 | 欧美成人专区 | av中文网| 成人精品一区二区三区中文字幕 | 99在线观看视频 | 亚洲精品91 | 91精品国产综合久久久久久丝袜 | 99热.com| 日日摸日日添日日躁av | 免费在线黄 | 欧美精品1区 | 欧美日韩在线免费 | 97av视频在线观看 | 精品久久久久一区二区国产 | 亚洲第一色av| 天堂av中文在线 | 污片在线观看 | 国产视频h | 成人黄页在线观看 | 日韩在线电影 | 中文字幕国产视频 | 国产成人精品亚洲日本在线观看 | 在线看91| 欧美日韩一区二区在线播放 | 久久综合爱 | 国产激情三区 | 午夜三级视频 | 中文字幕av色 | 99精品国产一区二区三区 | 欧区一欧区二欧区三免费 | 国产一区二区三区视频 | 国产精品久久久久久久午夜片 | 国产精品一区二区不卡 | 成人免费小视频 | 国内精品久久久久 | 超碰在线人 | 婷婷丁香激情 | 中文字幕亚洲在线 |