encrypt.h encrypt.cpp

#ifndef ENCRYPT_H
#define ENCRYPT_H
#include "windows.h"
#include "stdio.h"
#include <string>
#include "windef.h"
class encrypt
{
public:
    encrypt();
    std::string GetOneMac(int AdapterIndex);
    std::string GetKey(char *sKey,char fill);
    std::string Gencrypt(std::string sIn, char fill, std::string key);
    void HexTostringEx(std::string from_text,char *chOut);
    std::string decrypt(std::string sIn,char fill,std::string key);
    //std::string StringToHex(std::string from_text);
    std::string strToHex(std::string str, std::string separator = "");
    long NumericPassword(std::string password);
    std::string Cipher(std::string password  , std::string from_text ) ;
    std::string Decipher(std::string password , std::string from_text );
    std::string EncryptNumber(int type,char *chSn,int iVal);
    std::string EncryptDWORD(int type,char *chSn,DWORD iVal);
    int  GetNumber(int type, char *chSn, std::string str);
    std::string EncryptString(int type,char *chSn,std::string str);
    std::string GetString(int type,char *chSn,std::string str);
    std::string CreateLicense(std::string chSerial, std::string chSn, int iType, int nTagNum, std::string chFunction, int dwDate);
};

#endif // ENCRYPT_H




#include "encrypt.h"

#include<iostream>
#include <sstream>
#include "AES.h"
#include <QFile>
#include <QDebug>
const int CRYPTOLEN = 16;
std::string key="ems@#$&^$34_";//长度为14,请不要修改,内容可以修改
encrypt::encrypt()
{

}

std::string encrypt::GetOneMac(int AdapterIndex)
{
    return "";
}

std::string encrypt::GetKey(char *sKey, char fill)
{
    int len = key.length();
    int i;
    sprintf(sKey, "%s", key.c_str());
    for ( i=len; i<16; ++i)
        sKey[i] = fill;
    return sKey;
}

std::string encrypt::Gencrypt(std::string sIn, char fill, std::string key)
{
    int i;
    int len = sIn.length();
    int ldCL = len / CRYPTOLEN;
    int lmCL = len % CRYPTOLEN;
    int slen = (ldCL+1)*CRYPTOLEN;
    char* pSrc = new char[slen+1];
    sprintf(pSrc, "%s", sIn.c_str());
    for ( i=len; i<slen+1; ++i)
        pSrc[i] = fill;

    std::string content;
    aes_ctx aes;
    gen_tabs();
    if(aes_set_key(&aes, (const u8*)key.c_str() , 16) != 0) {
        //	DRIVERTRACE(FILE_LOG_SYS, "can't set key : %s ", key.c_str());
    }
    const int num = slen/CRYPTOLEN;
    for ( i=0; i<num; ++i) {
        char *p = pSrc + (i<<4);
        char inbuf[CRYPTOLEN];
        char outbuf[CRYPTOLEN];
        memcpy(inbuf, p, CRYPTOLEN);
        memset(outbuf, 0, CRYPTOLEN);
        aes_encrypt(&aes,(u8*)outbuf,(u8*)inbuf);
        char crytemp[CRYPTOLEN*2+1];
        memset(crytemp, 0, CRYPTOLEN*2+1);
        for (int i=0; i<CRYPTOLEN; ++i) {
            char temp[3];
            sprintf(temp,  "%02x", (u8)outbuf[i]);
            strcat(crytemp,  temp);
        }
        content += crytemp;
    }
    delete [] pSrc;
    return content.c_str();
}

void encrypt::HexTostringEx(std::string from_text, char *chOut)
{
    char ch;
    int str_len =from_text.length();
    int a,b;
    std::string strR;
    char  str[12];
    for(int i=0;i<str_len;i=i+2)
    {

        ch=from_text[i];
        ch=toupper(ch);
        if(ch>='A')
            a=10+ch-'A';
        else a=ch-'0';

        ch=from_text[i+1];
        ch=toupper(ch);
        if(ch>='A')
            b=10+ch-'A';
        else b=ch-'0';

        chOut[i/2]=a*16+b;
    }
}

std::string encrypt::decrypt(std::string sIn, char fill, std::string key)
{
    int i;
    std::string content;
    int len = sIn.length()/2;
    char* pSrc = new char[len+1];
    HexTostringEx(sIn,pSrc);
    aes_ctx aes;
    gen_tabs();
    if(aes_set_key(&aes, (const u8*)key.c_str() , 16) != 0) {
        //	DRIVERTRACE(FILE_LOG_SYS, "can't set key : %s ", key.c_str());
    }
    std::string  strReturn;
    const int num = len/CRYPTOLEN;
    for ( i=0; i<num; ++i) {
        char *p = pSrc + (i<<4);
        char inbuf[CRYPTOLEN];
        char outbuf[CRYPTOLEN+1];
        memcpy(inbuf, p, CRYPTOLEN);
        memset(outbuf, 0, CRYPTOLEN+1);
        aes_decrypt(&aes,(u8*)outbuf,(u8*)inbuf);
        strReturn.append(outbuf);
    }
    delete [] pSrc;

    strReturn.erase(strReturn.find_last_not_of(fill)+1);
    return strReturn;
}

//字符串转换成16进制
std::string encrypt::strToHex(std::string str, std::string separator)
{
    const std::string hex = "0123456789ABCDEF";
    std::stringstream ss;

    for (std::string::size_type i = 0; i < str.size(); ++i)
        ss << hex[(unsigned char)str[i] >> 4] << hex[(unsigned char)str[i] & 0xf] << separator;

    return ss.str();
}

long encrypt::NumericPassword(std::string password)
{
    long value=0 ;
    int ch=0 ;
    long shift1=0 ;
    long shift2=0 ;


    int  str_len = password.length();
    for(int i=0;i< str_len;i++)
    {

        // Add the next letter.
        ch = password.at(i);
        value = value ^ int((ch * pow(2 , shift1)));
        value = value ^ int((ch * pow(2 , shift2)));

        // Change the shift offsets.
        shift1 = (shift1 + 7) % 19;
        shift2 = (shift2 + 13) % 23;
    }
    return value;
}
//字符串加密
std::string encrypt::Cipher(std::string password, std::string from_text)
{
    const int MIN_ASC = 32 ;
    const int MAX_ASC = 126 ;
    const int NUM_ASC = MAX_ASC - MIN_ASC + 1;

    long offset ;
    int str_len;

    int ch;
    std::string strR;
    password = strToHex(password);
    from_text = strToHex(from_text);
    offset = NumericPassword(password);


    srand(offset);


    str_len = from_text.length();

    for(int i=0;i< str_len;i++)
    {
        ch = from_text.at(i);
        if( ch >= MIN_ASC && ch <= MAX_ASC )
        {
            ch = ch - MIN_ASC;
            offset = int(rand()%(NUM_ASC+1 ) );
            ch = ((ch + offset) % NUM_ASC);
            ch = ch + MIN_ASC;
            strR += char(ch);
        }
    }

    return strR;
}
//字符串解密
std::string encrypt::Decipher(std::string password, std::string from_text)
{
    const int MIN_ASC = 32 ;
    const int MAX_ASC = 126 ;
    const int NUM_ASC = MAX_ASC - MIN_ASC + 1;

    long offset ;
    int str_len;

    int ch;
    std::string strR;
    password = strToHex(password);

    offset = NumericPassword(password);
    srand(offset);
    str_len = from_text.length();
    for(int i=0;i< str_len;i++)
    {
        ch = from_text.at(i);
        if( ch >= MIN_ASC && ch <= MAX_ASC )
        {
            ch = ch - MIN_ASC;
            offset = int(rand()%(NUM_ASC+1 ) );
            ch = ((ch - offset) % NUM_ASC);
            if( ch < 0 ) ch = ch + NUM_ASC;
            ch = ch + MIN_ASC;
            strR += char(ch);
        }
    }
    return strR;
}
//对数字加密
std::string encrypt::EncryptNumber(int type, char *chSn, int iVal)
{
    std::string str;
    str= std::to_string(iVal);
    return Cipher(chSn,str);
}

std::string encrypt::EncryptDWORD(int type, char *chSn, DWORD iVal)
{
    std::string str = std::to_string(long long (iVal));
    return Cipher(chSn,str);
}
//对数字解密
int encrypt::GetNumber(int type, char *chSn, std::string str)
{
    std::string str1=Decipher(chSn,str);
    return atoi(str1.c_str());
}
//对字符串加密
std::string encrypt::EncryptString(int type, char *chSn, std::string str)
{
    return Cipher(chSn,str);
}

std::string encrypt::GetString(int type, char *chSn, std::string str)
{
    return Decipher(chSn,str);
}

std::string encrypt::CreateLicense(std::string chSerial,std::string chSn,int iType,int nTagNum,std::string chFunction,
                            int dwDate)
{
    char chKey[100];
    char chNote[100];
    std::string content,str;
    std::string sFileNote;
    //0
    content.append(chSerial);
    content.append( ",");
    //1加密  iType
    sprintf(chNote,"%d",iType);
    GetKey(chKey,0x30+1);
    str=Gencrypt(chNote,'@',chKey);
    content.append(str);
    content.append(",");

    //2加密  nTagNum
    sprintf(chNote,"%ld",nTagNum);
    GetKey(chKey,0x30+2);
    str=Gencrypt(chNote,'@',chKey);
    content.append(str);
    content.append(",");

    //3加密  chFunction
    GetKey(chKey,0x30+3);
    str=Gencrypt(chFunction,'@',chKey);
    content.append(str);
    content.append(",");

    //4加密  chDate
    GetKey(chKey,0x30+4);
    sprintf(chNote,"%ld",dwDate);
    str=Gencrypt(chNote,'@',chKey);
    content.append(str);
    content.append(",");

    //加密  chSn
    GetKey(chKey,0x30+5);
    str=Gencrypt(chSn,'@',chKey);
    content.append(str);
    content.append(",");

    GetKey(chKey,0x30+9);
    sFileNote=Gencrypt(content,'@',chKey);
    return sFileNote;
}





posted on 2019-07-05 16:15  码农er  阅读(515)  评论(0)    收藏  举报