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

📄 des算法源程序.mht

📁 精华BBS贴子
💻 MHT
📖 第 1 页 / 共 5 页
字号:
                        pointer to 2 unsigned long's and ks is =
the<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; des_key_schedule to=20
                        use.&nbsp;&nbsp;enc, is non zero specifies=20
                        encryption,<BR>&nbsp; &nbsp; &nbsp; &nbsp; zero =
if=20
                        decryption.<BR><BR>void =
des_encrypt2(<BR>unsigned long=20
                        *data,<BR>des_key_schedule ks,<BR>int =
enc);<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; This functions is the same =
as=20
                        des_encrypt() except that the DES<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; initial permutation (IP) and final =

                        permutation (FP) have been left<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; out.&nbsp;&nbsp;As for des_encrypt(), you =
should=20
                        not use this function.<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; It=20
                        is used by the routines in my library that =
implement=20
                        triple DES.<BR>&nbsp; &nbsp; &nbsp; &nbsp; IP()=20
                        des_encrypt2() des_encrypt2() des_encrypt2() =
FP() is the=20
                        same<BR>&nbsp; &nbsp; &nbsp; &nbsp; as =
des_encrypt()=20
                        des_encrypt() des_encrypt() except faster=20
                        :-).<BR><BR>void des_ecb_encrypt(<BR>des_cblock=20
                        *input,<BR>des_cblock =
*output,<BR>des_key_schedule=20
                        ks,<BR>int enc);<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
This is=20
                        the basic Electronic Code Book form of DES, the =
most=20
                        basic<BR>&nbsp; &nbsp; &nbsp; &nbsp;=20
                        form.&nbsp;&nbsp;Input is encrypted into output =
using=20
                        the key represented by<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        ks.&nbsp;&nbsp;If enc is non zero (DES_ENCRYPT), =

                        encryption occurs, otherwise<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; decryption occurs.&nbsp;&nbsp;Input is 8 =
bytes=20
                        long and output is 8 bytes.<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; (the des_cblock structure is 8 =
chars).<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; <BR>void=20
                        des_ecb3_encrypt(<BR>des_cblock =
*input,<BR>des_cblock=20
                        *output,<BR>des_key_schedule =
ks1,<BR>des_key_schedule=20
                        ks2,<BR>des_key_schedule ks3,<BR>int =
enc);<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; This is the 3 key EDE mode =
of ECB=20
                        DES.&nbsp;&nbsp;What this means is that =
<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; the 8 bytes of input is =
encrypted=20
                        with ks1, decrypted with ks2 and<BR>&nbsp; =
&nbsp; &nbsp;=20
                        &nbsp; then encrypted again with ks3, before =
being put=20
                        into output;<BR>&nbsp; &nbsp; &nbsp; &nbsp;=20
                        C=3DE(ks3,D(ks2,E(ks1,M))).&nbsp;&nbsp;There is =
a macro,=20
                        des_ecb2_encrypt()<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; that=20
                        only takes 2 des_key_schedules that=20
                        implements,<BR>&nbsp; &nbsp; &nbsp; &nbsp;=20
                        C=3DE(ks1,D(ks2,E(ks1,M))) in that the final =
encrypt is=20
                        done with ks1.<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
<BR>void=20
                        des_cbc_encrypt(<BR>des_cblock =
*input,<BR>des_cblock=20
                        *output,<BR>long length,<BR>des_key_schedule=20
                        ks,<BR>des_cblock *ivec,<BR>int enc);<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; This routine implements DES in =
Cipher=20
                        Block Chaining mode.<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        Input, which should be a multiple of 8 bytes is=20
                        encrypted<BR>&nbsp; &nbsp; &nbsp; &nbsp; (or =
decrypted)=20
                        to output which will also be a multiple of 8=20
                        bytes.<BR>&nbsp; &nbsp; &nbsp; &nbsp; The number =
of=20
                        bytes is in length (and from what I've said=20
                        above,<BR>&nbsp; &nbsp; &nbsp; &nbsp; should be =
a=20
                        multiple of 8).&nbsp;&nbsp;If length is not a =
