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

📄 goozo_crypt.cpp

📁 该压缩包中包括 tom的加密函数库及pdf说明 ,以及Rinick s ECC:椭圆曲线非对称加密密钥生成器
💻 CPP
字号:
//---------------------------------------------------------------------------




#include "goozo_crypt.h"


//---------------------------------------------------------------------------
ecc_key ltc_ecc_mykey;

void ltc_eax_encrypt(const struct ltc_cipher_descriptor *cipher,
              AnsiString key,AnsiString nonce,
              const unsigned char *pt,
              unsigned char *ct, unsigned length){
    int cn=register_cipher(cipher);
    if(cn==-1)throw Exception("wrong cipher");

    unsigned char tkey[32]={0};
    unsigned char tnonce[32]={0};
    memcpy(tkey,key.c_str(),key.Length());
    memcpy(tnonce,nonce.c_str(),nonce.Length());

    eax_state eax;
    eax_init( &eax,cn,tkey,32,tnonce,32,NULL,0);
    eax_encrypt(&eax, pt, ct, length);
}
//---------------------------------------------------------------------------
void ltc_eax_decrypt(const struct ltc_cipher_descriptor *cipher,
              AnsiString key,AnsiString nonce,
              const unsigned char *ct,
              unsigned char *pt, unsigned length){
    int cn=register_cipher(cipher);
    if(cn==-1)throw Exception("wrong cipher");

    unsigned char tkey[32]={0};
    unsigned char tnonce[32]={0};
    memcpy(tkey,key.c_str(),key.Length());
    memcpy(tnonce,nonce.c_str(),nonce.Length());

    eax_state eax;
    eax_init( &eax,cn,tkey,32,tnonce,32,NULL,0);
    eax_decrypt(&eax, ct, pt, length);
}
//---------------------------------------------------------------------------
AnsiString ltc_hash_mem(const struct ltc_hash_descriptor *hash,
              const unsigned char *in,  unsigned long length){
    int hn=register_hash(hash);
    if(hn==-1)throw Exception("wrong hash");

    unsigned char out[64];
    unsigned long outlen;
    hash_memory(hn,in,length,out,&outlen);

    AnsiString returnstr;
    for(int i=0;i<outlen;++i)
        returnstr.cat_printf("%02x",out[i]);
    return returnstr;
}
//---------------------------------------------------------------------------
AnsiString ltc_hash_file(const struct ltc_hash_descriptor *hash,
              AnsiString filename){
    int hn=register_hash(hash);
    if(hn==-1)throw Exception("wrong hash");

    unsigned char out[64];
    unsigned long outlen;
    hash_file(hn,filename.c_str(),out,&outlen);

    AnsiString returnstr;
    for(int i=0;i<outlen;++i)
        returnstr.cat_printf("%02x",out[i]);
    return returnstr;
}

//---------------------------------------------------------------------------

int ltc_ecc_encrypt(const unsigned char *pt, unsigned long inlen,
              unsigned char *ct){

    if(ltc_ecc_mykey.pubkey.x.used==0)
        throw Exception("Please include your private key");

    if(ecc_sets[ltc_ecc_mykey.idx].size<=inlen)
        throw Exception("inlen too long");

    int pn=register_prng(&fortuna_desc);
    prng_state tg;
    int getrand;
    prng_descriptor[pn].start(&tg);
        for(int i=0;i<3;++i){
            srand(time(NULL));
            for(int j=0;j<8;++j){
                getrand=rand();
                prng_descriptor[pn].add_entropy((char*)&getrand,2,&tg);
            }
        }

    prng_descriptor[pn].ready(&tg);

    unsigned long outlen=138;
    ecc_private_encrypt(pt,inlen,ct,&outlen,&tg, pn, &ltc_ecc_mykey);


    return outlen;
}
//---------------------------------------------------------------------------
void ltc_ecc_decrypt(const unsigned char *ct,unsigned long inlen,
              unsigned char *pt,unsigned long outlen){
    if(ltc_ecc_mykey.pubkey.x.used==0)
        throw Exception("Please include your private key");

    if(ecc_sets[ltc_ecc_mykey.idx].size<=outlen)
        throw Exception("outlen too long");

   unsigned long resultlen=65;//输出的最大长度

   unsigned char tt[65];
	 ecc_public_decrypt(ct,inlen,tt,&resultlen, &ltc_ecc_mykey);
     if(resultlen<outlen){
        int dlen=outlen-resultlen;
        memcpy(pt+dlen,tt,resultlen);
        memset(pt,0,dlen);
     }else{
        memcpy(pt,tt,outlen);
     }

}

⌨️ 快捷键说明

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