⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des算法源程序.mht

📁 精华BBS贴子
💻 MHT
📖 第 1 页 / 共 5 页
字号:
<BR>void=20
                        des_cfb64_encrypt(<BR>unsigned char =
*in,<BR>unsigned=20
                        char *out,<BR>long length,<BR>des_key_schedule=20
                        ks,<BR>des_cblock *ivec,<BR>int *num,<BR>int=20
                        enc);<BR>&nbsp; &nbsp; &nbsp; &nbsp; This is one =
of the=20
                        more useful functions in this DES library, =
it<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; implements CFB mode of DES =
with=20
                        64bit feedback.&nbsp;&nbsp;Why is this<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; useful you ask?&nbsp;&nbsp;Because =
this=20
                        routine will allow you to encrypt an<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; arbitrary number of bytes, no 8 =
byte=20
                        padding.&nbsp;&nbsp;Each call to this<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; routine will encrypt the input =
bytes to=20
                        output and then update ivec<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; and num.&nbsp;&nbsp;num contains 'how =
far' we are=20
                        though ivec.&nbsp;&nbsp;If this does<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; not make much sense, read more =
about cfb=20
                        mode of DES :-).<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
<BR>void=20
                        des_ede3_cfb64_encrypt(<BR>unsigned char=20
                        *in,<BR>unsigned char *out,<BR>long=20
                        length,<BR>des_key_schedule =
ks1,<BR>des_key_schedule=20
                        ks2,<BR>des_key_schedule ks3,<BR>des_cblock=20
                        *ivec,<BR>int *num,<BR>int enc);<BR>&nbsp; =
&nbsp; &nbsp;=20
                        &nbsp; Same as des_cfb64_encrypt() accept that =
the DES=20
                        operation is<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
triple=20
                        DES.&nbsp;&nbsp;As usual, there is a macro =
for<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; des_ede2_cfb64_encrypt() =
which=20
                        reuses ks1.<BR><BR>void =
des_ofb_encrypt(<BR>unsigned=20
                        char *in,<BR>unsigned char *out,<BR>int =
numbits,<BR>long=20
                        length,<BR>des_key_schedule ks,<BR>des_cblock=20
                        *ivec);<BR>&nbsp; &nbsp; &nbsp; &nbsp; This is a =

                        implementation of Output Feed Back mode of=20
                        DES.&nbsp;&nbsp;It is<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; the=20
                        same as des_cfb_encrypt() in that numbits is the =
size of=20
                        the<BR>&nbsp; &nbsp; &nbsp; &nbsp; units dealt =
with=20
                        during input and output (in bits).<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; <BR>void =
des_ofb64_encrypt(<BR>unsigned=20
                        char *in,<BR>unsigned char *out,<BR>long=20
                        length,<BR>des_key_schedule ks,<BR>des_cblock=20
                        *ivec,<BR>int *num);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; The=20
                        same as des_cfb64_encrypt() except that it is =
Output=20
                        Feed Back<BR>&nbsp; &nbsp; &nbsp; &nbsp;=20
                        mode.<BR><BR>void =
des_ede3_ofb64_encrypt(<BR>unsigned=20
                        char *in,<BR>unsigned char *out,<BR>long=20
                        length,<BR>des_key_schedule =
ks1,<BR>des_key_schedule=20
                        ks2,<BR>des_key_schedule ks3,<BR>des_cblock=20
                        *ivec,<BR>int *num);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; Same=20
                        as des_ofb64_encrypt() accept that the DES =
operation=20
                        is<BR>&nbsp; &nbsp; &nbsp; &nbsp; triple=20
                        DES.&nbsp;&nbsp;As usual, there is a macro =
for<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; des_ede2_ofb64_encrypt() =
which=20
                        reuses ks1.<BR><BR>int =
