编写ATL工程实现ActiveX控件调用cryptoAPI接口(三)------------AES对称加密与解密

注:下面的代码中用了Map,Base64,log,Result等都为自定义类型,太长就不一一贴出.

[cpp]  view plain  copy
 print ?
  1. /* 
  2.  *  
  3.  *  
  4.  * 文件名称:EncryptAES.cpp 
  5.  * 摘    要: 
  6.  *      AES对称加密与AES对称解密,生成会话密钥 
  7.  * 当前版本:1.0 
  8.  * 作    者:周绍禹 
  9.  * 创建日期:2012年3月4日 
  10.  */  
  11. #include "StdAfx.h"  
  12. #include "EncryptAES.h"  
  13. #include "base64.h"  
  14. #include <comdef.h>  
  15. #include "generic.h"  
  16. #include "Map.h"  
  17.   
  18. /** 
  19. * 
  20. *初始化CSP 
  21. * 
  22. */  
  23. CEncryptAES::CEncryptAES():CSP_NAME(MS_ENH_RSA_AES_PROV)  
  24. {  
  25.     log = new Log("CEncryptAES");  
  26. }  
  27.   
  28. CEncryptAES::~CEncryptAES()  
  29. {  
  30.     if(log) delete log;  
  31. }  
  32.   
  33.   
  34.   
  35. //-----------------------------------------------------------  
  36. // 函数名称:  
  37. //     encrypt  
  38. // 参数:  
  39. //    - string text 待加密明文  
  40. //    - string pwd_base64   对称密钥base64码  
  41. // 返回:  
  42. //     Result*  
  43. // 说明:  
  44. //     AES对称加密  
  45. //-----------------------------------------------------------  
  46. Result* CEncryptAES::encrypt(string text, string pwd_base64)  
  47. {  
  48.   
  49.     HCRYPTPROV hCryptProv = NULL;  
  50.     HCRYPTKEY hKey = 0;  
  51.     HCRYPTHASH hHash = 0;  
  52.     int dwLength = 0;  
  53.   
  54.     if(!CryptAcquireContext(&hCryptProv,  
  55.         NULL,  
  56.         this->CSP_NAME,//CSP_NAME  
  57.         PROV_RSA_AES,  
  58.         CRYPT_VERIFYCONTEXT))  
  59.     {  
  60.         DWORD dwLastErr = GetLastError();  
  61.   
  62.         if(NTE_BAD_KEYSET == dwLastErr)   
  63.         {  
  64.             Result* result = new Result("EncryptAES.cpp",54,"密钥库不存在,或者访问被拒绝!","{}");  
  65.             return result;  
  66.         }  
  67.         else{  
  68.             if(!CryptAcquireContext(&hCryptProv,  
  69.                 NULL,  
  70.                 this->CSP_NAME,  
  71.                 PROV_RSA_AES,  
  72.                 CRYPT_NEWKEYSET))  
  73.             {  
  74.                 Map* map = new Map(1);  
  75.                 map->put("errcode",dwLastErr);  
  76.                 string errcode = map->toString();  
  77.                 delete map;  
  78.                 Result* result = new Result("EncryptAES.cpp",68,"密钥库已存在,创建密钥库失败!",errcode);  
  79.                 return result;  
  80.             }  
  81.         }  
  82.     }  
  83.   
  84.   
  85.     HCRYPTPROV_Holder holder(hCryptProv);  
  86.   
  87.     if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))  
  88.     {  
  89.         string errorcode = getErrorCode();  
  90.         Result* result = new Result("EncryptAES.cpp",87,"创建hash对象失败!",errorcode.length()==0?"{}":errorcode);  
  91.         if(hKey) CryptDestroyKey(hKey);  
  92.         if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  93.         return result;  
  94.     }  
  95.   
  96.     //hash密钥字符串  
  97.     BYTE *pPwd = Base64::base64_decode(pwd_base64,dwLength);  
  98.     if(!CryptHashData(hHash, pPwd, dwLength, 0))  
  99.     {  
  100.         string errorcode = getErrorCode();  
  101.         Result* result = new Result("EncryptAES.cpp",103,"对密钥做hash运算失败!",errorcode.length()==0?"{}":errorcode);  
  102.         if(pPwd) delete []pPwd;  
  103.         if(hHash) CryptDestroyHash(hHash);  
  104.         if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  105.         return result;  
  106.     }  
  107.   
  108.     // 基于hash的密钥获取会话密钥  
  109.     if(!CryptDeriveKey(hCryptProv, CALG_AES_128, hHash, CRYPT_EXPORTABLE, &hKey))  
  110.     {  
  111.         string errorcode = getErrorCode();  
  112.         Result* result = new Result("EncryptAES.cpp",111,"获取会话密钥失败!",errorcode.length()==0?"{}":errorcode);  
  113.         if(pPwd) delete []pPwd;  
  114.         if(hHash) CryptDestroyHash(hHash);  
  115.         if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  116.         return result;  
  117.     }  
  118.   
  119.     int len = text.length();  
  120.     BYTE *pData ;  
  121.     pData = new BYTE[len*4];  
  122.     for(int i = 0; i < len; ++i)  
  123.     {  
  124.         pData[i] = (byte)text[i];  
  125.     }  
  126.     pData[len] = '\0';  
  127.     DWORD dwLen = len;  
  128.   
  129.     if(!CryptEncrypt(hKey, NULL, true, 0, pData, &dwLen, len*4))  
  130.     {  
  131.         string errorcode = getErrorCode();  
  132.         Result* result = new Result("EncryptAES.cpp",130,"对称加密失败!",errorcode.length()==0?"{}":errorcode);  
  133.         if(pData) delete []pData;  
  134.         if(pPwd) delete []pPwd;  
  135.         if(hKey) CryptDestroyKey(hKey);  
  136.         if(hHash) CryptDestroyHash(hHash);  
  137.         if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  138.         return result;  
  139.     }  
  140.   
  141.     pData[dwLen]='\0';  
  142.     string tobeDeBase = Base64::base64_encode(pData,dwLen);  
  143.   
  144.     if(pData) delete []pData;  
  145.     if(pPwd) delete []pPwd;  
  146.     if(hKey) CryptDestroyKey(hKey);  
  147.     if(hHash) CryptDestroyHash(hHash);  
  148.     if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  149.   
  150.     Result* result = new Result(tobeDeBase);  
  151.     return result;  
  152. }  
  153.   
  154.   
  155.   
  156. //-----------------------------------------------------------  
  157. // 函数名称:  
  158. //     decrypt  
  159. // 参数:  
  160. //    - string encrypted    待解密密文  
  161. //    - string pwd_base64   对称密钥base64码  
  162. // 返回:  
  163. //     Result*  
  164. // 说明:  
  165. //     AES对称解密  
  166. //-----------------------------------------------------------  
  167. Result* CEncryptAES::decrypt(string encrypted, string pwd_base64)  
  168. {  
  169.     HCRYPTPROV hCryptProv = NULL;  
  170.     HCRYPTKEY hKey = 0;  
  171.     HCRYPTHASH hHash = 0;  
  172.     int dwLength = 0;  
  173.     BYTE *pData ;  
  174.     int len;  
  175.     pData =Base64::base64_decode(encrypted,len);  
  176.   
  177.     if(!CryptAcquireContext(&hCryptProv,  
  178.         NULL,  
  179.         CSP_NAME,  
  180.         PROV_RSA_AES,  
  181.         CRYPT_VERIFYCONTEXT))  
  182.     {  
  183.         DWORD dwLastErr = GetLastError();  
  184.   
  185.         if(NTE_BAD_KEYSET == dwLastErr)   
  186.         {  
  187.             if(pData) delete []pData;  
  188.             Result* result = new Result("EncryptAES.cpp",146,"密钥库不存在,或者访问被拒绝!","{}");  
  189.             return result;  
  190.         }  
  191.         else{  
  192.             if(!CryptAcquireContext(&hCryptProv,  
  193.                 NULL,  
  194.                 this->CSP_NAME,  
  195.                 PROV_RSA_AES,  
  196.                 CRYPT_NEWKEYSET))  
  197.             {  
  198.                 if(pData) delete []pData;  
  199.                 Map* map = new Map(1);  
  200.                 map->put("errcode",dwLastErr);  
  201.                 string errcode = map->toString();  
  202.                 delete map;  
  203.                 Result* result = new Result("EncryptAES.cpp",160,"密钥库已存在,创建密钥库失败!",errcode);  
  204.                 return result;  
  205.             }  
  206.         }  
  207.     }  
  208.   
  209.     HCRYPTPROV_Holder holder(hCryptProv);  
  210.   
  211.     if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))  
  212.     {  
  213.         string errorcode = getErrorCode();  
  214.         Result* result = new Result("EncryptAES.cpp",178,"创建hash对象失败!",errorcode.length()==0?"{}":errorcode);  
  215.         if(pData) delete []pData;  
  216.         if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  217.         return result;  
  218.     }  
  219.     BYTE *pPwd = Base64::base64_decode(pwd_base64,dwLength);  
  220.     if(!CryptHashData(hHash, pPwd, dwLength, 0))  
  221.     {  
  222.         string errorcode = getErrorCode();  
  223.         Result* result = new Result("EncryptAES.cpp",185,"HASH摘要失败!",errorcode.length()==0?"{}":errorcode);  
  224.         if(pData) delete []pData;  
  225.         if(pPwd) delete []pPwd;  
  226.         if(hHash) CryptDestroyHash(hHash);  
  227.         if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  228.         return result;  
  229.     }  
  230.   
  231.     if(!CryptDeriveKey(hCryptProv, CALG_AES_128, hHash, CRYPT_EXPORTABLE, &hKey))  
  232.     {  
  233.         string errorcode = getErrorCode();  
  234.         Result* result = new Result("EncryptAES.cpp",192,"获取密钥失败!",errorcode.length()==0?"{}":errorcode);  
  235.         if(pData) delete []pData;  
  236.         if(pPwd) delete []pPwd;  
  237.         if(hHash) CryptDestroyHash(hHash);  
  238.         if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  239.         return result;  
  240.     }  
  241.   
  242.   
  243.     DWORD dwLen = len;  
  244.   
  245.   
  246.     if(!CryptDecrypt(hKey, NULL, true, 0, pData, &dwLen))  
  247.     {  
  248.         string errorcode = getErrorCode();  
  249.         Result* result = new Result("EncryptAES.cpp",203,"解密失败!",errorcode.length()==0?"{}":errorcode);  
  250.         if(pData) delete []pData;  
  251.         if(pPwd) delete []pPwd;  
  252.         if(hKey) CryptDestroyKey(hKey);  
  253.         if(hHash) CryptDestroyHash(hHash);  
  254.         if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  255.         return result;  
  256.     }  
  257.   
  258.     char* pData_pchar = new char[dwLen + 1];  
  259.   
  260.     for(int i = 0 ; i < dwLen; i++)  
  261.     {  
  262.         pData_pchar[i] = pData[i];  
  263.     }  
  264.     pData_pchar[dwLen] = '\0';  
  265.   
  266.     string resultData = pData_pchar;  
  267.   
  268.     if(pData) delete []pData;  
  269.     if(pPwd) delete []pPwd;  
  270.     if(hKey) CryptDestroyKey(hKey);  
  271.     if(hHash) CryptDestroyHash(hHash);  
  272.     if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  273.     Result* result = new Result(resultData);  
  274.     return result;  
  275. }  
  276. //-----------------------------------------------------------  
  277. // 函数名称:  
  278. //     generateSymmetricKey  
  279. // 参数:  
  280. //    - 无参数  
  281. // 返回:  
  282. //     Result*  
  283. // 说明:  
  284. //     生成会话密钥  
  285. //-----------------------------------------------------------  
  286. Result* CEncryptAES::generateSymmetricKey()  
  287. {  
  288.     HCRYPTPROV   hCryptProv;  
  289.     if(!CryptAcquireContext(&hCryptProv,  
  290.         NULL,  
  291.         CSP_NAME,  
  292.         PROV_RSA_AES,  
  293.         CRYPT_VERIFYCONTEXT))  
  294.     {  
  295.         DWORD dwLastErr = GetLastError();  
  296.   
  297.         if(NTE_BAD_KEYSET == dwLastErr)   
  298.         {  
  299.             Result* result = new Result("EncryptAES.cpp",248,"密钥库不存在,或者访问被拒绝!","{}");  
  300.             return result;  
  301.         }  
  302.         else{  
  303.             if(!CryptAcquireContext(&hCryptProv,  
  304.                 NULL,  
  305.                 this->CSP_NAME,  
  306.                 PROV_RSA_AES,  
  307.                 CRYPT_NEWKEYSET))  
  308.             {  
  309.                 Map* map = new Map(1);  
  310.                 map->put("errcode",dwLastErr);  
  311.                 string errcode = map->toString();  
  312.                 delete map;  
  313.                 Result* result = new Result("EncryptAES.cpp",262,"密钥库已存在,创建密钥库失败!",errcode);  
  314.                 return result;  
  315.             }  
  316.         }  
  317.     }  
  318.     BYTE  pbData[9];  
  319.     if(!CryptGenRandom(  
  320.         hCryptProv,   
  321.         8,   
  322.         pbData))   
  323.     {  
  324.         string errorcode = getErrorCode();  
  325.         Result* result = new Result("EncryptAES.cpp",278,"生成随机数失败!",errorcode.length()==0?"{}":errorcode);  
  326.         return result;  
  327.     }  
  328.     string key_base64 = Base64::base64_encode(pbData,9);  
  329.   
  330.     if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  331.     return new Result(key_base64);  
  332. }  






