如题
C的要不要?
要不你去网上找下OpenSSL开放源码,自己编译下,编译成DLL文件,里面什么算法都有...
Des,3Des..........
/* des.h */
/* Copyright (C) 1993 Eric Young - see README for more details */
#ifndef DES_DEFS
#define DES_DEFS
#ifdef __cplusplus
extern "C" {
#endif
typedef unsigned char des_cblock[8];
typedef struct des_ks_struct
{
union {
des_cblock _;
/* make sure things are correct size on machines with
* 8 byte longs */
unsigned long pad[2];
} ks;
#define _ ks._
} des_key_schedule[16];
#define DES_KEY_SZ (sizeof(des_cblock))
#define DES_ENCRYPT 1
#define DES_DECRYPT 0
#define DES_CBC_MODE 0
#define DES_PCBC_MODE 1
#define C_Block des_cblock
#define Key_schedule des_key_schedule
#define ENCRYPT DES_ENCRYPT
#define DECRYPT DES_DECRYPT
#define KEY_SZ DES_KEY_SZ
#define string_to_key des_string_to_key
#define read_pw_string des_read_pw_string
#define random_key des_random_key
#define pcbc_encrypt des_pcbc_encrypt
#define set_key des_set_key
#define key_sched des_key_sched
#define ecb_encrypt des_ecb_encrypt
#define cbc_encrypt des_cbc_encrypt
#define cbc_cksum des_cbc_cksum
#define quad_cksum des_quad_cksum
/* For compatibility with the MIT lib - eay 20/05/92 */
typedef struct des_ks_struct bit_64;
extern int des_check_key; /* defaults to false */
extern int des_rw_mode; /* defaults to DES_PCBC_MODE */
/* The next line is used to disable full ANSI prototypes, if your
* compiler has problems with the prototypes, make sure this line always
* evaluates to true :-) */
#define MSDOS
#if !defined(MSDOS) && !defined(__STDC__)
#ifndef KERBEROS
int des_3ecb_encrypt();
int des_cbc_encrypt();
int des_3cbc_encrypt();
int des_cfb_encrypt();
int des_ecb_encrypt();
int des_encrypt();
int des_enc_read();
int des_enc_write();
int des_ofb_encrypt();
int des_pcbc_encrypt();
int des_random_key();
int des_read_password();
int des_read_2passwords();
int des_read_pw_string();
int des_is_weak_key();
int des_set_key();
int des_key_sched();
int des_string_to_key();
int des_string_to_2keys();
#endif
char *crypt();
unsigned long des_cbc_cksum();
unsigned long des_quad_cksum();
unsigned long des_cbc_cksum();
void des_set_odd_parity();
#else /* PROTO */
int des_3ecb_encrypt(des_cblock *input,des_cblock *output,\
des_key_schedule ks1,des_key_schedule ks2,int encrypt);
unsigned long des_cbc_cksum(des_cblock *input,des_cblock *output,\
long length,des_key_schedule schedule,des_cblock *ivec);
int des_cbc_encrypt(des_cblock *input,des_cblock *output,long length,\
des_key_schedule schedule,des_cblock *ivec,int encrypt);
int des_3cbc_encrypt(des_cblock *input,des_cblock *output,long length,\
des_key_schedule sk1,des_key_schedule sk2,\
des_cblock *ivec1,des_cblock *ivec2,int encrypt);
int des_cfb_encrypt(unsigned char *in,unsigned char *out,int numbits,\
long length,des_key_schedule schedule,des_cblock *ivec,int encrypt);
int des_ecb_encrypt(des_cblock *input,des_cblock *output,\
des_key_schedule ks,int encrypt);
int des_encrypt(unsigned long *input,unsigned long *output,
des_key_schedule ks, int encrypt);
int des_enc_read(int fd,char *buf,int len,des_key_schedule sched,\
des_cblock *iv);
int des_enc_write(int fd,char *buf,int len,des_key_schedule sched,\
des_cblock *iv);
char *crypt(char *buf,char *salt);
int des_ofb_encrypt(unsigned char *in,unsigned char *out,\
int numbits,long length,des_key_schedule schedule,des_cblock *ivec);
int des_pcbc_encrypt(des_cblock *input,des_cblock *output,long length,\
des_key_schedule schedule,des_cblock *ivec,int encrypt);
unsigned long des_quad_cksum(des_cblock *input,des_cblock *output,\
long length,int out_count,des_cblock *seed);
int des_random_key(des_cblock ret);
int des_read_password(des_cblock *key,char *prompt,int verify);
int des_read_2passwords(des_cblock *key1,des_cblock *key2, \
char *prompt,int verify);
int des_read_pw_string(char *buf,int length,char *prompt,int verify);
void des_set_odd_parity(des_cblock *key);
int des_is_weak_key(des_cblock *key);
int des_set_key(des_cblock *key,des_key_schedule schedule);
int des_key_sched(des_cblock *key,des_key_schedule schedule);
int des_string_to_key(char *str,des_cblock *key);
int des_string_to_2keys(char *str,des_cblock *key1,des_cblock *key2);
#endif
#ifdef __cplusplus
}
#endif
#endif
/* des.c */
/* Copyright (C) 1993 Eric Young - see README for more details */
#include <stdio.h>
#ifdef VMS
#include <types.h>
#include <stat.h>
#else
#ifndef _IRIX
#include <sys/types.h>
#endif
#include <sys/stat.h>
#endif
#include "des.h"
#if defined(__STDC__) || defined(VMS) || defined(M_XENIX) || defined(MSDOS)
#include <string.h>
#define bcopy(f,t,n) memcpy(t,f,(size_t)(n))
#define bzero(s,n) memset(s,0,(size_t)(n))
#define bcmp(a,b,n) memcmp(a, b,(size_t)(n))
#define index(s,c) strchr(s,c)
#endif
#ifdef PROTO
int usage(void);
int doencryption(void);
int uufwrite(char *data, int size, int num, FILE *fp);
int uufwriteEnd(FILE *fp);
int uufread(char *out,int size,int num,FILE *fp);
int uuencode(unsigned char *in,int num,unsigned char *out);
int uudecode(unsigned char *in,int num,unsigned char *out);
#else
int usage();
int doencryption();
int uufwrite();
int uufwriteEnd();
int uufread();
int uuencode();
int uudecode();
#endif
#ifdef VMS
#define EXIT(a) exit(a&0x10000000)
#else
#define EXIT(a) exit(a)
#endif
#define BUFSIZE (8*1024)
#define VERIFY 1
#define KEYSIZ 8
#define KEYSIZB 1024 /* should hit tty line limit first :-) */
char key[KEYSIZB+1];
int do_encrypt,longk=0;
char *in=NULL,*out=NULL;
FILE *DES_IN,*DES_OUT,*CKSUM_OUT;
char uuname[200];
char uubuf[50];
int uubufnum;
#define INUUBUFN (45*100)
#define OUTUUBUF (65*100)
char b[OUTUUBUF];
char bb[300];
des_cblock cksum={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
char cksumname[200]="";
int cflag,eflag,dflag,kflag,bflag,fflag,sflag,uflag,flag3,hflag,error;
main(argc,argv)
int argc;
char *argv[];
{
int i;
struct stat ins,outs;
char *p;
cflag=eflag=dflag=kflag=hflag=bflag=fflag=sflag=uflag=flag3=0,error=0;
bzero(key,sizeof(key));
for (i=1; i<argc; i++)
{
p=argv[i];
if ((p[0] == '-') && (p[1] != '\0'))
{
p++;
while (*p)
{
switch (*(p++))
{
case '3':
flag3=1;
/* bflag=0; */
longk=1;
break;
case 'c':
cflag=1;
strncpy(cksumname,p,200);
p+=strlen(cksumname);
break;
case 'C':
cflag=1;
longk=1;
strncpy(cksumname,p,200);
p+=strlen(cksumname);
break;
case 'e':
eflag=1;
break;
case 'E':
eflag=1;
longk=1;
break;
case 'd':
dflag=1;
break;
case 'D':
dflag=1;
longk=1;
break;
case 'b':
bflag=1;
flag3=0;
break;
case 'f':
fflag=1;
break;
case 's':
sflag=1;
break;
case 'u':
uflag=1;
strncpy(uuname,p,200);
p+=strlen(uuname);
break;
case 'h':
hflag=1;
break;
case 'k':
kflag=1;
if ((i+1) == argc)
{
fputs("must have a key with the -k option\n",stderr);
error=1;
}
else
{
int j;
i++;
strncpy(key,argv[i],KEYSIZB);
for (j=strlen(argv[i])-1; j>=0; j--)
argv[i][j]='\0';
}
break;
default:
fprintf(stderr,"'%c' unknown flag\n",p[-1]);
error=1;
break;
}
}
}
else
{
if (in == NULL)
in=argv[i];
else if (out == NULL)
out=argv[i];
else
error=1;
}
}
if (error) usage();
/* We either
* do checksum or
* do encrypt or
* do decrypt or
* do decrypt then ckecksum or
* do checksum then encrypt
*/
if (((eflag+dflag) == 1) || cflag)
{
if (eflag) do_encrypt=DES_ENCRYPT;
if (dflag) do_encrypt=DES_DECRYPT;
}
else
usage();
if ( (in != NULL) &&
(out != NULL) &&
#ifndef MSDOS
(stat(in,&ins) != -1) &&
(stat(out,&outs) != -1) &&
(ins.st_dev == outs.st_dev) &&
(ins.st_ino == outs.st_ino))
#else /* MSDOS */
(strcmp(in,out) == 0))
#endif
{
fputs("input and output file are the same\n",stderr);
EXIT(3);
}
if (!kflag)
if (des_read_pw_string(key,KEYSIZB+1,"Enter key:",eflag?VERIFY:0))
{
fputs("password error\n",stderr);
EXIT(2);
}
if (in == NULL)
DES_IN=stdin;
else if ((DES_IN=fopen(in,"r")) == NULL)
{
perror("opening input file");
EXIT(4);
}
CKSUM_OUT=stdout;
if (out == NULL)
{
DES_OUT=stdout;
CKSUM_OUT=stderr;
}
else if ((DES_OUT=fopen(out,"w")) == NULL)
{
perror("opening output file");
EXIT(5);
}
#ifdef MSDOS
/* This should set the file to binary mode. */
{
#include <fcntl.h>
if (!(uflag && dflag))
setmode(fileno(DES_IN),O_BINARY);
if (!(uflag && eflag))
setmode(fileno(DES_OUT),O_BINARY);
}
#endif
doencryption();
fclose(DES_IN);
fclose(DES_OUT);
EXIT(0);
}
usage()
{
char **u;
static char *usage[]={
"des <options> [input-file [output-file]]",
"options:",
"-e : encrypt using sunOS compatible user key to DES key conversion.",
"-E : encrypt ",
"-d : decrypt using sunOS compatible user key to DES key conversion.",
"-D : decrypt ",
"-c[ckname] : generate a cbc_cksum using sunOS compatible user key to",
" DES key conversion and output to ckname (stdout default,",
" stderr if data being output on stdout). The checksum is",
" generated before encryption and after decryption if used",
" in conjunction with -[eEdD].",
"-C[ckname] : generate a cbc_cksum as for -c but compatible with -[ED].",
"-k key : use key 'key'",
"-h : the key that is entered will be a hexidecimal number",
"-u[uuname] : input file is uudecoded if -[dD] or output uuencoded",
" data if -[eE] (uuname is the filename to put in the",
" uuencode header).",
"-b : encrypt using DES in ecb encryption mode, the defaut is",
" cbc mode.",
"-3 : encrypt using tripple DES encryption. This uses 2 keys",
" generated from the input key. If the input key is less",
" than 8 characters long, this is equivelent to normal",
" encryption. Default is tripple cbc, -b makes it tripple ecb.",
NULL
};
for (u=usage; *u; u++)
{
fputs(*u,stderr);
fputc('\n',stderr);
}
EXIT(1);
}
doencryption()
{
register int i;
des_key_schedule ks,ks2;
unsigned char iv[8],iv2[8],iv3[8];
char *p;
int num=0,j,k,l,rem,ll,len,last,ex=0;
des_cblock kk,k2;
FILE *O;
int Exit=0;
#ifndef MSDOS
static unsigned char buf[BUFSIZE+8],obuf[BUFSIZE+8];
#else
static unsigned char *buf=NULL,*obuf=NULL;
if (buf == NULL)
{
if ( (( buf=(unsigned char *)malloc(BUFSIZE+8)) == NULL) ||
((obuf=(unsigned char *)malloc(BUFSIZE+8)) == NULL))
{
fputs("Not enough memory\n",stderr);
Exit=10;
goto problems;
}
}
#endif
if (hflag)
{
j=(flag3?16:8);
p=key;
for (i=0; i<j; i++)
{
k=0;
if ((*p <= '9') && (*p >= '0'))
k=(*p-'0')<<4;
else if ((*p <= 'f') && (*p >= 'a'))
k=(*p-'a'+10)<<4;
else if ((*p <= 'F') && (*p >= 'A'))
k=(*p-'A'+10)<<4;
else
{
fputs("Bad hex key\n",stderr);
Exit=9;
goto problems;
}
p++;
if ((*p <= '9') && (*p >= '0'))
k|=(*p-'0');
else if ((*p <= 'f') && (*p >= 'a'))
k|=(*p-'a'+10);
else if ((*p <= 'F') && (*p >= 'A'))
k|=(*p-'A'+10);
else
{
fputs("Bad hex key\n",stderr);
Exit=9;
goto problems;
}
p++;
if (i < 8)
kk[i]=k;
else
k2[i-8]=k;
}
des_set_key((C_Block *)k2,ks2);
bzero(k2,sizeof(k2));
}
else if (longk || flag3)
{
if (flag3)
{
des_string_to_2keys(key,(C_Block *)kk,(C_Block *)k2);
des_set_key((C_Block *)k2,ks2);
bzero(k2,sizeof(k2));
}
else
des_string_to_key(key,(C_Block *)kk);
}
else
for (i=0; i<KEYSIZ; i++)
{
l=0;
k=key[i];
for (j=0; j<8; j++)
{
if (k&1) l++;
k>>=1;
}
if (l & 1)
kk[i]=key[i]&0x7f;
else
kk[i]=key[i]|0x80;
}
des_set_key((C_Block *)kk,ks);
bzero(key,sizeof(key));
bzero(kk,sizeof(kk));
/* woops - A bug that does not showup under unix :-( */
bzero(iv,sizeof(iv));
bzero(iv2,sizeof(iv2));
bzero(iv3,sizeof(iv3));
l=1;
rem=0;
/* first read */
if (eflag || (!dflag && cflag))
{
for (;;)
{
num=l=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
l+=rem;
num+=rem;
if (l < 0)
{
perror("read error");
Exit=6;
goto problems;
}
rem=l%8;
len=l-rem;
if (feof(DES_IN))
{
srand(time(NULL));
for (i=7-rem; i>0; i--)
buf[l++]=rand()&0xff;
buf[l++]=rem;
ex=1;
len+=rem;
}
else
l-=rem;
if (cflag)
{
des_cbc_cksum((C_Block *)buf,(C_Block *)cksum,
(long)len,ks,(C_Block *)cksum);
if (!eflag)
{
if (feof(DES_IN)) break;
else continue;
}
}
if (bflag && !flag3)
for (i=0; i<l; i+=8)
des_ecb_encrypt(
(des_cblock *)&(buf[i]),
(des_cblock *)&(obuf[i]),
ks,do_encrypt);
else if (flag3 && bflag)
for (i=0; i<l; i+=8)
des_3ecb_encrypt(
(des_cblock *)&(buf[i]),
(des_cblock *)&(obuf[i]),
ks,ks2,do_encrypt);
else if (flag3 && !bflag)
{
char tmpbuf[8];
if (rem) bcopy(&(buf[l]),tmpbuf,rem);
des_3cbc_encrypt(
(des_cblock *)buf,(des_cblock *)obuf,
(long)l,ks,ks2,(des_cblock *)iv,
(des_cblock *)iv2,do_encrypt);
if (rem) bcopy(tmpbuf,&(buf[l]),rem);
}
else
{
des_cbc_encrypt(
(des_cblock *)buf,(des_cblock *)obuf,
(long)l,ks,(des_cblock *)iv,do_encrypt);
if (l >= 8) bcopy(&(obuf[l-8]),iv,8);
}
if (rem) bcopy(&(buf[l]),buf,rem);
i=0;
while (i < l)
{
if (uflag)
j=uufwrite(obuf,1,l-i,DES_OUT);
else
j=fwrite(obuf,1,l-i,DES_OUT);
if (j == -1)
{
perror("Write error");
Exit=7;
goto problems;
}
i+=j;
}
if (feof(DES_IN))
{
if (uflag) uufwriteEnd(DES_OUT);
break;
}
}
}
else /* decrypt */
{
ex=1;
for (;;)
{
if (ex) {
if (uflag)
l=uufread(buf,1,BUFSIZE,DES_IN);
else
l=fread(buf,1,BUFSIZE,DES_IN);
ex=0;
rem=l%8;
l-=rem;
}
if (l < 0)
{
perror("read error");
Exit=6;
goto problems;
}
if (bflag && !flag3)
for (i=0; i<l; i+=8)
des_ecb_encrypt(
(des_cblock *)&(buf[i]),
(des_cblock *)&(obuf[i]),
ks,do_encrypt);
else if (flag3 && bflag)
for (i=0; i<l; i+=8)
des_3ecb_encrypt(
(des_cblock *)&(buf[i]),
(des_cblock *)&(obuf[i]),
ks,ks2,do_encrypt);
else if (flag3 && !bflag)
{
des_3cbc_encrypt(
(des_cblock *)buf,(des_cblock *)obuf,
(long)l,ks,ks2,(des_cblock *)iv,
(des_cblock *)iv2,do_encrypt);
}
else
{
des_cbc_encrypt(
(des_cblock *)buf,(des_cblock *)obuf,
(long)l,ks,(des_cblock *)iv,do_encrypt);
if (l >= 8) bcopy(&(buf[l-8]),iv,8);
}
if (uflag)
ll=uufread(&(buf[rem]),1,BUFSIZE,DES_IN);
else
ll=fread(&(buf[rem]),1,BUFSIZE,DES_IN);
ll+=rem;
rem=ll%8;
ll-=rem;
if (feof(DES_IN) && (ll == 0))
{
last=obuf[l-1];
if ((last > 7) || (last < 0))
{
fputs("The file was not decrypted correctly.\n",
stderr);
/*Exit=8;
goto problems;*/
last=0;
}
l=l-8+last;
}
i=0;
if (cflag) des_cbc_cksum((C_Block *)obuf,
(C_Block *)cksum,(long)l/8*8,ks,
(C_Block *)cksum);
while (i != l)
{
j=fwrite(obuf,1,l-i,DES_OUT);
if (j == -1)
{
perror("Write error");
Exit=7;
goto problems;
}
i+=j;
}
l=ll;
if ((l == 0) && feof(DES_IN)) break;
}
}
if (cflag)
{
l=0;
if (cksumname[0] != '\0')
{
if ((O=fopen(cksumname,"w")) != NULL)
{
CKSUM_OUT=O;
l=1;
}
}
for (i=0; i<8; i++)
fprintf(CKSUM_OUT,"%02X",cksum[i]);
fprintf(CKSUM_OUT,"\n");
if (l) fclose(CKSUM_OUT);
}
problems:
bzero(buf,sizeof(buf));
bzero(obuf,sizeof(obuf));
bzero(ks,sizeof(ks));
bzero(ks2,sizeof(ks2));
bzero(iv,sizeof(iv));
bzero(iv2,sizeof(iv2));
bzero(iv3,sizeof(iv3));
bzero(kk,sizeof(kk));
bzero(k2,sizeof(k2));
bzero(uubuf,sizeof(uubuf));
bzero(b,sizeof(b));
bzero(bb,sizeof(bb));
bzero(cksum,sizeof(cksum));
if (Exit) EXIT(Exit);
}
int uufwrite(data,size,num,fp)
char *data;
int size; /* We ignore this parameter but it should be > ~50 I believe */
int num;
FILE *fp;
{
int i,j,left,rem,ret=num;
static int start=1;
if (start)
{
fprintf(fp,"begin 600 %s\n",
(uuname[0] == '\0')?"text.d":uuname);
start=0;
}
if (uubufnum)
{
if (uubufnum+num < 45)
{
bcopy(data,&(uubuf[uubufnum]),num);
uubufnum+=num;
return(num);
}
else
{
i=45-uubufnum;
bcopy(data,&(uubuf[uubufnum]),i);
j=uuencode(uubuf,45,b);
fwrite(b,1,j,fp);
uubufnum=0;
data+=i;
num-=i;
}
}
for (i=0; i<(num-INUUBUFN); i+=INUUBUFN)
{
j=uuencode(&(data[i]),INUUBUFN,b);
fwrite(b,1,j,fp);
}
rem=(num-i)%45;
left=(num-i-rem);
if (left)
{
j=uuencode(&(data[i]),left,b);
fwrite(b,1,j,fp);
i+=left;
}
if (i != num)
{
bcopy(&(data[i]),uubuf,rem);
uubufnum=rem;
}
return(ret);
}