调用cryptlib接口3DES对称加密、信封加密、数字签名等



[cpp]  view plain  copy
 print ?
  1. #include<iostream>  
  2. #include <string>  
  3. #include "Base.h" //提供base64转码功能  
  4. using namespace std;  
  5. #include "cryptlib.h"  
  6.   
  7. int main()  
  8. {  
  9.     cryptInit();  
  10.         //cryptlib调用前必须执行init,调用完必须执行end  
  11.     cryptEnd();  
  12.   
  13.     return 0;  
  14. }  
  15. /** 
  16. * 
  17. *cryptlib产生自签名证书 
  18. * 
  19. **/  
  20. void GenerateKey(){  
  21.     int* keyset;  
  22.     keyset = (int*)malloc(sizeof(int*)); //密钥库上下文  
  23.     int* cryptContext;  
  24.     cryptContext = (int*)malloc(sizeof(int*)); //加密上下文  
  25.     int* cryptCertificate;  
  26.     cryptCertificate = (int*)malloc(sizeof(int*)); //证书上下文  
  27.     int status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, "D:\\Private key file.p15", CRYPT_KEYOPT_CREATE);//创建密钥库,(CRYPT_KEYOPT_CREATE)  
  28.     status = cryptCreateContext(cryptContext,CRYPT_UNUSED, CRYPT_ALGO_RSA);//RSA算法的密钥上下文  
  29.     status = cryptSetAttributeString(*cryptContext, CRYPT_CTXINFO_LABEL, "test1",5);//密钥标签  
  30.     status = cryptGenerateKey(*cryptContext);  
  31.     status = cryptAddPrivateKey(*keyset, *cryptContext, "1234");//向密钥库添加私钥  
  32.     status = cryptCreateCert(cryptCertificate,CRYPT_UNUSED, CRYPT_CERTTYPE_CERTIFICATE);//创建证书  
  33.     status = cryptSetAttribute(*cryptCertificate, CRYPT_CERTINFO_XYZZY, 1);//设置为简单证书  
  34.     /* Add the public key and certificate owner name and sign the  
  35.     certificate with the private key */  
  36.     status = cryptSetAttribute(*cryptCertificate, CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO, *cryptContext);//证书公钥绑定  
  37.     status = cryptSetAttributeString(*cryptCertificate, CRYPT_CERTINFO_COMMONNAME, "test1",5);//证书CN  
  38.     status = cryptSignCert(*cryptCertificate, *cryptContext);//使用私钥自签发证书  
  39.     status =  cryptAddPublicKey(*keyset, *cryptCertificate);//向证书添加公钥  
  40.     status = cryptDestroyCert(*cryptCertificate);  
  41.     status = cryptDestroyContext(*cryptContext);  
  42.     status = cryptKeysetClose(*keyset);  
  43. }  
  44. /** 
  45. * 
  46. *从指定密钥库中获取私钥,并签名与验证 
  47. * 
  48. **/  
  49. void signature(){  
  50.     /*访问密钥库,获取私钥*/  
  51.     int* keyset;  
  52.     keyset = (int*)malloc(sizeof(int*));  
  53.     int* sigKeyContext;  
  54.     sigKeyContext = (int*)malloc(sizeof(int*));  
  55.     int status = 0;  
  56.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, "D:\\Private key file.p15", CRYPT_KEYOPT_READONLY);//读取密钥库,(CRYPT_KEYOPT_READONLY)  
  57.     status = cryptGetPrivateKey(*keyset,sigKeyContext, CRYPT_KEYID_NAME, "test1""1234");//通过标签和保护密钥获取私钥  
  58.     CRYPT_CONTEXT  hashContext;  
  59.   
  60.     void *signature;  
  61.   
  62.     int signatureLength;  
  63.   
  64.       
  65.   
  66.     status = cryptCreateContext( &hashContext, CRYPT_UNUSED, CRYPT_ALGO_MD5 );//创建hash上下文  
  67.   
  68.     /* 对数据做摘要 */  
  69.   
  70.     status = cryptEncrypt( hashContext, "1234", 4 );  
  71.   
  72.     status = cryptEncrypt( hashContext, "1234", 0 );  
  73.   
  74.     /* 为签名值分配空间 */  
  75.     int signatureMaxLength;  
  76.     cryptCreateSignature( NULL, 0, &signatureMaxLength, *sigKeyContext, hashContext );  
  77.   
  78.     signature = malloc( signatureMaxLength );  
  79.   
  80.     /*使用私钥对摘要进行签名*/  
  81.     status = cryptCreateSignature( signature, signatureMaxLength, &signatureLength, *sigKeyContext, hashContext );  
  82.   
  83.   
  84.     status = cryptDestroyContext( hashContext );  
  85.   
  86.   
  87.   
  88.   
  89.   
  90.     /* 创建hash上下文 */  
  91.   
  92.     status = cryptCreateContext( &hashContext, CRYPT_UNUSED, CRYPT_ALGO_MD5 );  
  93.   
  94.     /* 对数据做摘要 */  
  95.   
  96.     status = cryptEncrypt( hashContext, "1234", 4 );  
  97.   
  98.     status = cryptEncrypt( hashContext, "1234", 0 );  
  99.   
  100.     /* 使用公钥验证签名 */  
  101.   
  102.     status = cryptCheckSignature( signature, signatureLength, *sigKeyContext, hashContext );  
  103.   
  104.     status = cryptDestroyContext( hashContext );  
  105.   
  106.   
  107. }  
  108. /** 
  109. * 
  110. *导出指定密钥库中的公钥 
  111. * 
  112. **/  
  113. void ExportKey(){  
  114.     /*访问密钥库,获取公钥*/  
  115.     int* keyset;  
  116.     keyset = (int*)malloc(sizeof(int*));  
  117.     int* sigKeyContext;  
  118.     sigKeyContext = (int*)malloc(sizeof(int*));  
  119.     int status = 0;  
  120.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, "D:\\Private key file.p15", CRYPT_KEYOPT_READONLY);  
  121.     status = cryptGetPublicKey(*keyset,sigKeyContext, CRYPT_KEYID_NAME, "test1");//通过标签获取公钥  
  122.     CRYPT_CONTEXT pubKeyContext, cryptContext;  
  123.   
  124.     void *encryptedKey;  
  125.   
  126.     int encryptedKeyLength,encryptedKeyMaxLength;  
  127.     /* 生成保护密钥 */  
  128.   
  129.     status = cryptCreateContext( &cryptContext, CRYPT_UNUSED, CRYPT_ALGO_3DES );  
  130.   
  131.     status = cryptGenerateKey( cryptContext );  
  132.     status = cryptExportKey( NULL, 0, &encryptedKeyMaxLength, *sigKeyContext, cryptContext ); //获取公钥大小  
  133.   
  134.     encryptedKey = malloc( encryptedKeyMaxLength );  
  135.   
  136.     status = cryptExportKey( encryptedKey, encryptedKeyMaxLength, &encryptedKeyLength, *sigKeyContext, cryptContext );//导出公钥  
  137.   
  138.     cout<<(char*) encryptedKey<<endl;  
  139.   
  140.   
  141.   
  142. }  
  143. /** 
  144. * 
  145. *3DES对称加密 
  146. * 
  147. **/  
  148. void encrypt(string text,string pwd,string iv,BYTE* result,int* length){  
  149.     CRYPT_CONTEXT context;  
  150.     int status = cryptCreateContext(&context,CRYPT_UNUSED,CRYPT_ALGO_3DES);  
  151.     status  = cryptSetAttributeString(context,CRYPT_CTXINFO_IV,iv.c_str(),iv.size()); //初始向量  
  152.   
  153.     status = cryptSetAttributeString(context,CRYPT_CTXINFO_KEY,pwd.c_str(),pwd.size());//密钥  
  154.   
  155.     int keysize;  
  156.     status = cryptGetAttribute(context,CRYPT_CTXINFO_KEYSIZE,&keysize);  
  157.     if (text.size() % keysize > 0){  
  158.         *length = text.size() + keysize - text.size() % keysize;  
  159.           
  160.     }  
  161.     else{  
  162.         *length = text.size();  
  163.           
  164.     }  
  165.     for(int i =0 ;i<text.size();i++){  
  166.         result[i] = text[i];  //存储到BYTE数组中  
  167.     }  
  168.     status = cryptEncrypt(context,result,*length);//加密处理  
  169.     for(int i = 0 ; i < *length ; i++)  
  170.     {  
  171.         printf("%x ",result[i]);  
  172.     }  
  173.     status = cryptDestroyContext(context);  
  174.     if(status!=0) cout<<"fail"<<endl;  
  175. }  
  176. void decrypt(BYTE* enc , int* length,string pwd,string iv){  
  177.     CRYPT_CONTEXT context;  
  178.     int status = cryptCreateContext(&context,CRYPT_UNUSED,CRYPT_ALGO_3DES);  
  179.     status  = cryptSetAttributeString(context,CRYPT_CTXINFO_IV,iv.c_str(),iv.size());  
  180.     status = cryptSetAttributeString(context,CRYPT_CTXINFO_KEY,pwd.c_str(),pwd.size());  
  181.     int keysize;  
  182.     status = cryptGetAttribute(context,CRYPT_CTXINFO_KEYSIZE,&keysize);  
  183.     status  = cryptDecrypt(context,enc,*length);  
  184.     status  = cryptDestroyContext(context);  
  185. }  
  186. /** 
  187. *   公钥加密信封,返回base64编码字符串 
  188. */  
  189. string envelop(string text,string keyFile){  
  190.     BYTE* message;  
  191.     message = new BYTE[text.size()];  
  192.     for(int i=0;i<text.size();i++){  
  193.         message[i]=text[i];  
  194.     }  
  195.     int messageLength = text.size();  
  196.     /*访问密钥库,获取公钥*/  
  197.     int* keyset;  
  198.     keyset = (int*)malloc(sizeof(int*));  
  199.     int* pubKeyContext;  
  200.     pubKeyContext = (int*)malloc(sizeof(int*));  
  201.     int status = 0;  
  202.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, keyFile.c_str(), CRYPT_KEYOPT_READONLY);  
  203.     status = cryptGetPublicKey(*keyset,pubKeyContext, CRYPT_KEYID_NAME, "test1");  
  204.   
  205.     CRYPT_ENVELOPE cryptEnvelope;  
  206.   
  207.     int bytesCopied;  
  208.   
  209.     status = cryptCreateEnvelope( &cryptEnvelope, CRYPT_UNUSED, CRYPT_FORMAT_CRYPTLIB );  
  210.   
  211.     /* Add the public key */  
  212.   
  213.     status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_PUBLICKEY, *pubKeyContext );  
  214.   
  215.     /* Add the data size information and data, wrap up the processing, and pop out the processed data */  
  216.   
  217.     status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_DATASIZE, messageLength );  
  218.   
  219.     status = cryptPushData( cryptEnvelope, message, messageLength, &bytesCopied );  
  220.   
  221.     status = cryptFlushData( cryptEnvelope );  
  222.     BYTE* envelopedData;  
  223.     envelopedData = new BYTE[1000];  
  224.     int envelopedDataBufferSize = 1000;  
  225.     status = cryptPopData( cryptEnvelope, envelopedData, envelopedDataBufferSize, &bytesCopied );  
  226.   
  227.     status = cryptDestroyEnvelope( cryptEnvelope );  
  228.     string base64Result = base64_encode(envelopedData,bytesCopied);  
  229.     cout<<base64Result<<endl;  
  230.     return base64Result;  
  231. }  
  232. string develop(string env,string keyFile){  
  233.     BYTE* message;  
  234.     string debase = base64_decode(env);  
  235.     message = new BYTE[debase.size()];  
  236.     for(int i=0;i<debase.size();i++){  
  237.         message[i] = debase[i];  
  238.     }  
  239.     int* privKeyContext;  
  240.     privKeyContext = (int*)malloc(sizeof(int*));  
  241.     int messageLength = debase.size();  
  242.     message[messageLength]='\0';  
  243.     CRYPT_ENVELOPE cryptEnvelope;  
  244.     int bytesCopied, status;  
  245.     /*访问密钥库*/  
  246.     int* keyset;  
  247.     keyset = (int*)malloc(sizeof(int*));  
  248.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE,keyFile.c_str(), CRYPT_KEYOPT_READONLY);  
  249.     status = cryptGetPrivateKey(*keyset,privKeyContext, CRYPT_KEYID_NAME, "test1""1234");  
  250.   
  251.     status = cryptCreateEnvelope( &cryptEnvelope, CRYPT_UNUSED, CRYPT_FORMAT_AUTO );  
  252.   
  253.     /* Push in the enveloped data and the private decryption key required to de-envelope it, and pop out the recovered message */  
  254.   
  255.     status = cryptPushData( cryptEnvelope, message, messageLength, &bytesCopied );  
  256.   
  257.     status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_PRIVATEKEY, *privKeyContext );  
  258.   
  259.     status = cryptFlushData( cryptEnvelope );  
  260.     BYTE* result;  
  261.     result = new BYTE[1000];  
  262.     int resultLength = 1000;  
  263.     status = cryptPopData( cryptEnvelope, result, resultLength, &bytesCopied );  
  264.   
  265.     status = cryptDestroyEnvelope( cryptEnvelope );  
  266.   
  267.   
  268.     cout<<(char*)result<<endl;  
  269.     string r  =  string((char*)result);  
  270.     return r;  
  271. }  
  272. string exportCert(string keyFile){  
  273.     /*访问密钥库,获取公钥*/  
  274.     int* keyset;  
  275.     keyset = (int*)malloc(sizeof(int*));  
  276.     int* certContext;  
  277.     certContext = (int*)malloc(sizeof(int*));  
  278.     int status = 0;  
  279.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, keyFile.c_str(), CRYPT_KEYOPT_READONLY);  
  280.     status = cryptGetPublicKey(*keyset,certContext, CRYPT_KEYID_NAME, "test1");  
  281.   
  282.     void *certificate;  
  283.   
  284.     int certLength;  
  285.     int certMaxLength;  
  286.     status = cryptExportCert( NULL, 0, &certMaxLength,CRYPT_CERTFORMAT_CERTIFICATE  
  287.         , *certContext );  
  288.   
  289.     /* Allocate memory for the encoded certificate */  
  290.   
  291.     certificate = malloc( certMaxLength );  
  292.   
  293.     /* Export the encoded certificate from the certificate object */  
  294.   
  295.     status = cryptExportCert( certificate, certMaxLength, &certLength, CRYPT_CERTFORMAT_CERTIFICATE  
  296.         , *certContext );  
  297.     unsigned char* certChar = (unsigned char*)certificate;  
  298.     certChar[certLength]='\0';  
  299.     string cert_base64 = base64_encode(certChar,certLength);  
  300.     return cert_base64;  
  301.   
  302. }  
  303. void importCert(string cert_base64){  
  304.     CRYPT_CERTIFICATE cryptCertificate;  
  305.     int status;  
  306.     string certStr = base64_decode(cert_base64);  
  307.     /* Import the certificate object from the encoded certificate */  
  308.     BYTE* cert;  
  309.     int certLength = certStr.size();  
  310.     cert = new BYTE[certLength];  
  311.     for(int i = 0 ; i<certLength;i++){  
  312.         cert[i] = certStr[i];  
  313.     }  
  314.     cert[certLength]='\0';  
  315.     status = cryptImportCert( cert, certLength, CRYPT_UNUSED, &cryptCertificate );  
  316.   
  317.   
  318. }  