------------------------------下面为刚接触的版本,上面为修改后的版本--------------------------

代码有待修改

3DES对称加密与解密

[cpp]  view plain  copy
 print ?
  1. #include "Base.h"  
  2. #include <comdef.h>  
  3. #include "generic.h"  
  4. #include <iostream>  
  5. using namespace std;  
  6. //3DES对称加密  
  7. STDMETHODIMP CEncrypt3DES::Encrypt(BSTR text, BSTR pwd, BSTR* encrypted)  
  8. {  
  9.     // TODO: Add your implementation code here  
  10.     HCRYPTPROV hCryptProv = NULL;  
  11.     HCRYPTKEY hKey = 0;  
  12.     HCRYPTHASH hHash = 0;  
  13.     DWORD dwLength = 0;  
  14.   
  15.     // Create a keyset(aka container), if the keyset already exists,  
  16.     // delete it and re-create it.  
  17.   
  18.     if(!CryptAcquireContext(&hCryptProv,  
  19.                 NULL,  
  20.                 TEST_CSP_NAME,  
  21.                 PROV_RSA_FULL,  
  22.                 0))  
  23.     {  
  24.         DWORD dwLastErr = GetLastError();  
  25.   
  26.         if(0x8009000F == dwLastErr) //  Object already exists.  
  27.         {  
  28.              CComBSTR bstr("error:密钥库已存在");  
  29.         *encrypted = bstr;  
  30.             return S_FALSE;  
  31.         }  
  32.         else{  
  33.             if(!CryptAcquireContext(&hCryptProv,  
  34.                         NULL,  
  35.                         TEST_CSP_NAME,  
  36.                         PROV_RSA_FULL,  
  37.                         CRYPT_NEWKEYSET))  
  38.             {  
  39.                  CComBSTR bstr("error:创建新密钥库");  
  40.                  *encrypted = bstr;  
  41.                 return S_FALSE;  
  42.             }  
  43.         }  
  44.     }  
  45.     else  
  46.     {  
  47.     }  
  48.   
  49.     // Hold the handle and release when this function return.  
  50.     HCRYPTPROV_Holder holder(hCryptProv);  
  51.   
  52.     // Create a hash object.  
  53.     if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))  
  54.     {  
  55.          CComBSTR bstr("error:创建hash对象失败");  
  56.         *encrypted = bstr;  
  57.                 return S_FALSE;  
  58.     }  
  59.     else  
  60.     {  
  61.     }  
  62.   
  63.     // Hash the password string.  
  64.     dwLength = SysStringLen(pwd);  
  65.     BYTE *pPwd ;  
  66.     pPwd = new BYTE[dwLength];  
  67.       for(int i = 0; i < dwLength; ++i)  
  68.       {  
  69.             pPwd[i] = (byte)pwd[i];  
  70.       }  
  71.     pPwd[dwLength] = '\0';  
  72.     if(!CryptHashData(hHash, pPwd, dwLength, 0))  
  73.     {  
  74.          CComBSTR bstr("error:散列失败");  
  75.         *encrypted = bstr;  
  76.                 return S_FALSE;  
  77.     }  
  78.     else  
  79.     {  
  80.     }  
  81.   
  82.     // Create a block cipher session key based on the hash of the password.  
  83.     if(!CryptDeriveKey(hCryptProv, CALG_3DES, hHash, CRYPT_EXPORTABLE, &hKey))  
  84.     {  
  85.          CComBSTR bstr("error:获取密钥失败");  
  86.         *encrypted = bstr;  
  87.                 return S_FALSE;  
  88.     }  
  89.     else  
  90.     {  
  91.     }  
  92.     //Encrypt Data with RC2 key:  
  93.     int len = SysStringLen(text);  
  94.     BYTE *pData ;  
  95.     pData = new BYTE[len*2];  
  96.       for(int i = 0; i < len; ++i)  
  97.       {  
  98.             pData[i] = (byte)text[i];  
  99.       }  
  100.     pData[len] = '\0';  
  101.     DWORD dwLen = lstrlen((LPCTSTR) pData);//60;  
  102.   
  103.     //encrypt:  
  104.     if(!CryptEncrypt(hKey, NO_HASH, LAST_DATA, 0, pData, &dwLen, len*2))  
  105.     {  
  106.         int a = GetLastError();  
  107.          CComBSTR bstr("error:加密失败");  
  108.         *encrypted = bstr;  
  109.         return S_FALSE;  
  110.     }  
  111.     else  
  112.     {  
  113.     }  
  114.     pData[dwLen]='\0';  
  115.     //cout<<base64_encode(pData,dwLen).c_str();  
  116.     int len0 = dwLen;  
  117.     string tobeDeBase = base64_encode(pData,dwLen);  
  118.     int len1 = tobeDeBase.length();  
  119.     string result = base64_decode(tobeDeBase);  
  120.     int len2 = result.length();  
  121.     CComBSTR bstr(base64_encode(pData,dwLen).c_str());  
  122.       
  123.     //if(pData) free(pData);  
  124. //      if(pPwd) free(pPwd);  
  125.     if(hKey) CryptDestroyKey(hKey);  
  126.     if(hHash) CryptDestroyHash(hHash);  
  127.     if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  128.     *encrypted = bstr;  
  129.     return S_OK;  
  130. }  
  131.   
  132.   
  133.   
  134. //3DES对称解密  
  135. STDMETHODIMP CEncrypt3DES::Decrypt(BSTR encrypted, BSTR pwd, BSTR* text)  
  136. {  
  137.     // TODO: Add your implementation code here  
  138. // TODO: Add your implementation code here  
  139.     HCRYPTPROV hCryptProv = NULL;  
  140.     HCRYPTKEY hKey = 0;  
  141.     HCRYPTHASH hHash = 0;  
  142.     DWORD dwLength = 0;  
  143.     string temp = _bstr_t (encrypted);  
  144.     int len1 = temp.size();  
  145.     string toBeDec =base64_decode(temp);  
  146.     int len2 = toBeDec.length();  
  147.     // Create a keyset(aka container), if the keyset already exists,  
  148.     // delete it and re-create it.  
  149.   
  150.     if(!CryptAcquireContext(&hCryptProv,  
  151.                 NULL,  
  152.                 TEST_CSP_NAME,  
  153.                 PROV_RSA_FULL,  
  154.                 0))  
  155.     {  
  156.         DWORD dwLastErr = GetLastError();  
  157.   
  158.         if(0x8009000F == dwLastErr) //  Object already exists.  
  159.         {  
  160.              CComBSTR bstr("error:密钥库已存在");  
  161.         *text = bstr;  
  162.             return S_FALSE;  
  163.         }  
  164.         else{  
  165.             if(!CryptAcquireContext(&hCryptProv,  
  166.                         NULL,  
  167.                         TEST_CSP_NAME,  
  168.                         PROV_RSA_FULL,  
  169.                         CRYPT_NEWKEYSET))  
  170.             {  
  171.                  CComBSTR bstr("error:创建新密钥库");  
  172.                  *text = bstr;  
  173.                 return S_FALSE;  
  174.             }  
  175.         }  
  176.     }  
  177.     else  
  178.     {  
  179.     }  
  180.   
  181.     // Hold the handle and release when this function return.  
  182.     HCRYPTPROV_Holder holder(hCryptProv);  
  183.   
  184.     // Create a hash object.  
  185.     if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &hHash))  
  186.     {  
  187.          CComBSTR bstr("error:创建hash对象失败");  
  188.         *text = bstr;  
  189.                 return S_FALSE;  
  190.     }  
  191.     else  
  192.     {  
  193.     }  
  194.   
  195.     // Hash the password string.  
  196.     dwLength = SysStringLen(pwd);  
  197.     BYTE *pPwd ;  
  198.     pPwd = new BYTE[dwLength];  
  199.       for(int i = 0; i < dwLength; ++i)  
  200.       {  
  201.             pPwd[i] = (byte)pwd[i];  
  202.       }  
  203.     pPwd[dwLength] = '\0';  
  204.     if(!CryptHashData(hHash, pPwd, dwLength, 0))  
  205.     {  
  206.          CComBSTR bstr("error:散列失败");  
  207.         *text = bstr;  
  208.                 return S_FALSE;  
  209.     }  
  210.     else  
  211.     {  
  212.     }  
  213.   
  214.     // Create a block cipher session key based on the hash of the password.  
  215.     if(!CryptDeriveKey(hCryptProv, CALG_3DES, hHash, CRYPT_EXPORTABLE, &hKey))  
  216.     {  
  217.         int i = GetLastError();  
  218.          CComBSTR bstr("error:获取密钥失败:"+i);  
  219.         *text = bstr;  
  220.                 return S_FALSE;  
  221.     }  
  222.     else  
  223.     {  
  224.     }  
  225.     //Encrypt Data with RC2 key:  
  226.     int len = toBeDec.size();  
  227.      BYTE *pData ;  
  228.      pData = new BYTE[len] ;  
  229.       for(int i = 0; i < len; ++i)  
  230.       {  
  231.             pData[i] = (byte)toBeDec[i];  
  232.       }  
  233.     pData[len] = '\0';  
  234.     DWORD dwLen = len;//60;  
  235.   
  236.     //encrypt:  
  237.     if(!CryptDecrypt(hKey, NO_HASH, LAST_DATA, 0, pData, &dwLen))  
  238.     {  
  239.         int err = GetLastError();  
  240.          CComBSTR bstr("error:解密失败:"+err);  
  241.         *text = bstr;  
  242.         return S_FALSE;  
  243.     }  
  244.     else  
  245.     {  
  246.     }  
  247.     pData[dwLen]='\0';  
  248.     char* resultData = (char*)pData;  
  249.     CComBSTR plainText(resultData);  
  250.     *text = plainText;  
  251.     //if(resultData) free(resultData);  
  252.     //if(pData) free(pData);  
  253.     //if(pPwd) free(pPwd);  
  254.     if(hKey) CryptDestroyKey(hKey);  
  255.     if(hHash) CryptDestroyHash(hHash);  
  256.     if(hCryptProv) CryptReleaseContext(hCryptProv, 0);  
  257.     return S_OK;  
  258. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
