📄 des算法源程序.mht
字号:
<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> This is one =
of the=20
more useful functions in this DES library, =
it<BR> =20
implements CFB mode of DES =
with=20
64bit feedback. Why is this<BR> =
=20
useful you ask? Because =
this=20
routine will allow you to encrypt an<BR> =
=20
arbitrary number of bytes, no 8 =
byte=20
padding. Each call to this<BR> =
=20
routine will encrypt the input =
bytes to=20
output and then update ivec<BR> =
=20
and num. num contains 'how =
far' we are=20
though ivec. If this does<BR> =
=20
not make much sense, read more =
about cfb=20
mode of DES :-).<BR> =
<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> =
=20
Same as des_cfb64_encrypt() accept that =
the DES=20
operation is<BR> =
triple=20
DES. As usual, there is a macro =
for<BR> =20
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> This is a =
implementation of Output Feed Back mode of=20
DES. It is<BR> =
the=20
same as des_cfb_encrypt() in that numbits is the =
size of=20
the<BR> units dealt =
with=20
during input and output (in bits).<BR> =
=20
<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> =
The=20
same as des_cfb64_encrypt() except that it is =
Output=20
Feed Back<BR> =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> =
Same=20
as des_ofb64_encrypt() accept that the DES =
operation=20
is<BR> triple=20
DES. As usual, there is a macro =
for<BR> =20
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> This =
routine is=20
used to get a password from the terminal with=20
echo<BR> turned=20
off. Buf is where the string will end =
up and=20
length is the<BR> =
size of=20
buf. Prompt is a string presented to =
the=20
'user' and if<BR> =
verify is=20
set, the key is asked for twice and unless the 2 =
copies<BR> match, an =
error is=20
returned. A return code of -1 =
indicates=20
a<BR> 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> =
=20
This function produces an 8 byte checksum from =
input=20
that it puts in<BR> =
output=20
and returns the last 4 bytes as a =
long. The=20
checksum is<BR> =
generated via=20
cbc mode of DES in which only the last 8 byes=20
are<BR> =
kept. I=20
would recommend not using this function but =
instead=20
using<BR> the =
EVP_Digest=20
routines, or at least using MD5 or=20
SHA. This<BR> =
=20
function is used by Kerberos v4 so that is why =
it stays=20
in the<BR> =
library.<BR> =20
<BR>char =
*des_fcrypt(<BR>const char=20
*buf,<BR>const char *salt<BR>char =
*ret);<BR> =20
This is my fast version of =
the unix=20
crypt(3) function. This =
version<BR> =20
takes only a small amount =
of space=20
relative to other fast<BR> =
=20
crypt() implementations. This is =
different to=20
the normal crypt<BR> =
in that=20
the third parameter is the buffer that the =
return=20
value<BR> is written=20
into. It needs to be at least 14 =
bytes=20
long. This<BR> =
=20
function is thread safe, unlike the normal=20
crypt.<BR><BR>char *crypt(<BR>const char =
*buf,<BR>const=20
char *salt);<BR> This =
function calls des_fcrypt() with a static array =
passed=20
as the<BR> third=20
parameter. This emulates the normal=20
non-thread safe semantics<BR> =
=20
of crypt(3).<BR><BR>void =
des_string_to_key(<BR>char=20
*str,<BR>des_cblock *key);<BR> =
=20
This function takes str and converts it =
into a=20
DES key. I would<BR> =
=20
recommend using MD5 instead and use the =
first 8=20
bytes of output.<BR> =
When I=20
wrote the first version of these routines back =
in 1990,=20
MD5<BR> did not exist =
but I=20
feel these routines are still=20
sound. This<BR> =
=20
routines is compatible with the one in MIT's=20
libdes.<BR> <BR>void=20
des_string_to_2keys(<BR>char *str,<BR>des_cblock =
*key1,<BR>des_cblock *key2);<BR> =
=20
This function takes str and converts it =
into 2=20
DES keys.<BR> I would =
recommend using MD5 and using the 16 bytes as =
the 2=20
keys.<BR> I have =
nothing=20
against these 2 'string_to_key' routines, it's=20
just<BR> that if you =
say that=20
your encryption key is generated by using =
the<BR> =20
16 bytes of an MD5 hash, =
every-one=20
knows how you generated your<BR> =
=20
keys.<BR><BR>int =
des_read_password(<BR>des_cblock=20
*key,<BR>char *prompt,<BR>int verify);<BR> =
=20
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> =20
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> =
=20
This routine sets a starting point =
for=20
des_random_key().<BR> =
<BR>void des_random_key(<BR>des_cblock =
ret);<BR> =20
This function return a =
random=20
key. Make sure to 'seed' the =
random<BR> =20
number generator (with=20
des_random_seed()) before using this =
function.<BR> =20
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> =
=20
This function will write to a file descriptor =
the=20
encrypted data<BR> =
from=20
buf. This data will be preceded by a =
4 byte=20
'byte count' and<BR> =
will be=20
padded out to 8 bytes. The encryption =
is=20
either CBC of<BR> =
PCBC=20
depending on the value of =
des_rw_mode. If it=20
is DES_PCBC_MODE,<BR> =
pcbc is=20
used, if DES_CBC_MODE, cbc is =
used. The=20
default is to use<BR> =
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> =
=20
This routines read stuff written by =
des_enc_read() and=20
decrypts it.<BR> I =
have used=20
these routines quite a lot but I don't believe =
they=20
are<BR> suitable for=20
non-blocking io. If you are after a=20
full<BR> =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> This is a =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -