MD5withRSA簽名,驗籤,親測可以使用

2021-10-03 23:30:05 字數 2972 閱讀 3981

編譯命令:需要先安裝openssl

標頭檔案rsa.h:

#ifndef __rsa_h

#define __rsa_h

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

unsigned char *base64(const void input, size_t length,char result, size_t size);

unsigned char debase64(char input, size_t length, char result, size_t size);

unsigned char * getsign(char keyfile,char plaintext,unsigned char ciphertext,unsigned int ciphertextlen);

bool verifysign(char certfile,unsigned char ciphertext,unsigned int ciphertextlen,char plaintext);

#endif

原始碼:rsa.c

#include 「rsa.h」

/* 對二進位制資料進行base64編碼 /

/ input:陣列,length:陣列長度,result:儲存編碼後的資料,size:陣列大小 */

unsigned char *base64(const void *input, size_t length,char *result, size_t size)

memcpy(result, bptr->data, bptr->length-1);

result[bptr->length-1] = 0;

bio_free_all(b64);

return result;}

/* 對base64資料進行解碼 /

/ input:待解碼陣列,length:陣列長度,result:解碼後資料暫存區,暫存區大小 */

unsigned char *debase64(char *input, size_t length, char *result, size_t size)

bmem = bio_push(b64, bmem);

bio_read(bmem, result, length);

bio_free_all(b64);

return result;}

/* 生成簽名函式 /

/ 1:私鑰檔名,2:待簽名的資料,3:簽名後的資料儲存區,4:前面儲存區長度*/

unsigned char * getsign(char* keyfile,char* plaintext,unsigned char* ciphertext,unsigned int ciphertextlen)

/* do the signature */

evp_md_ctx md_ctx = evp_md_ctx_create();

evp_signinit (&md_ctx, evp_md5());

evp_signupdate (&md_ctx, plaintext, strlen(plaintext));

int err = evp_signfinal (&md_ctx, ciphertext, ciphertextlen, pkey);

if (err != 1)

//釋放記憶體操作

evp_md_ctx_cleanup(md_ctx);

evp_pkey_free(pkey);

return ciphertext;}

/* 驗證簽名函式 /

/ 1:公鑰檔名,2:簽名生成的資料,3:簽名生成的資料大小,4:驗籤結果*/

bool verifysign(char* certfile,unsigned char* ciphertext,unsigned int ciphertextlen,char* plaintext)

evp_md_ctx md_ctx;

evp_verifyinit (&md_ctx, evp_md5());

evp_verifyupdate (&md_ctx, plaintext, strlen((char)plaintext));

int err = evp_verifyfinal (&md_ctx, ciphertext, ciphertextlen, pkey);

evp_pkey_free (pkey);

if (err != 1) 

fclose(fp);

return true;

測試**test.c:

#include 「rsa.h」

int main()

if(verifysign(「pubkey.pem」,cipher,cipher_len,plaintext))

printf(「verify ok\n」);

else

printf(「verify fail\n」);

//printf(「get sign:%d\n%s\n」,cipher_len,cipher);

char *q = base64(cipher,cipher_len,base,sizeof(base));

printf(「get sign:%lu\n%s\n」,strlen(base),base);

return 0;
編譯說明:

gcc test.c rsa.c -lssl -lcrypto

C 實現MD5WITHRSA簽名

搞了好久才搞出來的.啥也不說了 直接上 using system using system.collections.generic using system.linq using system.text using system.security.cryptography.x509certifica...

銀聯簽名 md5

前幾天做手機銀聯pos支付,在伺服器端要對訂單進行簽名base64 rsa md5 訂單 base64跟md5在php中都有現成的函式。但恰是這個,悲劇呀。php的md5 函式 是可以設定 返回值的 string md5 string str bool raw output false 如果可選的 ...

api介面簽名驗證 MD5

你在寫開放的api介面時是如何保證資料的安全性的?先來看看有哪些安全性問題在開放的api介面中,我們通過http post或者get方式請求伺服器的時候,會面臨著許多的安全性問題,例如 請求 身份 是否合法?請求引數被篡改?請求的唯一性 不可複製 為了保證資料在通訊時的安全性,我們可以採用引數簽名的...