📄 des算法源程序.mht
字号:
pointer to 2 unsigned long's and ks is =
the<BR> =20
des_key_schedule to=20
use. enc, is non zero specifies=20
encryption,<BR> zero =
if=20
decryption.<BR><BR>void =
des_encrypt2(<BR>unsigned long=20
*data,<BR>des_key_schedule ks,<BR>int =
enc);<BR> =20
This functions is the same =
as=20
des_encrypt() except that the DES<BR> =
=20
initial permutation (IP) and final =
permutation (FP) have been left<BR> =
=20
out. As for des_encrypt(), you =
should=20
not use this function.<BR> =
It=20
is used by the routines in my library that =
implement=20
triple DES.<BR> IP()=20
des_encrypt2() des_encrypt2() des_encrypt2() =
FP() is the=20
same<BR> 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> =
This is=20
the basic Electronic Code Book form of DES, the =
most=20
basic<BR> =20
form. Input is encrypted into output =
using=20
the key represented by<BR> =
=20
ks. If enc is non zero (DES_ENCRYPT), =
encryption occurs, otherwise<BR> =
=20
decryption occurs. Input is 8 =
bytes=20
long and output is 8 bytes.<BR> =
=20
(the des_cblock structure is 8 =
chars).<BR> =20
<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> =20
This is the 3 key EDE mode =
of ECB=20
DES. What this means is that =
<BR> =20
the 8 bytes of input is =
encrypted=20
with ks1, decrypted with ks2 and<BR> =
=20
then encrypted again with ks3, before =
being put=20
into output;<BR> =20
C=3DE(ks3,D(ks2,E(ks1,M))). There is =
a macro,=20
des_ecb2_encrypt()<BR> =
that=20
only takes 2 des_key_schedules that=20
implements,<BR> =20
C=3DE(ks1,D(ks2,E(ks1,M))) in that the final =
encrypt is=20
done with ks1.<BR> =
<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> =
=20
This routine implements DES in =
Cipher=20
Block Chaining mode.<BR> =
=20
Input, which should be a multiple of 8 bytes is=20
encrypted<BR> (or =
decrypted)=20
to output which will also be a multiple of 8=20
bytes.<BR> The number =
of=20
bytes is in length (and from what I've said=20
above,<BR> should be =
a=20
multiple of 8). If length is not a =
multiple=20
of 8, I'm<BR> not =
being held=20
responsible :-). ivec is the =
initialisation=20
vector.<BR> This =
function=20
does not modify this variable. To =
correctly=20
implement<BR> cbc =
mode, you=20
need to do one of 2 things; copy the last 8 =
bytes=20
of<BR> cipher text =
for use as=20
the next ivec in your application,<BR> =
=20
or use des_ncbc_encrypt(). =
<BR> =20
Only this routine has this =
problem=20
with updating the ivec, all<BR> =
=20
other routines that are implementing cbc =
mode=20
update ivec.<BR> =
<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> =
=20
For historical reasons, =
des_cbc_encrypt()=20
did not update the<BR> =
ivec=20
with the value requires so that subsequent calls =
to<BR> =
des_cbc_encrypt()=20
would 'chain'. This was needed so =
that the=20
same<BR> 'length' =
values=20
would not need to be used when =
decrypting.<BR> =20
des_ncbc_encrypt() does the =
right=20
thing. It is the same as<BR> =
=20
des_cbc_encrypt accept that ivec =
is=20
updates with the correct value<BR> =
=20
to pass in subsequent calls to=20
des_ncbc_encrypt(). I advise =
using<BR> =20
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> =20
This is RSA's DESX mode of=20
DES. It uses inw and outw =
to<BR> =20
'whiten' the =
encryption. inw=20
and outw are secret (unlike the iv)<BR> =
=20
and are as such, part of the=20
key. So the key is sort of 24=20
bytes.<BR> This is =
much=20
better than cbc des.<BR> =
=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> This =
function is=20
flawed, do not use it. I have left it =
in=20
the<BR> library =
because it is=20
used in my des(1) program and will =
function<BR> =20
correctly when used by=20
des(1). If I removed the function,=20
people<BR> could end =
up=20
unable to decrypt files.<BR> =
=20
This routine implements outer triple cbc =
encryption=20
using 2 ks and<BR> 2=20
ivec's. Use des_ede2_cbc_encrypt()=20
instead.<BR> <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> =
This=20
function implements inner triple CBC DES =
encryption with=20
3<BR> =
keys. What=20
this means is that each 'DES' =
operation<BR> =20
inside the cbc mode is really an=20
C=3DE(ks3,D(ks2,E(ks1,M))).<BR> =
=20
Again, this is cbc mode so an ivec is=20
requires.<BR> This =
mode is=20
used by SSL.<BR> =
There is=20
also a des_ede2_cbc_encrypt() that only uses =
2<BR> =20
des_key_schedule's, the =
first being=20
reused for the final<BR> =
=20
=
encryption. C=3DE(ks1,D(ks2,E(ks1,M))). This=20
form of triple DES<BR> =
is=20
used by the RSAref library.<BR> =
=20
<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> =
This=20
is Propagating Cipher Block Chaining mode of=20
DES. It is used<BR> =
=20
by Kerberos v4. It's =
parameters are=20
the same as des_ncbc_encrypt().<BR> =
=20
<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> =
=20
Cipher Feedback Back mode of =
DES. This=20
implementation 'feeds back'<BR> =
=20
in numbit blocks. The input =
(and=20
output) is in multiples of numbits<BR> =
=20
bits. numbits should to =
be a=20
multiple of 8 bits. Length is =
the<BR> =20
number of bytes=20
input. If numbits is not a multiple =
of 8=20
bits,<BR> the extra =
bits in=20
the bytes will be considered =
padding. So=20
if<BR> numbits is 12, =
for=20
each 2 input bytes, the 4 high bits of =
the<BR> =20
second byte will be=20
ignored. So to encode 72 bits when=20
using<BR> a numbits =
of 12=20
take 12 bytes. To encode 72 bits when =
using<BR> numbits of =
9 will=20
take 16 bytes. To encode 80 bits when =
using<BR> numbits of =
16 will=20
take 10 bytes. etc, etc. This padding =
will<BR> apply to =
both input=20
and output.<BR><BR> =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -