基於OpenSSL的一些常用加密簽名算法

目前包括:MD5、SHA512、DES、RSA加解密、RSA+MD5簽名驗證算法,在openssl基礎上再進行封裝,使用簡單,頭文件需要包含openssl庫,可以使用vcpkg自動管理,省去繁瑣的配置工程的過程。

該RSA簽名算法中,已將輸入明文做了MD5處理。

注意RSA加密算法E,解密算法D,與RSA簽名算法S,驗證算法V,這裏的EDSV互補相等,不要認爲加密過程的E使用公鑰,驗證過程的V也使用公鑰,就把兩者混爲一談。

 

一看頭文件就顯得容易使用了

class COpenSSL
{
public:
	COpenSSL();
	~COpenSSL();

	// ---- md5摘要哈希 ---- // 
	void md5(const std::string &srcStr, std::string &encodedHexStr);

	// ---- sha256摘要哈希 ---- //  
	void sha256(const std::string &srcStr, std::string &encodedHexStr);

	// ---- des對稱加解密 ---- //    
	// 加密 ecb模式    
	std::string des_encrypt(const std::string &clearText, const std::string &key);
	// 解密 ecb模式    
	std::string des_decrypt(const std::string &cipherText, const std::string &key);

	// 函數方法生成密鑰對   
	void generateRSAKey(std::string strKey[2]);

	// 命令行方法生成公私鑰對(begin public key/ begin private key)  
	// 找到openssl命令行工具,運行以下  
	// openssl genrsa -out prikey.pem 1024   
	// openssl rsa - in privkey.pem - pubout - out pubkey.pem  

	// 公鑰加密    
	std::string rsa_pub_encrypt(const std::string &clearText, const std::string &pubKey);
	// 私鑰解密    
	std::string rsa_pri_decrypt(const std::string &cipherText, const std::string &priKey);

	//私鑰簽名
	std::string signMessage(std::string privateKey, std::string plainText);
	//公鑰驗證
	bool verifySignature(std::string &publicKey, std::string &plainText, std::string &signatureBase64);

 

項目文件:https://gitee.com/feistel/some_openssl

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章