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

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿(4)

            隨筆分類(378)

            隨筆檔案(329)

            鏈接

            最新隨筆

            搜索

            最新評論

            精品熟女少妇AV免费久久| 久久伊人中文无码| 久久香蕉国产线看观看99| 久久综合丁香激情久久| 草草久久久无码国产专区| 久久人人爽人人精品视频| 日本WV一本一道久久香蕉| 一本久久a久久精品综合夜夜| 午夜精品久久久久久影视777| 国产∨亚洲V天堂无码久久久| 国产成人香蕉久久久久| 大香伊人久久精品一区二区| 久久99精品综合国产首页| 色综合久久久久综合体桃花网| 精品久久久久久无码人妻蜜桃| 久久久老熟女一区二区三区| 欧美粉嫩小泬久久久久久久 | 狠狠色婷婷综合天天久久丁香| 久久九色综合九色99伊人| 国产高清国内精品福利99久久| 无码8090精品久久一区| 精品一区二区久久| 日产精品久久久久久久| 欧美激情精品久久久久久| 亚洲午夜久久影院| 久久婷婷国产麻豆91天堂| 天天爽天天狠久久久综合麻豆| 久久婷婷色香五月综合激情 | 国产L精品国产亚洲区久久| 中文字幕热久久久久久久| 麻豆亚洲AV永久无码精品久久| 久久国产免费直播| 欧美亚洲日本久久精品| 久久久久亚洲AV无码专区桃色| 办公室久久精品| 久久久久亚洲?V成人无码| 久久久久久无码国产精品中文字幕| 久久午夜电影网| 色婷婷噜噜久久国产精品12p| 亚洲伊人久久成综合人影院 | 久久91精品久久91综合|