multiple=20
                        of 8, I'm<BR>&nbsp; &nbsp; &nbsp; &nbsp; not =
being held=20
                        responsible :-).&nbsp;&nbsp;ivec is the =
initialisation=20
                        vector.<BR>&nbsp; &nbsp; &nbsp; &nbsp; This =
function=20
                        does not modify this variable.&nbsp;&nbsp;To =
correctly=20
                        implement<BR>&nbsp; &nbsp; &nbsp; &nbsp; cbc =
mode, you=20
                        need to do one of 2 things; copy the last 8 =
bytes=20
                        of<BR>&nbsp; &nbsp; &nbsp; &nbsp; cipher text =
for use as=20
                        the next ivec in your application,<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; or use des_ncbc_encrypt(). =
<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; Only this routine has this =
problem=20
                        with updating the ivec, all<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; other routines that are implementing cbc =
mode=20
                        update ivec.<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
<BR>void=20
                        des_ncbc_encrypt(<BR>des_cblock =
*input,<BR>des_cblock=20
                        *output,<BR>long length,<BR>des_key_schedule=20
                        sk,<BR>des_cblock *ivec,<BR>int enc);<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; For historical reasons, =
des_cbc_encrypt()=20
                        did not update the<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; ivec=20
                        with the value requires so that subsequent calls =

                        to<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
des_cbc_encrypt()=20
                        would 'chain'.&nbsp;&nbsp;This was needed so =
that the=20
                        same<BR>&nbsp; &nbsp; &nbsp; &nbsp; 'length' =
values=20
                        would not need to be used when =
decrypting.<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; des_ncbc_encrypt() does the =
right=20
                        thing.&nbsp;&nbsp;It is the same as<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; des_cbc_encrypt accept that ivec =
is=20
                        updates with the correct value<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; to pass in subsequent calls to=20
                        des_ncbc_encrypt().&nbsp;&nbsp;I advise =
using<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; des_ncbc_encrypt() instead =
of=20
                        des_cbc_encrypt();<BR><BR>void=20
                        des_xcbc_encrypt(<BR>des_cblock =
*input,<BR>des_cblock=20
                        *output,<BR>long length,<BR>des_key_schedule=20
                        sk,<BR>des_cblock *ivec,<BR>des_cblock=20
                        *inw,<BR>des_cblock *outw,<BR>int =
enc);<BR>&nbsp; &nbsp;=20
                        &nbsp; &nbsp; This is RSA's DESX mode of=20
                        DES.&nbsp;&nbsp;It uses inw and outw =
to<BR>&nbsp; &nbsp;=20
                        &nbsp; &nbsp; 'whiten' the =
encryption.&nbsp;&nbsp;inw=20
                        and outw are secret (unlike the iv)<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; and are as such, part of the=20
                        key.&nbsp;&nbsp;So the key is sort of 24=20
                        bytes.<BR>&nbsp; &nbsp; &nbsp; &nbsp; This is =
much=20
                        better than cbc des.<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        <BR>void des_3cbc_encrypt(<BR>des_cblock=20
                        *input,<BR>des_cblock *output,<BR>long=20
                        length,<BR>des_key_schedule =
sk1,<BR>des_key_schedule=20
                        sk2,<BR>des_cblock *ivec1,<BR>des_cblock =
*ivec2,<BR>int=20
                        enc);<BR>&nbsp; &nbsp; &nbsp; &nbsp; This =
function is=20
                        flawed, do not use it.&nbsp;&nbsp;I have left it =
in=20
                        the<BR>&nbsp; &nbsp; &nbsp; &nbsp; library =
because it is=20
                        used in my des(1) program and will =
function<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; correctly when used by=20
                        des(1).&nbsp;&nbsp;If I removed the function,=20
                        people<BR>&nbsp; &nbsp; &nbsp; &nbsp; could end =
up=20
                        unable to decrypt files.<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        This routine implements outer triple cbc =
encryption=20
                        using 2 ks and<BR>&nbsp; &nbsp; &nbsp; &nbsp; 2=20
                        ivec's.&nbsp;&nbsp;Use des_ede2_cbc_encrypt()=20
                        instead.<BR>&nbsp; &nbsp; &nbsp; &nbsp; <BR>void =

                        des_ede3_cbc_encrypt(<BR>des_cblock=20
                        *input,<BR>des_cblock *output, <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 enc);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; This=20
                        function implements inner triple CBC DES =
encryption with=20
                        3<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
keys.&nbsp;&nbsp;What=20
                        this means is that each 'DES' =
operation<BR>&nbsp; &nbsp;=20
                        &nbsp; &nbsp; inside the cbc mode is really an=20
                        C=3DE(ks3,D(ks2,E(ks1,M))).<BR>&nbsp; &nbsp; =
&nbsp; &nbsp;=20
                        Again, this is cbc mode so an ivec is=20
                        requires.<BR>&nbsp; &nbsp; &nbsp; &nbsp; This =
mode is=20
                        used by SSL.<BR>&nbsp; &nbsp; &nbsp; &nbsp; =
There is=20
                        also a des_ede2_cbc_encrypt() that only uses =
2<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; des_key_schedule's, the =
first being=20
                        reused for the final<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        =
encryption.&nbsp;&nbsp;C=3DE(ks1,D(ks2,E(ks1,M))).&nbsp;&nbsp;This=20
                        form of triple DES<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; is=20
                        used by the RSAref library.<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; <BR>void des_pcbc_encrypt(<BR>des_cblock=20
                        *input,<BR>des_cblock *output,<BR>long=20
                        length,<BR>des_key_schedule ks,<BR>des_cblock=20
                        *ivec,<BR>int enc);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp; This=20
                        is Propagating Cipher Block Chaining mode of=20
                        DES.&nbsp;&nbsp;It is used<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; by Kerberos v4.&nbsp;&nbsp;It's =
parameters are=20
                        the same as des_ncbc_encrypt().<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; <BR>void des_cfb_encrypt(<BR>unsigned =
char=20
                        *in,<BR>unsigned char *out,<BR>int =
numbits,<BR>long=20
                        length,<BR>des_key_schedule ks,<BR>des_cblock=20
                        *ivec,<BR>int enc);<BR>&nbsp; &nbsp; &nbsp; =
&nbsp;=20
                        Cipher Feedback Back mode of =
DES.&nbsp;&nbsp;This=20
                        implementation 'feeds back'<BR>&nbsp; &nbsp; =
&nbsp;=20
                        &nbsp; in numbit blocks.&nbsp;&nbsp;The input =
(and=20
                        output) is in multiples of numbits<BR>&nbsp; =
&nbsp;=20
                        &nbsp; &nbsp; bits.&nbsp;&nbsp;numbits should to =
be a=20
                        multiple of 8 bits.&nbsp;&nbsp;Length is =
the<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; number of bytes=20
                        input.&nbsp;&nbsp;If numbits is not a multiple =
of 8=20
                        bits,<BR>&nbsp; &nbsp; &nbsp; &nbsp; the extra =
bits in=20
                        the bytes will be considered =
padding.&nbsp;&nbsp;So=20
                        if<BR>&nbsp; &nbsp; &nbsp; &nbsp; numbits is 12, =
for=20
                        each 2 input bytes, the 4 high bits of =
the<BR>&nbsp;=20
                        &nbsp; &nbsp; &nbsp; second byte will be=20
                        ignored.&nbsp;&nbsp;So to encode 72 bits when=20
                        using<BR>&nbsp; &nbsp; &nbsp; &nbsp; a numbits =
of 12=20
                        take 12 bytes.&nbsp;&nbsp;To encode 72 bits when =

                        using<BR>&nbsp; &nbsp; &nbsp; &nbsp; numbits of =
9 will=20
                        take 16 bytes.&nbsp;&nbsp;To encode 80 bits when =

                        using<BR>&nbsp; &nbsp; &nbsp; &nbsp; numbits of =
16 will=20
                        take 10 bytes. etc, etc.&nbsp;&nbsp;This padding =

                        will<BR>&nbsp; &nbsp; &nbsp; &nbsp; apply to =
both input=20
                        and output.<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; =

⌨️ 快捷键说明

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