Presentation is loading. Please wait.

Presentation is loading. Please wait.

RSA library 資訊101乙 F74976324 郭至軒.

Similar presentations


Presentation on theme: "RSA library 資訊101乙 F74976324 郭至軒."— Presentation transcript:

1 RSA library 資訊101乙 F 郭至軒

2 將介紹Chilkat VC++ Library 此library含有許多類別,其中包括RSA的class

3 Download http://www.chilkatsoft.com/ 選擇左處的DOWNLOAD
接著往下找,找到符合你IDE的載點,點下去即可下載,這個 library似乎只能在windows上使用 我作業系統為Win Vista,使用Visual Stdio 2008 在這我們選擇Chilkat C/C++ Libraries for VC / Win32

4 使用預備工作 打開解壓縮檔 將裡面的include, lib這兩個資料夾解壓縮道專案的資料夾內

5 解壓縮裡有個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.

6 #include ". /include/CkZip. h" #include ". /include/CkFtp2
#include "../include/CkZip.h" #include "../include/CkFtp2.h" #include "../include/CkMailMan.h" #include "../include/CkXml.h" #include "../include/CkPrivateKey.h" #include "../include/CkCompression.h“ ……… ………… ……………

7 連結library 開啟Visual Stdio 2008 並開啟一個新專案

8 工具列從專案進去選屬性( Alt + F7 )

9 打開組態屬性->連結器->輸入

10 其他相依性裡打上 ws2_32.lib crypt32.lib rpcrt4.lib dnsapi.lib wininet.lib ../libs/ChilkatRelDll.lib

11 接下來於程式碼中 #include “../include/CkRsa.h” #include “../include/CkString.h” #include <windows.h> #include <tchar.h> 接著即可開始使用此library之功能

12 宣告 首先就是宣告一個此類別的物件 CkRsa rsa; 所有操作皆在此類別內 他有提供許多類別函式可供使用 宣告一個即可
注意!!此類別無法賦值及複製

13 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];

14 public: void. getImpl(void) const; CkRsa(void
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);

15 void get_EncodingMode(CkString &out); void put_EncodingMode(const char
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);

16 bool SignStringENC(const char. str, const char
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);

17 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); };

18 函式介紹 private成員 bool m_utf8; 此值用以判斷輸入之字串以UTF-8編碼或是以ANSI編碼
當其為true,所有輸入輸出之字串皆以UTF-8編碼,反之,則 為ANSI編碼 CkRsa(const CkRsa &); CkRsa &operator=(const CkRsa &); 此兩個成原函式放於private以至於此class無法進行賦值及複製

19 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. 傳入值 需在 之間 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 )

20 範例 產生一對金鑰 GenerateKey.cpp GenerateKey.exe public key.xml
private key.xml 匯入金鑰 KeyImport.cpp KeyImport.exe

21 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則表失敗

22 範例 訊息記錄 LastError.cpp LastError.exe log.txt

23 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之物件內

24 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之物件來儲存內容

25 範例 加密與解密 Encrypt_Decrypt.cpp Encrypt_Decrypt.exe

26 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之許多函式還有位元版,使用方是皆大同小異,就不再 贅述

27 範例 數位簽章 Sign.cpp Sign.exe

28 結論 由以上範例可得知,RSA僅僅加密短短的明文,也會產生相當 龐大的密文,因此使用RSA加密整個明文相當耗時且耗空間
此library無法加密中文 此library不僅有RSA之library,還有許多其他library,且欲使 用完整的RSA所有功能,也需熟悉其他library之功能,他許多 類別都是緊緊相扣的


Download ppt "RSA library 資訊101乙 F74976324 郭至軒."

Similar presentations


Ads by Google