A blockwise encrypting compressor-开源

  • L6_249671
    了解作者
  • 3.6KB
    文件大小
  • 文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-24 05:50
    上传日期
一组Unixish工具,允许在可交换媒体上进行加密和压缩的备份。
shrinkfish-0.7.tar.gz
  • shrinkfish-0.7
  • dysfil.c
    4.3KB
  • shrinkfish.c
    7.3KB
  • Makefile
    501B
内容介绍
/* * $ProjectHeader: shrinkfish 0.6 Tue, 10 Dec 2002 05:58:59 +0100 kjwolf $ * $Id: shrinkfish.c 1.5 Tue, 10 Dec 2002 05:58:59 +0100 kjwolf $ */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <openssl/blowfish.h> #include <openssl/sha.h> #include <zlib.h> typedef unsigned long long BIGGEST; #define MIN(a,b) (((a)<(b))?(a):(b)) #define MAXBYTES(n) MIN(sizeof(unsigned long long), n) #define CKSUM_LEN 2 /* you can change this! [bytes] */ #define UP(a,b) ((((a)+(b)-1)/(b))*(b)) #define IOERR(nm) { fprintf(stderr, "%s: %s: %s\n", argv[0], nm, strerror(errno)); \ return 8; } BIGGEST nicenumber(const char *us) { BIGGEST x=atoll(us); int i=strlen(us); if(i>0) switch(us[i-1]) { case 'k': x*=1000; break; case 'K': x*=1024; break; case 'm': x*=1000000L; break; case 'M': x*=1048576L; break; case 'g': x*=1000000000L; break; case 'G': x*=1073741824L; break; case 't': x*=1000000000000LL; break; case 'T': x*=1099511627776LL; break; } return x; } unsigned cksum(const unsigned char *buf, unsigned len) { unsigned i; unsigned s=0; for(i=0; i<len; i++,buf++) { register unsigned char j=i%(CKSUM_LEN*8); s^=(((unsigned)*buf)<<j)|(((unsigned)*buf)>>((CKSUM_LEN*8-1)-j)); } return s; } void u2b(BIGGEST x, unsigned char *buf, int bytes) { int n=MAXBYTES(bytes), i; switch(n) { case 8: buf[7]=(unsigned char)((x>>56)&0xff); case 7: buf[6]=(unsigned char)((x>>48)&0xff); case 6: buf[5]=(unsigned char)((x>>40)&0xff); case 5: buf[4]=(unsigned char)((x>>32)&0xff); case 4: buf[3]=(unsigned char)((x>>24)&0xff); case 3: buf[2]=(unsigned char)((x>>16)&0xff); case 2: buf[1]=(unsigned char)((x>>8)&0xff); case 1: buf[0]=(unsigned char)(x&0xff); } for(i=n+1; i<bytes; i++) /* if platform doesn't support that large */ buf[i]=0; /* variables, set rest to zero */ } BIGGEST b2u(const unsigned char *buf, int bytes) { BIGGEST x=0; switch(MAXBYTES(bytes)) { case 8: x|=((BIGGEST)buf[7])<<56; case 7: x|=((BIGGEST)buf[6])<<48; case 6: x|=((BIGGEST)buf[5])<<40; case 5: x|=((BIGGEST)buf[4])<<32; case 4: x|=((BIGGEST)buf[3])<<24; case 3: x|=((BIGGEST)buf[2])<<16; case 2: x|=((BIGGEST)buf[1])<<8; case 1: x|=((BIGGEST)buf[0]); } return x; /* Staenkefeld ist ueberall */ } long read_enforced(int fd, void *buf, long mincount, long maxcount) { long off=0, d; do { d=read(fd, buf+off, maxcount-off); if(d<0) { if(errno==EINTR) { usleep(1000); continue; } if(errno==EAGAIN) { usleep(10000); continue; } return -1; } if(!d) return off; else off+=d; } while(off<mincount); return off; } int main(int argc, char *argv[]) { int eflg=-1, zlev=6, pf=-1; unsigned bufsize=0x100000, bufsize2; char *buf, *buf2; char password[0x200]; BIGGEST pos=0; BF_KEY ky; password[0]=0; if(argc<2) { // $Format: " puts(\"shrinkfish Rel V$ProjectVersion$ (C)2002 by Klaus-J. Wolf <yanestra@web.de>\");"$ puts("shrinkfish Rel V0.6 (C)2002 by Klaus-J. Wolf <yanestra@web.de>"); printf("usage:\t%s [-p passstream] [-b blocksize] [-z compr_level] -e|-d\n", argv[0]); puts("\t\t[-P password]"); puts("options: -e encrypt; -d decrypt"); return 10; } for(;;) { int c=getopt(argc, argv, "b:edz:P:"); if(c==-1) break; switch(c) { case 'b': bufsize=nicenumber(optarg); break; case 'z': zlev=atoi(optarg); break; case 'e': eflg=1; break; case 'd': eflg=0; break; case 'p': pf=atoi(optarg); break; case 'P': strcpy(password, optarg); { char *p; for(p=optarg; *p; p++) *p='?'; } break; case '?': fprintf(stderr, "%s: unknown option -%c\n", argv[0], optopt); return 10; default: fprintf(stderr, "%s: unexpected option -%c\n", argv[0], c); return 10; } } if(eflg==-1) { fprintf(stderr, "%s: we would like to know what to do\n", argv[0]); return 10; } // bufsize=(bufsize+0xff)&~0xff; // bufsize2=bufsize+bufsize/100+32; bufsize=UP(bufsize,0x100); bufsize2=bufsize+bufsize/100+32; buf=malloc(bufsize2); if(!buf) { fprintf(stderr, "%s: we've run out of memory\n", argv[0]); return 9; } buf2=malloc(bufsize2); if(!buf2) { fprintf(stderr, "%s: we've run out of memory\n", argv[0]); return 9; } if(!password[0]) if(pf>=0) { FILE *f=fdopen(pf, "r"); int i; if(!f) { perror("``passstream''"); return 8; } fgets(password, sizeof password, f); fclose(f); i=strlen(password); if(i>0) if(password[i-1]=='\n') password[i-1]=0; } else for(;;) { strcpy(password, getpass("Password: ")); if(!strcmp(password, getpass("Again: "))) break; fputs("Password mismatch; please try again.\n", stderr); } { SHA_CTX sc; char kbuf[20]; SHA1_Init(&sc); SHA1_Update(&sc, password, strlen(password)); SHA1_Final(kbuf, &sc); BF_set_key(&ky, 20, kbuf); } for(;;) { unsigned i, i1; unsigned long uu, uu1; int j; if(!eflg) { unsigned k; i=read_enforced(0, buf, 4, 4); if(i==0) break; if(i<0) IOERR("``stdin''") i1=b2u(buf, 4); i=read_enforced(0, buf, i1, i1); if(i<=0) IOERR("``stdin''") for(k=0; k<i; k+=8) BF_ecb_encrypt(buf+k, buf2+k, &ky, 0); i1=UP(i,8); memcpy(buf, buf2, i1); } else { i=read_enforced(0, buf, bufsize, bufsize); if(i==0) break; if(i==-1) { fprintf(stderr, "%s: ignoring: ``stdin'': %s\n", argv[0], strerror(errno)); continue; } i1=UP(i,8); } //DEBUG fprintf(stderr, "pos=%llu size=%u\n", pos, i); uu=bufsize2; j=eflg?compress2(buf2, &uu, buf, i1, zlev): uncompress(buf2, &uu, buf, i1); switch(j) { case Z_OK: break; case Z_DATA_ERROR: fprintf(stderr, "%s: ignoring: block decompression status: @%llu: DATA_ERROR\n", argv[0], pos); continue; case Z_MEM_ERROR: fprintf(stderr, "%s: ignoring: block compression status: @%llu: MEM_ERROR\n", argv[0], pos); continue; case Z_BUF_ERROR: fprintf(stderr, "%s: ignoring: block compression status: @%llu: BUF_ERROR\n", argv[0], pos); continue; case Z_STREAM_ERROR: fprintf(stderr, "%s: ignoring: block compression status: @%llu: STREAM_ERROR\n", argv[0], pos); continue; default: fprintf(stderr, "%s: ignoring: unknown block compression status: @%llu: %d\n", argv[0], pos, j); continue; } if(eflg) { unsigned k; for(k=0; k<uu; k+=8) BF_ecb_encrypt(buf2+k, buf+k, &ky, 1); uu1=UP(uu,8); memcpy(buf2+4, buf, uu1); u2b(uu1, buf2, 4); uu1+=4; } else uu1=UP(uu,8); if(write(1, buf2, uu1)<0) IOERR("``stdout''") pos+=i; } return 0; }
评论
    相关推荐
    • UNIX UNLEASHED
      经典的UNIX教材 UNIX UNLEASHED Sams Development Team SAMS Publishibng 201 West 103rd Street, Indianapolis, IN 46290
    • Unix
      Unix
    • Unix Tutorial
      清华大学UNIX教程讲义PPT<br>适合初学者
    • Unix Tutorial
      清华大学UNIX教程讲义PPT<br>适合初学者
    • Unix时间戳
      Unix时间戳计算方法,看完就可以自己订制属于自己的VB时间戳了
    • UNIX
      UNIX课程幻灯片
    • Unix递归
      Unix递归 快速执行递归文件系统操作的功能。 $ stack run unix-recursive-bin -- ${path-to-directory}
    • lions unix
      莱昂氏unix源码pdf,或代码获取网址,及对应的英汉注释的书。下载,请评论。
    • unix unix
      unixunixunixunixunix
    • Unix
      Unix