• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            天下

            記錄修行的印記

            對openssl做的一些簡單封裝

            #include "stdafx.h"

            int GetRSA(RSA **RsaKeys)
            {
                
            *RsaKeys=RSA_generate_key(RSALEN,RSA_F4,NULL,NULL);
                
            if(NULL==*RsaKeys)
                    
            return -1;
                
            return 0 ;
            }

            //取得私鑰
            int GetPrivateKey(RSA *RsaKeys,RSA **Pvtkey)
            {
                
            *Pvtkey = RSAPrivateKey_dup(RsaKeys);
                
            if(NULL==*Pvtkey)
                    
            return -1
                
            return 0;
            }

            //私鑰To數據流
            int PrivateKeyToData(RSA *Pvtkey,unsigned char* bufkey)
            {
                BIO
            * pBio = BIO_new(BIO_s_mem());
                
            if (pBio == NULL) {
                    
            return -1;
                }
                memset(bufkey,
            '\0',RSALEN);
                
            if( i2d_RSAPrivateKey_bio(pBio,Pvtkey) < 0 ) {
                    BIO_free(pBio);    
                    
            return -1;
                }
                BIO_read(pBio,bufkey,RSALEN);

                BIO_free(pBio);    
                
            return 0;
            }

            //數據流To私鑰
            int DataToPrivateKey(unsigned char* bufkey,RSA **Pvtkey)
            {
                BIO 
            *pBio = BIO_new(BIO_s_mem());
                
            if (pBio == NULL) {
                    
            return -1;
                }
                BIO_write(pBio,bufkey,RSALEN);
                
            if( NULL == d2i_RSAPrivateKey_bio(pBio,Pvtkey)) {
                    BIO_free(pBio);
                    
            return -1;
                }
                BIO_free(pBio);
                
            return 0;
            }

            //取得公鑰
            int GetPublicKey(RSA *RsaKeys,RSA **Pubkey)
            {
                
            *Pubkey = RSAPublicKey_dup(RsaKeys);
                
            if(NULL==*Pubkey)
                    
            return -1
                
            return 0;
            }


            //公鑰To數據流
            int PublicKeyToData(RSA *Pubkey,unsigned char* bufkey)
            {
                BIO 
            *pBio = BIO_new(BIO_s_mem());
                
            if (pBio ==NULL) {
                    
            return -1;
                }
                memset(bufkey,
            '\0',RSALEN);
                
            if(i2d_RSAPublicKey_bio(pBio,Pubkey) < 0) {
                    BIO_free(pBio);
                    
            return -1;
                }
                BIO_read(pBio,bufkey,RSALEN);
                BIO_free(pBio);
                
            return 0;
            }

            //數據流To公鑰
            int DataToPublicKey(unsigned char*bufkey,RSA **Pubkey)
            {
                BIO    
            * pBio=BIO_new(BIO_s_mem());
                
            if (pBio ==NULL)
                {
                    
            return -1;
                }
                BIO_write(pBio,bufkey,RSALEN);
                
            if( d2i_RSAPublicKey_bio(pBio,Pubkey) < 0 ) {
                    BIO_free(pBio);    
                    
            return -1;
                }
                BIO_free(pBio);    
                
            return 0;    
            }

            /*公鑰加密->私鑰解密*/
            int RSAPublicEncrypt(RSA *Publickey, char *From, char *To)
            {
                
            int len=0;
                len 
            = RSA_size(Publickey) -11;

                
            if(-1 == (len=RSA_public_encrypt(len,(unsigned char *)From,(unsigned char *)To,Publickey,RSA_PKCS1_PADDING)) )
                    
            return -1;

                
            return len;
            }


            /*私鑰解密<-公鑰加密*/
            int RSAPrivateDecrypt(RSA *Privtekey, char *From, char *To)
            {
                
            if(-1 == (RSA_private_decrypt(RSALEN/8,(unsigned char *)From,(unsigned char *)To,Privtekey,RSA_PKCS1_PADDING)))
                    
            return -1;

                
            return 0;
            }


            /*私鑰加密->公鑰解密*/
            int RSAPrivateEncrypt(RSA *Privtekey, char *From, char *To)
            {
                
            int len = RSA_size(Privtekey)-11;
                
            if(-1 == (len = RSA_private_encrypt(len,(unsigned char *)From,(unsigned char *)To,Privtekey,RSA_PKCS1_PADDING)))
                    
            return -1;

                
            return len;
            }


            /*公鑰解密<-私鑰加密*/
            int RSAPublicDecrypt(RSA *Publickey, char *From, char *To)
            {
                
            if(-1 == (RSA_public_decrypt(RSALEN/8,(unsigned char *)From,(unsigned char *)To,Publickey,RSA_PKCS1_PADDING)) )
                    
            return -1;

                
            return 0;
            }



            //void DesEncrypt(char *Key,char *Msg, char *Result,int Length)
            //{
            //    int             n=0;
            //    DES_cblock      desblock;
            //    DES_key_schedule schedule;
            //
            //    DES_string_to_key(Key,&desblock);
            //    DES_set_key_checked( &desblock, &schedule );
            //
            //    DES_cfb64_encrypt( (unsigned char *)Msg, (unsigned char *)Result,
            //        Length, &schedule, &desblock, &n, DES_ENCRYPT );
            //
            //}
            //
            //
            //void DesDecrypt( char *Key, char *Msg, char *Result,int Length)
            //{
            //
            //    int             n=0;
            //
            //    DES_cblock      desblock;
            //    DES_key_schedule schedule;
            //
            //    DES_string_to_key(Key,&desblock);
            //    DES_set_key_checked( &desblock, &schedule );
            //
            //    DES_cfb64_encrypt( (unsigned char *) Msg, (unsigned char *)Result,
            //        Length, &schedule, &desblock, &n, DES_DECRYPT );
            //
            //}

            void DESGenerateKey(char *pKey)
            {
                
            int nLen=33;
                
            int flag=0;
                
            int i,k=0;

                srand((unsigned)time(NULL));
                
            for(i=0;i<nLen-1;i++)
                {
                    flag
            =rand()%2;
                    
            if(flag)
                        pKey[k
            ++]='A'+rand()%26;
                    
            else
                        pKey[k
            ++]='a'+rand()%26;
                }

                pKey[k]
            ='\0';
            }

            posted on 2014-03-21 17:40 天下 閱讀(1682) 評論(0)  編輯 收藏 引用 所屬分類: 加密解密

            <2012年5月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            久久婷婷五月综合97色一本一本| 久久亚洲美女精品国产精品| 88久久精品无码一区二区毛片| 99久久国产综合精品五月天喷水 | 久久99国产精品久久| 91精品观看91久久久久久| 久久人人超碰精品CAOPOREN| 色欲久久久天天天综合网| 国产日韩久久久精品影院首页| 久久午夜福利无码1000合集| 四虎国产精品免费久久久| 久久国产亚洲精品| 99久久精品这里只有精品| 久久综合噜噜激激的五月天| 久久影院午夜理论片无码| 99久久综合狠狠综合久久止| 亚洲人成网站999久久久综合| 久久er99热精品一区二区| 久久综合成人网| 国产成人久久777777| 久久人人爽人人爽人人AV东京热| 久久久综合香蕉尹人综合网| 狠狠久久亚洲欧美专区| 国产精品久久久久a影院| 久久福利片| 丰满少妇人妻久久久久久4| 亚洲va国产va天堂va久久| 99久久香蕉国产线看观香| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 久久夜色精品国产噜噜噜亚洲AV| 久久午夜综合久久| 91麻精品国产91久久久久| 久久国产精品99久久久久久老狼| 亚洲AV日韩AV永久无码久久| 亚洲欧洲精品成人久久曰影片| 国产精品久久久99| 精品久久久久久无码免费| 久久精品国产只有精品66| 久久久精品日本一区二区三区| 久久精品二区| 久久久久亚洲精品中文字幕|