[cpp]  view plain  copy
 print ?
  1. #include<iostream>  
  2. #include <string>  
  3. #include "Base.h" //提供base64转码功能  
  4. using namespace std;  
  5. #include "cryptlib.h"  
  6.   
  7. int main()  
  8. {  
  9.     cryptInit();  
  10.         //cryptlib调用前必须执行init,调用完必须执行end  
  11.     cryptEnd();  
  12.   
  13.     return 0;  
  14. }  
  15. /** 
  16. * 
  17. *cryptlib产生自签名证书 
  18. * 
  19. **/  
  20. void GenerateKey(){  
  21.     int* keyset;  
  22.     keyset = (int*)malloc(sizeof(int*)); //密钥库上下文  
  23.     int* cryptContext;  
  24.     cryptContext = (int*)malloc(sizeof(int*)); //加密上下文  
  25.     int* cryptCertificate;  
  26.     cryptCertificate = (int*)malloc(sizeof(int*)); //证书上下文  
  27.     int status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, "D:\\Private key file.p15", CRYPT_KEYOPT_CREATE);//创建密钥库,(CRYPT_KEYOPT_CREATE)  
  28.     status = cryptCreateContext(cryptContext,CRYPT_UNUSED, CRYPT_ALGO_RSA);//RSA算法的密钥上下文  
  29.     status = cryptSetAttributeString(*cryptContext, CRYPT_CTXINFO_LABEL, "test1",5);//密钥标签  
  30.     status = cryptGenerateKey(*cryptContext);  
  31.     status = cryptAddPrivateKey(*keyset, *cryptContext, "1234");//向密钥库添加私钥  
  32.     status = cryptCreateCert(cryptCertificate,CRYPT_UNUSED, CRYPT_CERTTYPE_CERTIFICATE);//创建证书  
  33.     status = cryptSetAttribute(*cryptCertificate, CRYPT_CERTINFO_XYZZY, 1);//设置为简单证书  
  34.     /* Add the public key and certificate owner name and sign the  
  35.     certificate with the private key */  
  36.     status = cryptSetAttribute(*cryptCertificate, CRYPT_CERTINFO_SUBJECTPUBLICKEYINFO, *cryptContext);//证书公钥绑定  
  37.     status = cryptSetAttributeString(*cryptCertificate, CRYPT_CERTINFO_COMMONNAME, "test1",5);//证书CN  
  38.     status = cryptSignCert(*cryptCertificate, *cryptContext);//使用私钥自签发证书  
  39.     status =  cryptAddPublicKey(*keyset, *cryptCertificate);//向证书添加公钥  
  40.     status = cryptDestroyCert(*cryptCertificate);  
  41.     status = cryptDestroyContext(*cryptContext);  
  42.     status = cryptKeysetClose(*keyset);  
  43. }  
  44. /** 
  45. * 
  46. *从指定密钥库中获取私钥,并签名与验证 
  47. * 
  48. **/  
  49. void signature(){  
  50.     /*访问密钥库,获取私钥*/  
  51.     int* keyset;  
  52.     keyset = (int*)malloc(sizeof(int*));  
  53.     int* sigKeyContext;  
  54.     sigKeyContext = (int*)malloc(sizeof(int*));  
  55.     int status = 0;  
  56.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, "D:\\Private key file.p15", CRYPT_KEYOPT_READONLY);//读取密钥库,(CRYPT_KEYOPT_READONLY)  
  57.     status = cryptGetPrivateKey(*keyset,sigKeyContext, CRYPT_KEYID_NAME, "test1""1234");//通过标签和保护密钥获取私钥  
  58.     CRYPT_CONTEXT  hashContext;  
  59.   
  60.     void *signature;  
  61.   
  62.     int signatureLength;  
  63.   
  64.       
  65.   
  66.     status = cryptCreateContext( &hashContext, CRYPT_UNUSED, CRYPT_ALGO_MD5 );//创建hash上下文  
  67.   
  68.     /* 对数据做摘要 */  
  69.   
  70.     status = cryptEncrypt( hashContext, "1234", 4 );  
  71.   
  72.     status = cryptEncrypt( hashContext, "1234", 0 );  
  73.   
  74.     /* 为签名值分配空间 */  
  75.     int signatureMaxLength;  
  76.     cryptCreateSignature( NULL, 0, &signatureMaxLength, *sigKeyContext, hashContext );  
  77.   
  78.     signature = malloc( signatureMaxLength );  
  79.   
  80.     /*使用私钥对摘要进行签名*/  
  81.     status = cryptCreateSignature( signature, signatureMaxLength, &signatureLength, *sigKeyContext, hashContext );  
  82.   
  83.   
  84.     status = cryptDestroyContext( hashContext );  
  85.   
  86.   
  87.   
  88.   
  89.   
  90.     /* 创建hash上下文 */  
  91.   
  92.     status = cryptCreateContext( &hashContext, CRYPT_UNUSED, CRYPT_ALGO_MD5 );  
  93.   
  94.     /* 对数据做摘要 */  
  95.   
  96.     status = cryptEncrypt( hashContext, "1234", 4 );  
  97.   
  98.     status = cryptEncrypt( hashContext, "1234", 0 );  
  99.   
  100.     /* 使用公钥验证签名 */  
  101.   
  102.     status = cryptCheckSignature( signature, signatureLength, *sigKeyContext, hashContext );  
  103.   
  104.     status = cryptDestroyContext( hashContext );  
  105.   
  106.   
  107. }  
  108. /** 
  109. * 
  110. *导出指定密钥库中的公钥 
  111. * 
  112. **/  
  113. void ExportKey(){  
  114.     /*访问密钥库,获取公钥*/  
  115.     int* keyset;  
  116.     keyset = (int*)malloc(sizeof(int*));  
  117.     int* sigKeyContext;  
  118.     sigKeyContext = (int*)malloc(sizeof(int*));  
  119.     int status = 0;  
  120.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, "D:\\Private key file.p15", CRYPT_KEYOPT_READONLY);  
  121.     status = cryptGetPublicKey(*keyset,sigKeyContext, CRYPT_KEYID_NAME, "test1");//通过标签获取公钥  
  122.     CRYPT_CONTEXT pubKeyContext, cryptContext;  
  123.   
  124.     void *encryptedKey;  
  125.   
  126.     int encryptedKeyLength,encryptedKeyMaxLength;  
  127.     /* 生成保护密钥 */  
  128.   
  129.     status = cryptCreateContext( &cryptContext, CRYPT_UNUSED, CRYPT_ALGO_3DES );  
  130.   
  131.     status = cryptGenerateKey( cryptContext );  
  132.     status = cryptExportKey( NULL, 0, &encryptedKeyMaxLength, *sigKeyContext, cryptContext ); //获取公钥大小  
  133.   
  134.     encryptedKey = malloc( encryptedKeyMaxLength );  
  135.   
  136.     status = cryptExportKey( encryptedKey, encryptedKeyMaxLength, &encryptedKeyLength, *sigKeyContext, cryptContext );//导出公钥  
  137.   
  138.     cout<<(char*) encryptedKey<<endl;  
  139.   
  140.   
  141.   
  142. }  
  143. /** 
  144. * 
  145. *3DES对称加密 
  146. * 
  147. **/  
  148. void encrypt(string text,string pwd,string iv,BYTE* result,int* length){  
  149.     CRYPT_CONTEXT context;  
  150.     int status = cryptCreateContext(&context,CRYPT_UNUSED,CRYPT_ALGO_3DES);  
  151.     status  = cryptSetAttributeString(context,CRYPT_CTXINFO_IV,iv.c_str(),iv.size()); //初始向量  
  152.   
  153.     status = cryptSetAttributeString(context,CRYPT_CTXINFO_KEY,pwd.c_str(),pwd.size());//密钥  
  154.   
  155.     int keysize;  
  156.     status = cryptGetAttribute(context,CRYPT_CTXINFO_KEYSIZE,&keysize);  
  157.     if (text.size() % keysize > 0){  
  158.         *length = text.size() + keysize - text.size() % keysize;  
  159.           
  160.     }  
  161.     else{  
  162.         *length = text.size();  
  163.           
  164.     }  
  165.     for(int i =0 ;i<text.size();i++){  
  166.         result[i] = text[i];  //存储到BYTE数组中  
  167.     }  
  168.     status = cryptEncrypt(context,result,*length);//加密处理  
  169.     for(int i = 0 ; i < *length ; i++)  
  170.     {  
  171.         printf("%x ",result[i]);  
  172.     }  
  173.     status = cryptDestroyContext(context);  
  174.     if(status!=0) cout<<"fail"<<endl;  
  175. }  
  176. void decrypt(BYTE* enc , int* length,string pwd,string iv){  
  177.     CRYPT_CONTEXT context;  
  178.     int status = cryptCreateContext(&context,CRYPT_UNUSED,CRYPT_ALGO_3DES);  
  179.     status  = cryptSetAttributeString(context,CRYPT_CTXINFO_IV,iv.c_str(),iv.size());  
  180.     status = cryptSetAttributeString(context,CRYPT_CTXINFO_KEY,pwd.c_str(),pwd.size());  
  181.     int keysize;  
  182.     status = cryptGetAttribute(context,CRYPT_CTXINFO_KEYSIZE,&keysize);  
  183.     status  = cryptDecrypt(context,enc,*length);  
  184.     status  = cryptDestroyContext(context);  
  185. }  
  186. /** 
  187. *   公钥加密信封,返回base64编码字符串 
  188. */  
  189. string envelop(string text,string keyFile){  
  190.     BYTE* message;  
  191.     message = new BYTE[text.size()];  
  192.     for(int i=0;i<text.size();i++){  
  193.         message[i]=text[i];  
  194.     }  
  195.     int messageLength = text.size();  
  196.     /*访问密钥库,获取公钥*/  
  197.     int* keyset;  
  198.     keyset = (int*)malloc(sizeof(int*));  
  199.     int* pubKeyContext;  
  200.     pubKeyContext = (int*)malloc(sizeof(int*));  
  201.     int status = 0;  
  202.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, keyFile.c_str(), CRYPT_KEYOPT_READONLY);  
  203.     status = cryptGetPublicKey(*keyset,pubKeyContext, CRYPT_KEYID_NAME, "test1");  
  204.   
  205.     CRYPT_ENVELOPE cryptEnvelope;  
  206.   
  207.     int bytesCopied;  
  208.   
  209.     status = cryptCreateEnvelope( &cryptEnvelope, CRYPT_UNUSED, CRYPT_FORMAT_CRYPTLIB );  
  210.   
  211.     /* Add the public key */  
  212.   
  213.     status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_PUBLICKEY, *pubKeyContext );  
  214.   
  215.     /* Add the data size information and data, wrap up the processing, and pop out the processed data */  
  216.   
  217.     status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_DATASIZE, messageLength );  
  218.   
  219.     status = cryptPushData( cryptEnvelope, message, messageLength, &bytesCopied );  
  220.   
  221.     status = cryptFlushData( cryptEnvelope );  
  222.     BYTE* envelopedData;  
  223.     envelopedData = new BYTE[1000];  
  224.     int envelopedDataBufferSize = 1000;  
  225.     status = cryptPopData( cryptEnvelope, envelopedData, envelopedDataBufferSize, &bytesCopied );  
  226.   
  227.     status = cryptDestroyEnvelope( cryptEnvelope );  
  228.     string base64Result = base64_encode(envelopedData,bytesCopied);  
  229.     cout<<base64Result<<endl;  
  230.     return base64Result;  
  231. }  
  232. string develop(string env,string keyFile){  
  233.     BYTE* message;  
  234.     string debase = base64_decode(env);  
  235.     message = new BYTE[debase.size()];  
  236.     for(int i=0;i<debase.size();i++){  
  237.         message[i] = debase[i];  
  238.     }  
  239.     int* privKeyContext;  
  240.     privKeyContext = (int*)malloc(sizeof(int*));  
  241.     int messageLength = debase.size();  
  242.     message[messageLength]='\0';  
  243.     CRYPT_ENVELOPE cryptEnvelope;  
  244.     int bytesCopied, status;  
  245.     /*访问密钥库*/  
  246.     int* keyset;  
  247.     keyset = (int*)malloc(sizeof(int*));  
  248.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE,keyFile.c_str(), CRYPT_KEYOPT_READONLY);  
  249.     status = cryptGetPrivateKey(*keyset,privKeyContext, CRYPT_KEYID_NAME, "test1""1234");  
  250.   
  251.     status = cryptCreateEnvelope( &cryptEnvelope, CRYPT_UNUSED, CRYPT_FORMAT_AUTO );  
  252.   
  253.     /* Push in the enveloped data and the private decryption key required to de-envelope it, and pop out the recovered message */  
  254.   
  255.     status = cryptPushData( cryptEnvelope, message, messageLength, &bytesCopied );  
  256.   
  257.     status = cryptSetAttribute( cryptEnvelope, CRYPT_ENVINFO_PRIVATEKEY, *privKeyContext );  
  258.   
  259.     status = cryptFlushData( cryptEnvelope );  
  260.     BYTE* result;  
  261.     result = new BYTE[1000];  
  262.     int resultLength = 1000;  
  263.     status = cryptPopData( cryptEnvelope, result, resultLength, &bytesCopied );  
  264.   
  265.     status = cryptDestroyEnvelope( cryptEnvelope );  
  266.   
  267.   
  268.     cout<<(char*)result<<endl;  
  269.     string r  =  string((char*)result);  
  270.     return r;  
  271. }  
  272. string exportCert(string keyFile){  
  273.     /*访问密钥库,获取公钥*/  
  274.     int* keyset;  
  275.     keyset = (int*)malloc(sizeof(int*));  
  276.     int* certContext;  
  277.     certContext = (int*)malloc(sizeof(int*));  
  278.     int status = 0;  
  279.     status = cryptKeysetOpen(keyset,CRYPT_UNUSED, CRYPT_KEYSET_FILE, keyFile.c_str(), CRYPT_KEYOPT_READONLY);  
  280.     status = cryptGetPublicKey(*keyset,certContext, CRYPT_KEYID_NAME, "test1");  
  281.   
  282.     void *certificate;  
  283.   
  284.     int certLength;  
  285.     int certMaxLength;  
  286.     status = cryptExportCert( NULL, 0, &certMaxLength,CRYPT_CERTFORMAT_CERTIFICATE  
  287.         , *certContext );  
  288.   
  289.     /* Allocate memory for the encoded certificate */  
  290.   
  291.     certificate = malloc( certMaxLength );  
  292.   
  293.     /* Export the encoded certificate from the certificate object */  
  294.   
  295.     status = cryptExportCert( certificate, certMaxLength, &certLength, CRYPT_CERTFORMAT_CERTIFICATE  
  296.         , *certContext );  
  297.     unsigned char* certChar = (unsigned char*)certificate;  
  298.     certChar[certLength]='\0';  
  299.     string cert_base64 = base64_encode(certChar,certLength);  
  300.     return cert_base64;  
  301.   
  302. }  
  303. void importCert(string cert_base64){  
  304.     CRYPT_CERTIFICATE cryptCertificate;  
  305.     int status;  
  306.     string certStr = base64_decode(cert_base64);  
  307.     /* Import the certificate object from the encoded certificate */  
  308.     BYTE* cert;  
  309.     int certLength = certStr.size();  
  310.     cert = new BYTE[certLength];  
  311.     for(int i = 0 ; i<certLength;i++){  
  312.         cert[i] = certStr[i];  
  313.     }  
  314.     cert[certLength]='\0';  
  315.     status = cryptImportCert( cert, certLength, CRYPT_UNUSED, &cryptCertificate );  
  316.   
  317.   
  318. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值