• <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)

            鏈接

            最新隨筆

            搜索

            最新評論

            久久精品人人槡人妻人人玩AV | 国产成人综合久久精品红| 26uuu久久五月天| 99久久人人爽亚洲精品美女| 久久久久婷婷| 日韩亚洲国产综合久久久| 久久久亚洲AV波多野结衣| AV色综合久久天堂AV色综合在 | 国产99久久久国产精品小说| 99久久精品国产麻豆| 久久丝袜精品中文字幕| 国内精品伊人久久久久av一坑| 久久99久久无码毛片一区二区 | 亚洲欧美国产精品专区久久 | 午夜精品久久久久久中宇| 国产成人精品久久二区二区| 久久只有这里有精品4| 99精品久久精品| 久久综合国产乱子伦精品免费| 精品一久久香蕉国产线看播放| 久久无码中文字幕东京热| 国产激情久久久久影院老熟女| 精品久久久久久无码不卡| 久久福利片| 精品久久人人爽天天玩人人妻| 国产日产久久高清欧美一区| 亚洲人成精品久久久久| 中文成人久久久久影院免费观看| 欧美777精品久久久久网| 久久精品黄AA片一区二区三区| 久久国产AVJUST麻豆| 狠狠人妻久久久久久综合| 久久99毛片免费观看不卡| 久久精品人人做人人妻人人玩| 亚洲精品乱码久久久久久| 伊人久久五月天| 亚洲精品综合久久| 久久丫忘忧草产品| 亚洲AV无码久久寂寞少妇| 亚洲中文字幕久久精品无码APP| 国产aⅴ激情无码久久|