• <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 天下 閱讀(1674) 評論(0)  編輯 收藏 引用 所屬分類: 加密解密

            <2013年5月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            色综合久久久久| 久久棈精品久久久久久噜噜| 精品999久久久久久中文字幕 | 国产精品99久久久久久www| 国产精品永久久久久久久久久| 久久精品无码一区二区app| 久久亚洲AV无码精品色午夜| 国产亚洲欧美精品久久久| 久久午夜综合久久| 国产精品一区二区久久不卡 | 久久精品麻豆日日躁夜夜躁| 国产精品狼人久久久久影院| 亚洲国产精品18久久久久久| 无码国内精品久久人妻麻豆按摩| 91精品国产色综合久久| 狠狠色婷婷久久综合频道日韩 | 久久精品中文字幕久久| 久久精品麻豆日日躁夜夜躁| 亚洲&#228;v永久无码精品天堂久久 | 久久精品一本到99热免费| 久久国产精品一区二区| 久久久久人妻精品一区| 18禁黄久久久AAA片| 久久久久亚洲精品中文字幕| 热久久这里只有精品| 国产亚洲欧美精品久久久| 亚洲狠狠婷婷综合久久蜜芽| 思思久久好好热精品国产| 久久亚洲天堂| 久久97久久97精品免视看秋霞 | 久久天天躁狠狠躁夜夜av浪潮| 91精品国产高清久久久久久91| 久久超碰97人人做人人爱| 亚洲第一极品精品无码久久| 伊人久久精品无码二区麻豆| 久久中文字幕人妻熟av女| 久久久久青草线蕉综合超碰| 久久人与动人物a级毛片| 亚洲人成伊人成综合网久久久| 囯产极品美女高潮无套久久久| 少妇人妻综合久久中文字幕|