openssl從記憶體中讀取RSA公鑰

2021-09-13 03:37:37 字數 690 閱讀 6858

背景:近期需要在專案中進行rsa簽名驗證,廠商會給出pem格式的rsa公鑰。在以往專案中使用openssl讀取rsa公鑰時基本都是從pem檔案中讀取,基本沒什麼問題,可最近由於專案需要需要從資料庫中讀取rsa公鑰,經查資料發現openssl提供了bio介面以支援各種形式的秘鑰讀取。

在使用bio介面從記憶體中讀取pem格式的公鑰時,總是讀取公鑰失敗,經不斷查詢資料,發現在我們得到base64編碼的rsa公鑰後,從記憶體中讀取這個公鑰時要注意以下幾點:

(1)公鑰字串開頭要加上「-----begin public key-----\n」,結尾加上「\n-----end public key-----\n」。否則會出現error:0906d06c:pem routines:pem_read_bio:no start line

(2)公鑰字串每隔64個字元要加乙個換行,否則會報秘鑰格式錯誤。

c++**實現舉例:

bio *bio = null; 

rsa *rsa = null; 

char *chpublickey = const_cast(strpublickey.c_str());

if ((bio = bio_new_mem_buf(chpublickey, -1)) == null)       //從字串讀取rsa公鑰

{     

cout<<"bio_new_mem_buf failed!"<

記憶體中讀取

把資源先放到記憶體中,然後從記憶體中讀取資源。1.加到記憶體 plist檔案載入到記憶體,對應的也就被載入到記憶體了。cc.spriteframecache.getinstance addspriteframes plist 載入完後呼叫 可不調 cc.texturecache.getinstanc...

從XML中讀取資料到記憶體的例項

public clssimuresultbyonegoods getonegoodssimulationxml string pathandfilename break default break else if r.nodetype xmlnodetype.endelement break def...

從XML檔案中讀取資料

using system using system.data using system.configuration using system.web using system.web.security using system.web.ui using system.web.ui.webcontro...