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

📄 crypto.c.txt

📁 当今
💻 TXT
📖 第 1 页 / 共 2 页
字号:
        following that is 16 bits of "key check" material, which is a 

  

  

        duplicate of the last 2 bytes of the random prefix.  Encrypted key 

  

  

        check bytes detect if correct IDEA key was used to decrypt ciphertext. 

  

  

*/ 

  

  

{       int status = 0; 

  

  

        FILE *f; 

  

  

  

  

  

        /* init CFB key */ 

  

  

        fill0(iv,sizeof(*iv) * 4); /* define initialization vector IV as 0 */ 

  

  

        initcfb_idea(Z,iv,ideakey,decryp); 

  

  

  

  

  

        if (!decryp)    /* encrypt-- insert key check bytes */ 

  

  

        {       /* There is a random prefix followed by 2 key check bytes */ 

  

  

                int     i; 

  

  

  

  

  

                for (i=0; i<RAND_PREFIX_LENGTH; ++i) 

  

  

                                buf[i] = randombyte(); 

  

  

                /* key check bytes are simply duplicates of final 2 random bytes 

*/ 

  

  

                buf[i] = buf[i-2];      /* key check bytes for redundancy */ 

  

  

                buf[i+1] = buf[i-1]; 

  

  

  

  

  

                ideacfb(buf,RAND_PREFIX_LENGTH+2, Z, iv, decryp); 

  

  

                return 0; 

  

  

        } 

  

  

        else    /* decrypt-- check for key check bytes */ 

  

  

        {       /* See if the redundancy is present after the random prefix */ 

  

  

                ideacfb(buf,RAND_PREFIX_LENGTH+2, Z, iv, decryp); 

  

  

                if ((buf[RAND_PREFIX_LENGTH] != buf[RAND_PREFIX_LENGTH-2]) 

  

  

                        || (buf[RAND_PREFIX_LENGTH+1] != buf[RAND_PREFIX_LENGTH- 

])) 

  

  

                        return(-2);             /* bad key error */ 

  

  

                return 0; 

  

  

        } 

  

  

} 

  

  

  

  

  

#if 0 

  

  

int test() 

  

  