(*****************************************************)(* *)(* Advanced Encryption Standard (AES) *)(* Interface Unit v1.3 *)(* *)(* Readme.txt 自述文档 2004.12.04 *)(* *)(*****************************************************)(* 介绍 *)AES 是一种使用安全码进行信息加密的标准。它支持 128 位、192 位和 256 位的密匙。加密算法的实现在 ElAES.pas 单元中。本人将其加密方法封装在 AES.pas 单元中,只需要调用两个标准函数就可以完成字符串的加密和解密。(* 密匙长度 *)128 位支持长度为 16 个字符192 位支持长度为 24 个字符256 位支持长度为 32 个字符所有加密和解密操作在默认情况下为 128 位密匙。(* 文件列表 *)..Source AES 单元文件..Example 演示程序(* 适用平台 *)这份 Delphi 的执行基于 FIPS 草案标准,并且 AES 原作者已经通过了以下平台的测试: Delphi 4 Delphi 5 C++ Builder 5 Kylix 1本人又重新进行了补充测试,并顺利通过了以下平台: Delphi 6 Delphi 7特别说明: 在 Delphi 3 标准版中进行测试时,因为缺少 Longword 数据类型和 Math.pas 文件,并且不支持 overload 指示字,所以不能正常编译。(* 演示程序 *)这个示例程序演示了如何使用 AES 模块进行字符串的加密和解密过程。(* 使用方法 *)在程序中引用 AES 单元。调用函数 EncryptString 和 DecryptString 进行字符串的加密和解密调用函数 EncryptStream 和 DecryptStream 进行流的加密和解密调用过程 EncryptFile 和 DecryptFile 进行文件的加密和解密。详细参阅 Example 文件夹中的例子。(* 许可协议 *)您可以随意拷贝、使用和发部这个程序,但是必须保证程序的完整性,包括作者信息、版权信息和说明文档。请勿修改作者和版权信息。 这个程序基于 Mozilla Public License Version 1.1 许可,如果您使用了这个程序,那么就意味着您同意了许可协议中的所有内容。您可以在以下站点获取一个许可协议的副本。 http://www.mozilla.org/MPL/许可协议的发布基于 "AS IS" 基础,详细请阅读该许可协议。Alexander Ionov 是 AES 算法的最初作者,保留所有权利。(* 作者信息 *)ElAES 作者:EldoS, Alexander IonovAES Interface Unit 作者:杨泽晖 (Jorlen Young)您可以通过以下方式与我取得联系。WebSite: http://jorlen.51.net/ http://mycampus.03.com.cn/ http://mycampus.1155.net/ http://mycampus.ecoo.net/ http://mycampus.5500.org/Email: stanley_xfx@163.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值