rsa library
DESCRIPTION
RSA library. 資訊 101 乙 F74976324 郭至軒. 將介紹 Chilkat VC++ Library 此 library 含有許多類別,其中包括 RSA 的 class. Download. http://www.chilkatsoft.com/ 選擇左處的 DOWNLOAD 接著往下找,找到符合你 IDE 的載點,點下去即可下載,這個 library 似乎只能在 windows 上使用 我作業系統 為 Win Vista ,使用 Visual Stdio 2008 - PowerPoint PPT PresentationTRANSCRIPT
RSA library資訊 101 乙
F74976324
郭至軒
將介紹 Chilkat VC++ Library此 l ibrary 含有許多類別,其中包括 RSA 的 class
Download
http://www.chilkatsoft.com/
選擇左處的 DOWNLOAD
接著往下找,找到符合你 IDE 的載點,點下去即可下載,這個 library 似乎只能在 windows 上使用
我作業系統為 Win Vista ,使用 Visual Stdio 2008
在這我們選擇 Chilkat C/C++ Libraries for VC++ 9.0 / Win32
使用預備工作 打開解壓縮檔 將裡面的 include, lib 這兩個資料夾解壓縮道專案的資料夾內
解壓縮裡有個 LinkSample 的資料夾,裡面有連結的範例 以下為他的內容
// LinkSample.cpp : Defines the entry point for the console application.//
#include <windows.h>#include <tchar.h>
// This example does nothing more than include a number of Chilkat headers// and instantiating objects for the purpose of demonstrating a clean build// and link (i.e. there are no unresolved externals).
// If you didn't ready the README.html that is included with the download,// you should probably spend a few minutes reviewing it. It tells what// Chilkat lib should be linked, depending on the "Runtime Library" setting// in your Project Properties --> C++ --> Code Generation settings.// It also tells what additional Microsoft libs need to be added to your// list of link libraries, such as ws2_32.lib, crypt32.lib, etc.
#include "../include/CkZip.h"
#include "../include/CkFtp2.h"
#include "../include/CkMailMan.h"
#include "../include/CkXml.h"
#include "../include/CkPrivateKey.h"
#include "../include/CkCompression.h“
………
…………
……………
連結 library
開啟 Visual Stdio 2008 並開啟一個新專案
工具列從專案進去選屬性 ( Alt + F7 )
打開組態屬性 -> 連結器 -> 輸入
其他相依性裡打上 ws2_32.lib crypt32.lib rpcrt4.lib dnsapi.lib
wininet.lib ../libs/ChilkatRelDll.lib
接下來於程式碼中#include “../include/CkRsa.h”
#include “../include/CkString.h”
#include <windows.h>
#include <tchar.h>
接著即可開始使用此 library 之功能
宣告
首先就是宣告一個此類別的物件CkRsa rsa;
所有操作皆在此類別內他有提供許多類別函式可供使用
宣告一個即可 注意 !! 此類別無法賦值及複製
RSA 類別定義
// CLASS: CkRsa
class CkRsa : public CkObject
{
private:
void *m_impl;
bool m_utf8;
CkRsa(const CkRsa &);
CkRsa &operator=(const CkRsa &);
unsigned long nextIdx(void);
unsigned long m_resultIdx;
CkString m_resultString[10];
public:
void *getImpl(void) const;
CkRsa(void *impl);
CkRsa();
virtual ~CkRsa();
bool get_Utf8(void) const;
void put_Utf8(bool b);
bool UnlockComponent(const char *unlockCode);
bool SaveLastError(const char *filename);
void LastErrorXml(CkString &str);
void LastErrorHtml(CkString &str);
void LastErrorText(CkString &str);
bool DecryptStringENC(const char *str, bool bUsePrivateKey, CkString &out);
bool DecryptString(const CkByteData &bData, bool bUsePrivateKey, CkString &out);
bool DecryptBytesENC(const char *str, bool bUsePrivateKey, CkByteData &out);
bool DecryptBytes(const CkByteData &bData, bool bUsePrivateKey, CkByteData &out);
bool EncryptStringENC(const char *str, bool bUsePrivateKey, CkString &out);
bool EncryptBytesENC(const CkByteData &bData, bool bUsePrivateKey, CkString &out);
bool EncryptString(const char *str, bool bUsePrivateKey, CkByteData &out);
bool EncryptBytes(const CkByteData &bData, bool bUsePrivateKey, CkByteData &out);
void get_EncodingMode(CkString &out);
void put_EncodingMode(const char *str);
void get_Charset(CkString &out);
void put_Charset(const char *str);
bool GenerateKey(int numBits);
bool ExportPublicKey(CkString &strXml);
bool ExportPrivateKey(CkString &strXml);
bool ImportPublicKey(const char *strXml);
bool ImportPrivateKey(const char *strXml);
bool SnkToXml(const char *filename, CkString &strXml);
long get_NumBits(void);
bool get_OaepPadding(void);
void put_OaepPadding(bool newVal);
bool get_LittleEndian(void);
void put_LittleEndian(bool newVal);
bool VerifyStringENC(const char *str, const char *hashAlg, const char *sig);
bool VerifyString(const char *str, const char *hashAlg, const CkByteData &sigData);
bool VerifyBytesENC(const CkByteData &bData, const char *hashAlg, const char *encodedSig);
bool VerifyBytes(const CkByteData &bData, const char *hashAlg, const CkByteData &sigData);
const char *signStringENC(const char *str, const char *hashAlg);
const char *signBytesENC(const CkByteData &bData, const char *hashAlg);
bool SignStringENC(const char *str, const char *hashAlg, CkString &out);
bool SignBytesENC(const CkByteData &bData, const char *hashAlg, CkString &out);
bool SignString(const char *str, const char *hashAlg, CkByteData &out);
bool SignBytes(const CkByteData &bData, const char *hashAlg, CkByteData &out);
const char *lastErrorText(void);
const char *lastErrorXml(void);
const char *lastErrorHtml(void);
const char *decryptStringENC(const char *str, bool bUsePrivateKey);
const char *decryptString(const CkByteData &bData, bool bUsePrivateKey);
const char *encryptStringENC(const char *str, bool bUsePrivateKey);
const char *encryptBytesENC(const CkByteData &bData, bool bUsePrivateKey);
const char *encodingMode(void);
const char *charset(void);
const char *exportPublicKey(void);
const char *exportPrivateKey(void);
const char *snkToXml(const char *filename);
void get_Version(CkString &str);
const char *version(void);
bool OpenSslVerifyBytes(CkByteData &signature, CkByteData &outBytes);
bool OpenSslSignBytes(CkByteData &data, CkByteData &outBytes);
bool OpenSslSignBytesENC(CkByteData &data, CkString &outStr);
const char *openSslSignBytesENC(CkByteData &data);
bool OpenSslSignString(const char *str, CkByteData &outBytes);
bool OpenSslSignStringENC(const char *str, CkString &outStr);
const char *openSslSignStringENC(const char *str);
bool OpenSslVerifyBytesENC(const char *str, CkByteData &outBytes);
bool OpenSslVerifyString(CkByteData &data, CkString &outStr);
const char *openSslVerifyString(CkByteData &data);
bool OpenSslVerifyStringENC(const char *str, CkString &outStr);
const char *openSslVerifyStringENC(const char *str);
bool VerifyPrivateKey(const char *xml);
bool VerifyHash(CkByteData &hashBytes, const char *hashAlg, CkByteData &sigBytes);
bool VerifyHashENC(const char *encodedHash, const char *hashAlg, const char *encodedSig);
bool SignHash(CkByteData &hashBytes, const char *hashAlg, CkByteData &outBytes);
bool SignHashENC(const char *encodedHash, const char *hashAlg, CkString &outStr);
const char *signHashENC(const char *encodedHash, const char *hashAlg);
bool get_VerboseLogging(void);
void put_VerboseLogging(bool newVal);
};
函式介紹
private 成員 bool m_utf8;
此值用以判斷輸入之字串以 UTF-8 編碼或是以 ANSI 編碼當其為 true ,所有輸入輸出之字串皆以 UTF-8 編碼,反之,則為 ANSI 編碼
CkRsa(const CkRsa &);
CkRsa &operator=(const CkRsa &);
此兩個成原函式放於 private 以至於此 class 無法進行賦值及複製
public 成員 bool get_Utf8(void) const;
查看目前字串編碼狀態 void put_Utf8(bool b);
設定字串編碼狀態, true 為 UTF-8 , false 為 ANSI
bool GenerateKey(int numBits);
產生金鑰,同時產生 public key 及 private key ,回傳表成功與否, PS. 傳入值需在 512-4096 之間
long get_NumBits(void);
回傳目前金鑰位元數 const char *exportPublicKey(void);
const char *exportPrivateKey(void);
將存在物件裡的金鑰以一字串型態回傳 ( 上者為 public key 下者為 private key )
bool ImportPublicKey(const char *strXml);
bool ImportPrivateKey(const char *strXml);
將現有金鑰存入物件內,回傳值表是否成功 ( 上者為 public key 下者為 private key )
範例
產生一對金鑰GenerateKey.cpp
GenerateKey.exe
public key.xml
private key.xml
匯入金鑰KeyImport.cpp
KeyImport.exe
bool get_OaepPadding(void);
取得目前 OAEP( Optimal Asymmetric Encryption Padding ) 是否使用, true 為有使用, flase 則無使用
void put_OaepPadding(bool newVal);
欲使用 OAEP 則傳入 true ,否則傳入 false
const char *lastErrorText(void);
const char *lastErrorXml(void);
const char *lastErrorHtml(void);
以上皆為回傳最後訊息,僅儲存格式不同 void LastErrorXml(CkString &str);
void LastErrorHtml(CkString &str);
void LastErrorText(CkString &str);
同樣為記錄最後訊息,但需傳入一 CkString 之物件來儲存 bool SaveLastError(const char *filename);
將錯誤訊息做記錄,參數為一檔案名稱之字串,回傳值 true 表示存檔成功,false 則表失敗
範例
訊息記錄LastError.cpp
LastError.exe
log.txt
void put_EncodingMode(const char *str);
此函式用來決定密文以何種型式儲存,有“ hex”, “base64”, “url”, “quoted-printable” 四種模式
const char *encodingMode(void);
回傳目前以何種型式儲存密文 void get_EncodingMode(CkString &out);
傳入一 CkString 物件,以儲存目前密文以何種型式儲存 const char *encryptStringENC(const char *str, bool
bUsePrivateKey);
加密明文,第一個參數傳入明文,第二個參數為使用 public key 還是 private key , true 表使用 private key ,反之則使用public key ,傳回一個密文的字串,此字串內容為 xml 檔案
bool EncryptStringENC(const char *str, bool bUsePrivateKey, CkString &out);
同樣為加密,但回傳值表示是否加密成功,加密後密文將存於第三個參數 CkString 之物件內
const char *decryptStringENC(const char *str, bool bUsePrivateKey);
解密密文,第一個參數傳入密文,第二個參數為使用 public key 還是 private key , true 表使用 private key ,反之則使用public key ,傳回一個明文的字串,此字串內容為 xml 檔案
bool DecryptStringENC(const char *str, bool bUsePrivateKey, CkString &out);
同樣為解密,但回傳值表示是否解密成功,解密後明文將存於第三個參數 CkString 之物件內
void put_Charset(const char *str);
傳入一字串,字串內容為編碼方式 (EX.utf-8) ,則所有明文都將先轉換為 utf-8 之編碼,再做加密
const char *charset(void);
取得目前編碼方式,傳回一字串表目前編碼 void get_Charset(CkString &out);
同樣為取得編碼方式,但需傳入一 CkString 之物件來儲存內容
範例
加密與解密Encrypt_Decrypt.cpp
Encrypt_Decrypt.exe
bool SnkToXml(const char *filename, CkString &strXml);
將 .snk 檔轉為 xml 的模式並儲存於一 CkString 物件 const char *snkToXml(const char *filename);
功能同上一函式,但將轉換結果以字串傳回 const char *signStringENC(const char *str, const char
*hashAlg);
製作簽章,第一個參數為欲製作簽章之明文,第二個為使用之 hash演算法 (EX.sha-1,md5….) ,以字串型態回傳簽章, PS. 使用時,物件內需以匯入私密金鑰
bool VerifyStringENC(const char *str, const char *hashAlg, const char *sig);
驗證簽章,第一個參數為欲驗證之明文,第二個為使用之 hash 演算法 (EX.sha-1,md5….) ,第三個為已得到之簽章,回傳值表驗證是否正確, PS. 使用時物件內需已匯入公開金鑰
此 library 之許多函式還有位元版,使用方是皆大同小異,就不再贅述
範例
數位簽章Sign.cpp
Sign.exe
結論 由以上範例可得知, RSA 僅僅加密短短的明文,也會產生相
當龐大的密文,因此使用 RSA 加密整個明文相當耗時且耗空間
此 library 無法加密中文 此 library 不僅有 RSA 之 library ,還有許多其他
library ,且欲使用完整的 RSA 所有功能,也需熟悉其他library 之功能,他許多類別都是緊緊相扣的