{ 

  

  

        byte ideakey[16]; 

  

  

        byte passphrase[256]; 

  

  

        extern char password[]; 

  

  

        FILE*   f; 

  

  

        FILE*   f1; 

  

  

        int i; 

  

  

  

  

  

        if (GetHashedPassPhrase((char *)passphrase,(char *)ideakey,NOECHO2) <= 0 

  

  

  

        { 

  

  

                return(-1); 

  

  

        } 

  

  

  

  

  

        f = fopen("t", "r"); 

  

  

        f1 = fopen("t1", "w"); 

  

  

        /* Now compress the plaintext and encrypt it with IDEA... */ 

  

  

        init_idea_stream( ideakey, ENCRYPT_IT ); 

  

  

        for (i = 0 ; i < RAND_PREFIX_LENGTH + 2 ; i++) 

  

  

                putc(buf[i], f1); 

  

  

  

  

  

        while(!feof(f)) 

  

  

        { 

  

  

                int c; 

  

  

                char c1; 

  

  

                c = getc(f); 

  

  

                c1 = c; 

  

  

                if (c < 0) break; 

  

  

                idea_stream(&c1); 

  

  

                putc(c, f1); 

  

  

        } 

  

  

  

  

  

        burn(passphrase); 

  

  

  

  

  

        close_idea_stream(); 

  

  

        return 0; 

  

  

}       /* idea_encryptfile */ 

  

  

  

  

  

int test1() 

  

  

{ 

  

  

        byte ideakey[16]; 

  

  

        byte passphrase[256]; 

  

  

        extern char password[]; 

  

  

        FILE*   f; 

  

  

        FILE*   f1; 

  

  

        int i; 

  

  

  

  

  

        if (GetHashedPassPhrase((char *)passphrase,(char *)ideakey,NOECHO2) <= 0 

  

  

  

        { 

  

  

                return(-1); 

  

  

        } 

  

  

  

  

  

        /* Now compress the plaintext and encrypt it with IDEA... */ 

  

  

        f = fopen("t1", "r"); 

  

  

        f1 = fopen("t2", "w"); 

  

  

        for (i = 0 ; i < RAND_PREFIX_LENGTH + 2 ; i++) 

  

  

                buf[i] = getc(f); 

  

  

  

  

  

        if (init_idea_stream( ideakey, DECRYPT_IT) < 0) 

  

  

                abort(); 

  

  

  

  

  

        while(!feof(f)) 

  

  

        { 

  

  

                int c; 

  

  

                char c1; 

  

  

                c = getc(f); 

  

  

                c1 = c; 

  

  

                if (c < 0) break; 

  

  

                idea_stream(&c); 

  

  

                putc(c, f1); 

  

  

        } 

  

  

  

  

  

        burn(passphrase); 

  

  

  

  

  

        close_idea_stream(); 

  

  

        return 0; 

  

  

}       /* idea_encryptfile */ 

  

  

#endif 

  

  

  

  

  

  

  

  

idea_stream(byte* ch, IDEAkey Z, word16 iv[4], boolean decryp) 

  

  

{ 

  

  

        ideacfb(ch, 1, Z, iv, decryp); 

  

  

        return 0; 

  

  

} 

  

  

  

  

  

close_idea_stream(IDEAkey Z, word16 iv[4]) 

  

  

{ 

  

  

        close_idea(Z);  /* Clean up data structures */ 

  

  

        burn(iv);               /* burn sensitive data */ 

  

  

        return(0);      /* should always take normal return */ 

  

  

}       /* idea_file */ 

  

  

  

  

  

long timeshift = 0; 

  

  

  

  

  

byte moduli_ext[] = 

  

  

{0x2, 0x1, 0x1, 0xc1, 0xff, 0x9d, 0x1a, 0x49, 0x1c, 0xb4, 0xf8, 

  

  

 0x27, 0x29, 0xb8, 0xb4, 0x9d, 0xa9, 0xa, 0xd6, 0x59, 0xd5, 0xb8, 

  

  

 0x1a, 0xcc, 0x1c, 0x40, 0x9f, 0xad, 0x1a, 0xf5, 0xbe, 0x8c, 0xe6, 

  

  

 0x9d, 0x4e, 0x44, 0xe4, 0xb7, 0x6e, 0x95, 0x5d, 0x76, 0x61, 0x79, 

  

  

 0x6b, 0xae, 0x9c, 0xd5, 0x75, 0x2a, 0x80, 0xba, 0x3b, 0xe2, 0xb4, 

  

  

 0x5e, 0xca, 0x91, 0x54, 0xe9, 0x70, 0x5e, 0x8e, 0x8a, 0xf7, 0x5f, 

  

  

 0x5f}; 

  

  

  

  

  

unit moduli[MAX_UNIT_PRECISION];        /* q */ 

  

  

unit agreedkey[MAX_UNIT_PRECISION];     /* session key */ 

  

  

unit publickey[MAX_UNIT_PRECISION];     /* my public key */ 

  

  

unit privatekey[MAX_UNIT_PRECISION];    /* my private key */ 

  

  

byte buf[MAX_BYTE_PRECISION];           /* Temporary static area */ 

  

  

  

  

  

unit two[MAX_UNIT_PRECISION];           /* The number 2 */ 

  

  

  

  

  

static unit randomunit(void) 

  

  

        /* Fills 1 unit with random bytes, and returns unit. */ 

  

  

{       unit u = 0; 

  

  

        byte i; 

  

  

        static int already_complained = 0; 

  

  

        i = BYTES_PER_UNIT; 

  

  

        if (strong_pseudorandom((byte*)&u, i) < 0) 

  

  

        { 

  

  

            if (!already_complained) 

  

  

            { 

  

  

                panic("No random seed file (normal for first run)."); 

  

  

                already_complained = 1; 

  

  

            } 

  

  

            do 

  

  

                u = (u << 8) + randombyte(); 

  

  

            while (--i != 0); 

  

  

        } 

  

  

        return(u); 

  

  

}       /* randomunit */ 

  

  

  

  

  

static void randombits(unitptr p, short nbits) 

  

  

/*      Make a random unit array p with nbits of precision.  Used mainly to 

  

  

        generate large random numbers to search for primes. 

  

  

*/ 

  

  

{       /* Fill a unit array with exactly nbits of random bits... */ 

  

  

        short nunits;   /* units of precision */ 

  

  

        mp_init(p,0); 

  

  

        nunits = bits2units(nbits);     /* round up to units */ 

  

  

        make_lsbptr(p,global_precision); 

  

  

        *p = randomunit(); 

  

  

        while (--nunits) 

  

  

        {       *pre_higherunit(p) = randomunit(); 

  

  

                nbits -= UNITSIZE; 

  

  

        } 

  

  

        *p &= (power_of_2(nbits)-1); /* clear the top unused bits remaining */ 

  

  

}       /* randombits */ 

  

  

  

  

  

void 

  

  

init_crypto() 

  

  

{ 

  

  

    set_precision(MAX_UNIT_PRECISION); 

  

  

    mp_init(two, 2); 

  

  

    mpi2reg(moduli, moduli_ext); 

  

  

    /*mp_display("moduli", moduli);*/ 

  

  

} 

  

  

  

  

  

static int have_key = 0; 

  

  

  

  

  

  

  

  

int 

  

  

createDHkey() 

  

  

{ 

  

  

    if (!have_key) 

  

  

    { 

  

  

        randombits(privatekey, 510); 

  

  

        mp_modexp(publickey, two, privatekey, moduli); 

  

  

    } 

  

  

} 

  

  

  

  

  

int 

  

  

create_getDHpublic(byte* ptr) 

  

  

{ 

  

  

    createDHkey(); 

  

  

    have_key = 0; 

  

  

  

  

  

    /*mp_display("privatekey", privatekey);*/ 

  

  

    /*mp_display("publickey", publickey);*/ 

  

  

    /*mp_display("two", two);*/ 

  

  

  

  

  

    return reg2mpi(ptr, publickey) + 2; 

  

  

} 

  

  

  

  

  

int 

  

  

getDHpublic(byte* ptr) 

  

  

{ 

  

  

    have_key = 0; 

  

  

    return reg2mpi(ptr, publickey) + 2; 

  

  

} 

  

  

  

  

  

void 

  

  

DHprepare() 

  

  

{ 

  

  

    if (!have_key) 

  

  

    { 

  

  

        randombits(privatekey, 510); 

  

  

        mp_modexp(publickey, two, privatekey, moduli); 

  

  

        have_key = 1; 

  

  

    } 

  

  

} 

  

  

  

  

  

int 

  

  

computeDHagreed_key(unit* key, byte** ptr) 

  

  

{ 

  

  

    int len; 

  

  

  

  

  

    /*mp_display("theirkey", key);*/ 

  

  

    mp_modexp(agreedkey, key, privatekey, moduli); 

  

  

    /*mp_display("agreedkey", agreedkey);*/ 

  

  

    len = reg2mpi(buf, agreedkey); 

  

  

    *ptr = buf + 2; 

  

  

    return len; 

  

  

} 

  

  

  

  

  

void 

  

  

DHburn() 

  

  

{ 

  

  

    mp_burn(privatekey); 

  

  

    burn(buf); 

  

  

    mp_burn(agreedkey); 

  

  

} 

  

  

  

-- 

              NT? 还行,就是满身的的补丁让人心有余悸。 

              Solaris?不错,就是动不动要License. 



              Linux?好样的! 

  

⌨️ 快捷键说明

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