des_read_pw_string(<BR>char=20
                        *buf,<BR>int length,<BR>char *prompt,<BR>int=20
                        verify);<BR>&nbsp; &nbsp; &nbsp; &nbsp; This =
routine is=20
                        used to get a password from the terminal with=20
                        echo<BR>&nbsp; &nbsp; &nbsp; &nbsp; turned=20
                        off.&nbsp;&nbsp;Buf is where the string will end =
up and=20
                        length is the<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
size of=20
                        buf.&nbsp;&nbsp;Prompt is a string presented to =
the=20
                        'user' and if<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
verify is=20
                        set, the key is asked for twice and unless the 2 =

                        copies<BR>&nbsp; &nbsp; &nbsp; &nbsp; match, an =
error is=20
                        returned.&nbsp;&nbsp;A return code of -1 =
indicates=20
                        a<BR>&nbsp; &nbsp; &nbsp; &nbsp; system error, 1 =
failure=20
                        due to use interaction, and 0 is=20
                        success.<BR><BR>unsigned long=20
                        des_cbc_cksum(<BR>des_cblock =
*input,<BR>des_cblock=20
                        *output,<BR>long length,<BR>des_key_schedule=20
                        ks,<BR>des_cblock *ivec);<BR>&nbsp; &nbsp; =
&nbsp; &nbsp;=20
                        This function produces an 8 byte checksum from =
input=20
                        that it puts in<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
output=20
                        and returns the last 4 bytes as a =
long.&nbsp;&nbsp;The=20
                        checksum is<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
generated via=20
                        cbc mode of DES in which only the last 8 byes=20
                        are<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
kept.&nbsp;&nbsp;I=20
                        would recommend not using this function but =
instead=20
                        using<BR>&nbsp; &nbsp; &nbsp; &nbsp; the =
EVP_Digest=20
                        routines, or at least using MD5 or=20
                        SHA.&nbsp;&nbsp;This<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        function is used by Kerberos v4 so that is why =
it stays=20
                        in the<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
library.<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; <BR>char =
*des_fcrypt(<BR>const char=20
                        *buf,<BR>const char *salt<BR>char =
*ret);<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; This is my fast version of =
the unix=20
                        crypt(3) function.&nbsp;&nbsp;This =
version<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; takes only a small amount =
of space=20
                        relative to other fast<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        crypt() implementations.&nbsp;&nbsp;This is =
different to=20
                        the normal crypt<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
in that=20
                        the third parameter is the buffer that the =
return=20
                        value<BR>&nbsp; &nbsp; &nbsp; &nbsp; is written=20
                        into.&nbsp;&nbsp;It needs to be at least 14 =
bytes=20
                        long.&nbsp;&nbsp;This<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        function is thread safe, unlike the normal=20
                        crypt.<BR><BR>char *crypt(<BR>const char =
*buf,<BR>const=20
                        char *salt);<BR>&nbsp; &nbsp; &nbsp; &nbsp; This =

                        function calls des_fcrypt() with a static array =
passed=20
                        as the<BR>&nbsp; &nbsp; &nbsp; &nbsp; third=20
                        parameter.&nbsp;&nbsp;This emulates the normal=20
                        non-thread safe semantics<BR>&nbsp; &nbsp; =
&nbsp; &nbsp;=20
                        of crypt(3).<BR><BR>void =
des_string_to_key(<BR>char=20
                        *str,<BR>des_cblock *key);<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; This function takes str and converts it =
into a=20
                        DES key.&nbsp;&nbsp;I would<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; recommend using MD5 instead and use the =
first 8=20
                        bytes of output.<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
When I=20
                        wrote the first version of these routines back =
in 1990,=20
                        MD5<BR>&nbsp; &nbsp; &nbsp; &nbsp; did not exist =
but I=20
                        feel these routines are still=20
                        sound.&nbsp;&nbsp;This<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        routines is compatible with the one in MIT's=20
                        libdes.<BR>&nbsp; &nbsp; &nbsp; &nbsp; <BR>void=20
                        des_string_to_2keys(<BR>char *str,<BR>des_cblock =

                        *key1,<BR>des_cblock *key2);<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; This function takes str and converts it =
