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

📄 rsa src.txt

📁 当今
💻 TXT
📖 第 1 页 / 共 2 页
字号:
                                  BigInt e, 

                                  BigInt randomStart) 

#endif 

{ 

        BigInt p, q, ignore, r1, r2; 

        Key_exps *exps; 

        RSAKeySet *key_set; 

        int oldlen; 

        BigInt randStart; 

  

        p = bigInit(0); 

        q = bigInit(0); 

        r1 = NULL; 

        r2 = NULL; 

        randStart = NULL; 

        if (randomStart != NULL) { 

                r1 = bigInit(0); 

                r2 = bigInit(0); 

                randStart = bigInit(0); 

                bigCopy(randomStart, randStart); 

                oldlen = LENGTH(randStart); 

                LENGTH(randStart) = nbits/32/2; 

                bigCopy(randStart, r1); 

                LENGTH(randStart) = oldlen; 

                bigRightShift(randStart, nbits/2, randStart); 

                oldlen = LENGTH(randStart); 

                LENGTH(randStart) = nbits/32/2; 

                bigCopy(randStart, r2); 

                LENGTH(randStart) = oldlen; 

                bigRightShift(randStart, nbits/2, randStart); 

        } 

        if (ebits == 2) 

                genPrimesFor3(nbits, p, q, r1, r2); 

  

        else { 

                ignore = bigInit(0); 

                genStrongPrimeSet(nbits/2, p, (int)NULL, ignore, GORDON, r1); 

                genStrongPrimeSet(nbits/2, q, (int)NULL, ignore, GORDON, r2); 

                freeBignum(ignore); 

        } 

        exps = genKeyExps(p, q, e, ebits, randStart); 

        key_set = buildRSAKeySet(exps->e, exps->d, p, q); 

        freeBignum(exps->e); 

        freeBignum(exps->d); 

        if (r1 != NULL) { 

                freeBignum(r1); 

                freeBignum(r2); 

                freeBignum(randStart); 

        } 

#ifdef DLLEXPORT 

        GlobalUnlock(exps->exp_handle); 

        GlobalFree(exps->exp_handle); 

#else 

        free((char *)exps); 

#endif 

        return key_set; 

} 

  

  

/* 

   Chinese Remainder Theorem reconstruction of m^d mod n, using 

   m^dp mod p and m^dq mod q with dp = d mod p-1, dq = d mod q-1. 

   */ 

#ifdef K_AND_R 

static void 

chineseRemTheorem(m, key, em) 

  BigInt m, em; 

  RSAPrivateKey *key; 

#else 

  static void chineseRemTheorem(BigInt m, 

                                RSAPrivateKey *key, 

                                BigInt em) 

#endif 

{ 

        BigInt u1, u2; 

        BigInt p, q, dp, dq, c12; 

  

        p = key->crt->p; 

        q = key->crt->q; 

        dp = key->crt->dp; 

        dq = key->crt->dq; 

        c12 = key->crt->c12; 

  

        u1 = bigInit(0); 

        u2 = bigInit(0); 

  

        bigPow(m, dp, p, u1); 

        bigPow(m, dq, q, u2); 

  

        crtCombine(u1, u2, p, q, c12, em); 

  

        freeBignum(u1); 

        freeBignum(u2); 

  

} 

  

#ifdef K_AND_R 

_TYPE( void ) 

freeRSAPublicKey(pk) 

  RSAPublicKey *pk; 

#else 

_TYPE( void ) freeRSAPublicKey(RSAPublicKey *pk) 

#endif 

{ 

        freeBignum(pk->publicExponent); 

        freeBignum(pk->modulus); 

#ifdef DLLEXPORT 

        GlobalUnlock(pk->pubkey_handle); 

        GlobalFree(pk->pubkey_handle); 

#else 

        free((char *)pk); 

#endif 

} 

  

#ifdef K_AND_R 

_TYPE( void ) 

freeRSAPrivateKey(pk) 

  RSAPrivateKey *pk; 

#else 

_TYPE( void ) freeRSAPrivateKey(RSAPrivateKey *pk) 

#endif 

{ 

        freeBignum(pk->publicExponent); 

        freeBignum(pk->privateExponent); 

        freeBignum(pk->modulus); 

        freeBignum(pk->crt->p); 

        freeBignum(pk->crt->q); 

        freeBignum(pk->crt->dp); 

        freeBignum(pk->crt->dq); 

        freeBignum(pk->crt->c12); 

#ifdef DLLEXPORT 

        GlobalUnlock(pk->crt->crt_handle); 

        GlobalFree(pk->crt->crt_handle); 

        GlobalUnlock(pk->privkey_handle); 

        GlobalFree(pk->privkey_handle); 

#else 

        free((char *)pk->crt); 

        free((char *)pk); 

#endif 

} 

  

#ifdef K_AND_R 

_TYPE( void ) 

freeRSAKeys(ks) 

  RSAKeySet *ks; 

#else 

_TYPE( void ) freeRSAKeys(RSAKeySet *ks) 

#endif 

{ 

  

        freeRSAPublicKey(ks->publicKey); 

        freeRSAPrivateKey(ks->privateKey); 

#ifdef DLLEXPORT 

        GlobalUnlock(ks->keyset_handle); 

        GlobalFree(ks->keyset_handle); 

#else 

        free((char *)ks); 

#endif 

} 

  

#ifdef K_AND_R 

_TYPE( BigInt ) 

RSAEncrypt(message, key) 

  BigInt message; 

  RSAPublicKey *key; 

#else 

_TYPE( BigInt ) RSAEncrypt(BigInt message, 

                           RSAPublicKey *key) 

#endif 

{ 

        BigInt result; 

  

        result = bigInit(3); 

        if (bigCompare(key->publicExponent, result) == 0) { 

                reset_big(result, 0); 

                bigCube(message, key->modulus, result); 

        } 

        else { 

                reset_big(result, 0); 

                bigPow(message, key->publicExponent, key->modulus, result); 

        } 

        return result; 

} 

  

#ifdef K_AND_R 

_TYPE( BigInt ) 

RSADecrypt(message, key) 

  BigInt message; 

  RSAPrivateKey *key; 

#else 

_TYPE( BigInt ) RSADecrypt(BigInt message, 

                           RSAPrivateKey *key) 

#endif 

{ 

        BigInt result; 

  

        result = bigInit(0); 

  

        chineseRemTheorem(message, key, result); 

        return result; 

  

} 

  

  

#ifdef K_AND_R 

_TYPE( RSASignature * ) 

RSASign(message, key) 

  BigInt message; 

  RSAPrivateKey *key; 

#else 

_TYPE( RSASignature * ) RSASign(BigInt message, 

                                RSAPrivateKey *key) 

#endif 

{ 

        return (RSASignature *)RSADecrypt(message, key); 

} 

  

  

#ifdef K_AND_R 

_TYPE( Boolean ) 

RSAVerify(message, sig, key) 

  BigInt message; 

  RSASignature *sig; 

  RSAPublicKey *key; 

#else 

_TYPE( Boolean ) RSAVerify(BigInt message, 

                           RSASignature *sig, 

                           RSAPublicKey *key) 

#endif 

{ 

        Boolean retval; 

        BigInt cmp; 

  

        cmp = (BigInt)RSAEncrypt((BigInt)sig, key); 

  

        if (bigCompare(message, cmp) == 0) 

                retval = TRUE; 

        else 

                retval = FALSE; 

  

        freeBignum(cmp); 

  

        return retval; 

} 

  

#ifdef K_AND_R 

_TYPE( void ) 

freeRSASig(sig) 

  RSASignature *sig; 

#else 

_TYPE( void ) freeRSASig(RSASignature *sig) 

#endif 

{ 

        freeBignum((BigInt)sig); 

} 

  

#ifdef K_AND_R 

_TYPE( void ) 

RSAPrivateKeyDesEncrypt(pk, deskey) 

  RSAPrivateKey *pk; 

  unsigned char *deskey; 

#else 

_TYPE( void ) 

RSAPrivateKeyDesEncrypt(RSAPrivateKey *pk, unsigned char *deskey) 

#endif 

{ 

        bignumDesEncrypt(pk->publicExponent, deskey); 

        bignumDesEncrypt(pk->privateExponent, deskey); 

        bignumDesEncrypt(pk->modulus, deskey); 

        bignumDesEncrypt(pk->crt->p, deskey); 

        bignumDesEncrypt(pk->crt->q, deskey); 

        bignumDesEncrypt(pk->crt->dp, deskey); 

        bignumDesEncrypt(pk->crt->dq, deskey); 

        bignumDesEncrypt(pk->crt->c12, deskey); 

} 

  

#ifdef K_AND_R 

_TYPE( void ) 

RSAPrivateKeyDesDecrypt(pk, deskey) 

  RSAPrivateKey *pk; 

  unsigned char *deskey; 

#else 

_TYPE( void ) 

RSAPrivateKeyDesDecrypt(RSAPrivateKey *pk, unsigned char *deskey) 

#endif 

{ 

        bignumDesDecrypt(pk->publicExponent, deskey); 

        bignumDesDecrypt(pk->privateExponent, deskey); 

        bignumDesDecrypt(pk->modulus, deskey); 

        bignumDesDecrypt(pk->crt->p, deskey); 

        bignumDesDecrypt(pk->crt->q, deskey); 

        bignumDesDecrypt(pk->crt->dp, deskey); 

        bignumDesDecrypt(pk->crt->dq, deskey); 

        bignumDesDecrypt(pk->crt->c12, deskey); 

} 

  

#ifdef K_AND_R 

_TYPE( BigInt ) 

quantized_RSADecrypt(m, key) 

  BigInt m; 

  RSAPrivateKey *key; 

#else 

_TYPE( BigInt ) 

quantized_RSADecrypt(BigInt m, RSAPrivateKey *key) 

#endif 

{ 

        BigInt result; 

  

        start_quantize(STD_QUANTUM); 

        result = RSADecrypt(m, key); 

        end_quantize(); 

  

        return result; 

} 

  

  

#ifdef K_AND_R 

_TYPE( RSASignature *) 

quantized_RSASign(m, key) 

  BigInt m; 

  RSAPrivateKey *key; 

#else 



_TYPE( RSASignature *) 

quantized_RSASign(BigInt m, RSAPrivateKey *key) 

#endif 

{ 

        return (RSASignature *)quantized_RSADecrypt(m, key); 

} 

  

  

  

-- 

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

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

              Linux?好样的! 

  

⌨️ 快捷键说明

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