1 / 28

RSA library

RSA library. 資訊 101 乙 F74976324 郭至軒. 將介紹 Chilkat VC++ Library 此 library 含有許多類別,其中包括 RSA 的 class. Download. http://www.chilkatsoft.com/ 選擇左處的 DOWNLOAD 接著往下找,找到符合你 IDE 的載點,點下去即可下載,這個 library 似乎只能在 windows 上使用 我作業系統 為 Win Vista ,使用 Visual Stdio 2008

Télécharger la présentation

RSA library

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. RSA library 資訊101乙 F74976324 郭至軒

  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++ 9.0 / Win32

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

  5. // 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. • 解壓縮裡有個LinkSample的資料夾,裡面有連結的範例 • 以下為他的內容

  6. #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. 宣告 • 首先就是宣告一個此類別的物件 CkRsarsa; • 所有操作皆在此類別內 他有提供許多類別函式可供使用 • 宣告一個即可 • 注意!!此類別無法賦值及複製

  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; CkStringm_resultString[10];

  14. public: void *getImpl(void) const; CkRsa(void *impl); CkRsa(); virtual ~CkRsa(); bool get_Utf8(void) const; void put_Utf8(bool b); boolUnlockComponent(const char *unlockCode); boolSaveLastError(const char *filename); void LastErrorXml(CkString &str); void LastErrorHtml(CkString &str); void LastErrorText(CkString &str); boolDecryptStringENC(const char *str, boolbUsePrivateKey, CkString &out); boolDecryptString(const CkByteData &bData, boolbUsePrivateKey, CkString &out); boolDecryptBytesENC(const char *str, boolbUsePrivateKey, CkByteData &out); boolDecryptBytes(const CkByteData &bData, boolbUsePrivateKey, CkByteData &out); boolEncryptStringENC(const char *str, boolbUsePrivateKey, CkString &out); boolEncryptBytesENC(const CkByteData &bData, boolbUsePrivateKey, CkString &out); boolEncryptString(const char *str, boolbUsePrivateKey, CkByteData &out); boolEncryptBytes(const CkByteData &bData, boolbUsePrivateKey, CkByteData &out);

  15. void get_EncodingMode(CkString &out); void put_EncodingMode(const char *str); void get_Charset(CkString &out); void put_Charset(const char *str); boolGenerateKey(intnumBits); boolExportPublicKey(CkString &strXml); boolExportPrivateKey(CkString &strXml); boolImportPublicKey(const char *strXml); boolImportPrivateKey(const char *strXml); boolSnkToXml(const char *filename, CkString &strXml); long get_NumBits(void); boolget_OaepPadding(void); void put_OaepPadding(boolnewVal); boolget_LittleEndian(void); void put_LittleEndian(boolnewVal); boolVerifyStringENC(const char *str, const char *hashAlg, const char *sig); boolVerifyString(const char *str, const char *hashAlg, const CkByteData &sigData); boolVerifyBytesENC(const CkByteData &bData, const char *hashAlg, const char *encodedSig); boolVerifyBytes(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. boolSignStringENC(const char *str, const char *hashAlg, CkString &out); boolSignBytesENC(const CkByteData &bData, const char *hashAlg, CkString &out); boolSignString(const char *str, const char *hashAlg, CkByteData &out); boolSignBytes(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, boolbUsePrivateKey); const char *decryptString(const CkByteData &bData, boolbUsePrivateKey); const char *encryptStringENC(const char *str, boolbUsePrivateKey); const char *encryptBytesENC(const CkByteData &bData, boolbUsePrivateKey); 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. boolOpenSslVerifyBytes(CkByteData &signature, CkByteData &outBytes); boolOpenSslSignBytes(CkByteData &data, CkByteData &outBytes); boolOpenSslSignBytesENC(CkByteData &data, CkString &outStr); const char *openSslSignBytesENC(CkByteData &data); boolOpenSslSignString(const char *str, CkByteData &outBytes); boolOpenSslSignStringENC(const char *str, CkString &outStr); const char *openSslSignStringENC(const char *str); boolOpenSslVerifyBytesENC(const char *str, CkByteData &outBytes); boolOpenSslVerifyString(CkByteData &data, CkString &outStr); const char *openSslVerifyString(CkByteData &data); boolOpenSslVerifyStringENC(const char *str, CkString &outStr); const char *openSslVerifyStringENC(const char *str); boolVerifyPrivateKey(const char *xml); boolVerifyHash(CkByteData &hashBytes, const char *hashAlg, CkByteData &sigBytes); boolVerifyHashENC(const char *encodedHash, const char *hashAlg, const char *encodedSig); boolSignHash(CkByteData &hashBytes, const char *hashAlg, CkByteData &outBytes); boolSignHashENC(const char *encodedHash, const char *hashAlg, CkString &outStr); const char *signHashENC(const char *encodedHash, const char *hashAlg); boolget_VerboseLogging(void); void put_VerboseLogging(boolnewVal); };

  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 • boolGenerateKey(intnumBits); 產生金鑰,同時產生public key 及 private key,回傳表成功與否,PS. 傳入值需在512-4096之間 • long get_NumBits(void); 回傳目前金鑰位元數 • const char *exportPublicKey(void); • const char *exportPrivateKey(void); 將存在物件裡的金鑰以一字串型態回傳( 上者為public key 下者為private key ) • boolImportPublicKey(const char *strXml); • boolImportPrivateKey(const char *strXml); 將現有金鑰存入物件內,回傳值表是否成功(上者為public key 下者為private key )

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

  21. boolget_OaepPadding(void); • 取得目前OAEP(Optimal Asymmetric Encryption Padding)是否使用,true為有使用,flase則無使用 • void put_OaepPadding(boolnewVal); 欲使用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之物件來儲存 • boolSaveLastError(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, boolbUsePrivateKey); 加密明文,第一個參數傳入明文,第二個參數為使用public key 還是 private key,true表使用private key,反之則使用public key,傳回一個密文的字串,此字串內容為xml檔案 • boolEncryptStringENC(const char *str, boolbUsePrivateKey, CkString &out); 同樣為加密,但回傳值表示是否加密成功,加密後密文將存於第三個參數CkString之物件內

  24. const char *decryptStringENC(const char *str, boolbUsePrivateKey); 解密密文,第一個參數傳入密文,第二個參數為使用public key 還是 private key,true表使用private key,反之則使用public key,傳回一個明文的字串,此字串內容為xml檔案 • boolDecryptStringENC(const char *str, boolbUsePrivateKey, 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. boolSnkToXml(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. 使用時,物件內需以匯入私密金鑰 • boolVerifyStringENC(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之功能,他許多類別都是緊緊相扣的

More Related