into 2=20
                        DES keys.<BR>&nbsp; &nbsp; &nbsp; &nbsp; I would =

                        recommend using MD5 and using the 16 bytes as =
the 2=20
                        keys.<BR>&nbsp; &nbsp; &nbsp; &nbsp; I have =
nothing=20
                        against these 2 'string_to_key' routines, it's=20
                        just<BR>&nbsp; &nbsp; &nbsp; &nbsp; that if you =
say that=20
                        your encryption key is generated by using =
the<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; 16 bytes of an MD5 hash, =
every-one=20
                        knows how you generated your<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; keys.<BR><BR>int =
des_read_password(<BR>des_cblock=20
                        *key,<BR>char *prompt,<BR>int verify);<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; This routine combines =
des_read_pw_string()=20
                        with des_string_to_key().<BR><BR>int=20
                        des_read_2passwords(<BR>des_cblock =
*key1,<BR>des_cblock=20
                        *key2,<BR>char *prompt,<BR>int =
verify);<BR>&nbsp; &nbsp;=20
                        &nbsp; &nbsp; This routine combines =
des_read_pw_string()=20
                        with des_string_to_2key().<BR><BR>void=20
                        des_random_seed(<BR>des_cblock key);<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; This routine sets a starting point =
for=20
                        des_random_key().<BR>&nbsp; &nbsp; &nbsp; &nbsp; =

                        <BR>void des_random_key(<BR>des_cblock =
ret);<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; This function return a =
random=20
                        key.&nbsp;&nbsp;Make sure to 'seed' the =
random<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; number generator (with=20
                        des_random_seed()) before using this =
function.<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; I personally now use a MD5 =
based=20
                        random number system.<BR><BR>int =
des_enc_read(<BR>int=20
                        fd,<BR>char *buf,<BR>int =
len,<BR>des_key_schedule=20
                        ks,<BR>des_cblock *iv);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        This function will write to a file descriptor =
the=20
                        encrypted data<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
from=20
                        buf.&nbsp;&nbsp;This data will be preceded by a =
4 byte=20
                        'byte count' and<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
will be=20
                        padded out to 8 bytes.&nbsp;&nbsp;The encryption =
is=20
                        either CBC of<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
PCBC=20
                        depending on the value of =
des_rw_mode.&nbsp;&nbsp;If it=20
                        is DES_PCBC_MODE,<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
pcbc is=20
                        used, if DES_CBC_MODE, cbc is =
used.&nbsp;&nbsp;The=20
                        default is to use<BR>&nbsp; &nbsp; &nbsp; &nbsp; =

                        DES_PCBC_MODE.<BR><BR>int des_enc_write(<BR>int=20
                        fd,<BR>char *buf,<BR>int =
len,<BR>des_key_schedule=20
                        ks,<BR>des_cblock *iv);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        This routines read stuff written by =
des_enc_read() and=20
                        decrypts it.<BR>&nbsp; &nbsp; &nbsp; &nbsp; I =
have used=20
                        these routines quite a lot but I don't believe =
they=20
                        are<BR>&nbsp; &nbsp; &nbsp; &nbsp; suitable for=20
                        non-blocking io.&nbsp;&nbsp;If you are after a=20
                        full<BR>&nbsp; &nbsp; &nbsp; &nbsp;=20
                        authentication/encryption over networks, have a =
look at=20
                        SSL instead.<BR><BR>unsigned long=20
                        des_quad_cksum(<BR>des_cblock =
*input,<BR>des_cblock=20
                        *output,<BR>long length,<BR>int =
out_count,<BR>des_cblock=20
                        *seed);<BR>&nbsp; &nbsp; &nbsp; &nbsp; This is a =

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -