Exemplo n.º 1
0
static int PasswordVerifier(unsigned char *key)
{
    unsigned char decryptedVerifier[16];
    AES_KEY akey;
    SHA_CTX ctx;
    unsigned char checkHash[20];
    unsigned char decryptedVerifierHash[32];

    memset(&akey, 0, sizeof(AES_KEY));
    if(AES_set_decrypt_key(key, 128, &akey) < 0) {
        fprintf(stderr, "AES_set_decrypt_key failed!\n");
        return 0;
    }
    AES_ecb_encrypt(cur_salt->encryptedVerifier, decryptedVerifier, &akey, AES_DECRYPT);
    memset(&akey, 0, sizeof(AES_KEY));
    if(AES_set_decrypt_key(key, 128, &akey) < 0) {
        fprintf(stderr, "AES_set_decrypt_key failed!\n");
        return 0;
    }
    AES_ecb_encrypt(cur_salt->encryptedVerifierHash, decryptedVerifierHash, &akey, AES_DECRYPT);
    AES_ecb_encrypt(cur_salt->encryptedVerifierHash+16, decryptedVerifierHash+16, &akey, AES_DECRYPT);

    /* find SHA1 hash of decryptedVerifier */
    SHA1_Init(&ctx);
    SHA1_Update(&ctx, decryptedVerifier, 16);
    SHA1_Final(checkHash, &ctx);

    return !memcmp(checkHash, decryptedVerifierHash, 16);
}
Exemplo n.º 2
0
/* Decrypt using selected mode of operation */
unsign32 AES_decrypt(aes *a,char *buff)
{
    int j,bytes;
    char st[16];
    unsign32 fell_off;

   /* Supported modes of operation */
    fell_off=0;
    switch (a->mode)
    {
    case ECB:
        AES_ecb_decrypt(a,(uchar *)buff);
        return 0;
    case CBC:
        for (j=0;j<4*NB;j++)
        {
            st[j]=a->f[j];
            a->f[j]=buff[j];
        }
        AES_ecb_decrypt(a,(uchar *)buff);
        for (j=0;j<4*NB;j++)
        {
            buff[j]^=st[j];
            st[j]=0;
        }
        return 0;
    case CFB1:
    case CFB2:
    case CFB4:
        bytes=a->mode-CFB1+1;
        for (j=0;j<bytes;j++) fell_off=(fell_off<<8)|a->f[j];
        for (j=0;j<4*NB;j++) st[j]=a->f[j];
        for (j=bytes;j<4*NB;j++) a->f[j-bytes]=a->f[j];
        AES_ecb_encrypt(a,(uchar *)st);
        for (j=0;j<bytes;j++)
        {
            a->f[16-bytes+j]=buff[j];
            buff[j]^=st[j];
        }
        return fell_off;
    case OFB1:
    case OFB2:
    case OFB4:
    case OFB8:
    case OFB16:
        bytes=a->mode-OFB1+1;
        AES_ecb_encrypt(a,(uchar *)(a->f));
        for (j=0;j<bytes;j++) buff[j]^=a->f[j];
        return 0;


    default:
        return 0;
    }
}
Exemplo n.º 3
0
/* Initialize GCM mode */
void GCM_init(gcm* g,char *key,int niv,char *iv)
{ /* iv size niv is usually 12 bytes (96 bits). AES key is 16 bytes */
	int i;
	uchar H[16];
	for (i=0;i<16;i++) {H[i]=0; g->stateX[i]=0;}

	AES_init(&(g->a),ECB,key,iv);
	AES_ecb_encrypt(&(g->a),H);     /* E(K,0) */
	precompute(g,H);
	
	g->lenA[0]=g->lenC[0]=g->lenA[1]=g->lenC[1]=0;
	if (niv==12)
	{
		for (i=0;i<12;i++) g->a.f[i]=iv[i];
		unpack((unsign32)1,(uchar *)&(g->a.f[12]));  /* initialise IV */
		for (i=0;i<16;i++) g->Y_0[i]=g->a.f[i];
	}
	else
	{
		g->status=GCM_ACCEPTING_CIPHER;
		GCM_ghash(g,iv,niv); /* GHASH(H,0,IV) */
		GCM_wrap(g);
		for (i=0;i<16;i++) {g->a.f[i]=g->stateX[i];g->Y_0[i]=g->a.f[i];g->stateX[i]=0;}
		g->lenA[0]=g->lenC[0]=g->lenA[1]=g->lenC[1]=0;
	}
	g->status=GCM_ACCEPTING_HEADER;
}
Exemplo n.º 4
0
/* Add Ciphertext - decrypts to plaintext */
int GCM_add_cipher(gcm *g,char *plain,char *cipher,int len)
{ /* Add ciphertext to extract plaintext, len is length of ciphertext. */
	int i,j=0;
	unsign32 counter;
	uchar B[16];
	if (g->status==GCM_ACCEPTING_HEADER) g->status=GCM_ACCEPTING_CIPHER;
	if (g->status!=GCM_ACCEPTING_CIPHER) return 0;

	while (j<len)
	{
		counter=pack((uchar *)&(g->a.f[12]));
		counter++;
		unpack(counter,(uchar *)&(g->a.f[12]));  /* increment counter */
		for (i=0;i<16;i++) B[i]=g->a.f[i];
		AES_ecb_encrypt(&(g->a),B);        /* encrypt it  */
		for (i=0;i<16 && j<len;i++)
		{
			plain[j]=cipher[j]^B[i];
			g->stateX[i]^=cipher[j++];
			g->lenC[1]++; if (g->lenC[1]==0) g->lenC[0]++;
		}
		gf2mul(g);
	}
	if (len%16!=0) g->status=GCM_NOT_ACCEPTING_MORE;
	return 1;
}
Exemplo n.º 5
0
/**
  Performs AES decryption on a data buffer of the specified size in ECB mode.

  This function performs AES decryption on data buffer pointed by Input, of specified
  size of InputSize, in ECB mode.
  InputSize must be multiple of block size (16 bytes). This function does not perform
  padding. Caller must perform padding, if necessary, to ensure valid input data size.
  AesContext should be already correctly initialized by AesInit(). Behavior with
  invalid AES context is undefined.

  If AesContext is NULL, then return FALSE.
  If Input is NULL, then return FALSE.
  If InputSize is not multiple of block size (16 bytes), then return FALSE.
  If Output is NULL, then return FALSE.

  @param[in]   AesContext  Pointer to the AES context.
  @param[in]   Input       Pointer to the buffer containing the data to be decrypted.
  @param[in]   InputSize   Size of the Input buffer in bytes.
  @param[out]  Output      Pointer to a buffer that receives the AES decryption output.

  @retval TRUE   AES decryption succeeded.
  @retval FALSE  AES decryption failed.

**/
BOOLEAN
EFIAPI
AesEcbDecrypt (
  IN   VOID         *AesContext,
  IN   CONST UINT8  *Input,
  IN   UINTN        InputSize,
  OUT  UINT8        *Output
  )
{
  AES_KEY  *AesKey;

  //
  // Check input parameters.
  //
  if (AesContext == NULL || Input == NULL || (InputSize % AES_BLOCK_SIZE) != 0 || Output == NULL) {
    return FALSE;
  }

  AesKey = (AES_KEY *) AesContext;

  //
  // Perform AES data decryption with ECB mode (block-by-block)
  //
  while (InputSize > 0) {
    AES_ecb_encrypt (Input, Output, AesKey + 1, AES_DECRYPT);
    Input     += AES_BLOCK_SIZE;
    Output    += AES_BLOCK_SIZE;
    InputSize -= AES_BLOCK_SIZE;
  }

  return TRUE;
}
Exemplo n.º 6
0
static int smp_e(const unsigned char *k,const unsigned char *data, unsigned char *out)
{
	AES_KEY key;
	AES_set_encrypt_key(k, 128, &key);
	AES_ecb_encrypt(data, out, &key, AES_ENCRYPT);
	return 0;
}
Exemplo n.º 7
0
Arquivo: aes.c Projeto: Kristd/snippet
int main(int argc, char *argv[]) {
	int i = 0;
	const char *in = "\x31\x32\x33\x34\x31\x32\x33\x34\x31\x32\x33\x34\x31\x32\x33\x34";
	unsigned char out[256] = {0};
	AES_KEY *aeskey = NULL;
	
	const char *testkey = "\x31\x32\x33\x34\x31\x32\x33\x34\x31\x32\x33\x34\x31\x32\x33\x34";
	const int length = 128;

	aeskey = (AES_KEY*)malloc(sizeof(AES_KEY));
	memset(aeskey, 0, sizeof(AES_KEY));
	
	memcpy(out, AES_options(), strlen(AES_options()));
	printf("key=%s\n", testkey);
	
	int ret = AES_set_encrypt_key((const unsigned char*)testkey, length, aeskey);
	if(ret != 0) {
		printf("ret=%d\n", ret);
	}
	
	AES_ecb_encrypt((const unsigned char*)in, out, aeskey, AES_ENCRYPT);
	for(i = 0; i < strlen((const char*)out); i++) {
		printf("%02X", out[i]);
	}
	
	printf("\n");
	free(aeskey);
	
	return 0;
}
Exemplo n.º 8
0
static void openssl_decrypt_block(struct openssl_decrypt *s,
                                  const unsigned char *ciphertext, unsigned char *plaintext, const char *nonce_and_counter,
                                  int len)
{
#ifndef HAVE_CRYPTO
    UNUSED(ciphertext);
    UNUSED(plaintext);
#endif
    if(nonce_and_counter) {
        memcpy(s->ivec, nonce_and_counter, AES_BLOCK_SIZE);
        s->num = 0;
    }

    switch(s->mode) {
    case MODE_AES128_ECB:
        assert(len == AES_BLOCK_SIZE);
#ifdef HAVE_CRYPTO
        AES_ecb_encrypt(ciphertext, plaintext,
                        &s->key, AES_DECRYPT);
#endif // HAVE_CRYPTO
        break;
    case MODE_AES128_CTR:
#ifdef HAVE_CRYPTO
        AES_ctr128_encrypt(ciphertext, plaintext, len, &s->key, s->ivec,
                           s->ecount, &s->num);
#endif
        break;
    default:
        abort();
    }
}
Exemplo n.º 9
0
static void wrapper_aes_ecb_encrypt(const unsigned char *in, unsigned char *out, size_t length, const AES_KEY *key){
	uint32_t i;

	for (i = 0; i < length / AES_BLOCK_SIZE; i++){
		AES_ecb_encrypt(in, out, key, AES_ENCRYPT);
		in += AES_BLOCK_SIZE;
		out += AES_BLOCK_SIZE;
	}
}
Exemplo n.º 10
0
			void decrypt(AES_KEY &ctx, decrypt_output_type &dst, const decrypt_input_type &src, iv_type &iv)
			{
				// 16 블록(bytes)의 배수가 아닐경우 예외, 패딩을 사용할 것.
				if (0 != src.size() % 16) throw salm::exception("block size should be a fixed multiple", salm::INVALID_BAD_MULTIPLE);

				for (int i = 0; i < src.size(); i += 16)
				{
					AES_ecb_encrypt((&src[0] + i), (&dst[0] + i), (const AES_KEY*)&ctx, AES_DECRYPT);
				}
			}
Exemplo n.º 11
0
static RET_CODE decrypt_key(UINT8 *in, UINT8 *out, UINT8 *key, INT32 length)
{
	UINT8 otp_key[16];
	UINT8 decrypted_key[16];
	//UINT8 temp[2][16];
	AES_KEY aes_key;
	UINT32 value;
	UINT32 i;

	//read key from otp
	for(i=0; i<4; i++)
	{
		value = ali_otp_read(0x4d+i*4);
		MEMCPY(otp_key+i*4, &value, 4);
	}

	//decrypt key
	AES_set_decrypt_key(otp_key, 128, &aes_key);
	AES_ecb_encrypt(key, decrypted_key, &aes_key, 0);

	//decrypt input
	AES_set_decrypt_key(decrypted_key, 128, &aes_key);
	while(length>0)
	{
		if(length<16)
		{
			//MEMSET(temp[0], 0, 16);
			//MEMCPY(temp[0], in, length);
			//AES_ecb_encrypt(temp[0], temp[1], &aes_key, 0);
			//MEMCPY(out, temp[1], length);
			MEMCPY(out, in, length);
		}
		else
		{
			AES_ecb_encrypt(in, out, &aes_key, 0);
		}
		in+=16;
		out+=16;
		length -= 16;
	}

	return 0;
}
Exemplo n.º 12
0
void dvbaes_decrypt_stream(void *key, dvbapi_batch *batch, int max_len)
{
	int i, j, len;
	for (i = 0; batch[i].data && i < max_len; i++)
	{
		len = (batch[i].len / 16) * 16;
		for (j = 0; j < len; j++)
			AES_ecb_encrypt(batch[i].data + j, batch[i].data + j,
					(AES_KEY *) key, AES_DECRYPT);

	}
}
Exemplo n.º 13
0
static int
ossl_aes_ecb_decrypt(PX_Cipher * c, const uint8 *data, unsigned dlen,
					 uint8 *res)
{
	unsigned	bs = gen_ossl_block_size(c);
	ossldata   *od = c->ptr;
	const uint8 *end = data + dlen - bs;

	if (!od->init)
		ossl_aes_key_init(od, AES_DECRYPT);

	for (; data <= end; data += bs, res += bs)
		AES_ecb_encrypt(data, res, &od->u.aes_key, AES_DECRYPT);
	return 0;
}
Exemplo n.º 14
0
/* Finish and extract Tag */
void GCM_finish(gcm *g,char *tag)
{ /* Finish off GHASH and extract tag (MAC) */
	int i;

	GCM_wrap(g);

/* extract tag */
	if (tag!=NULL)
	{
		AES_ecb_encrypt(&(g->a),g->Y_0);        /* E(K,Y0) */
		for (i=0;i<16;i++) g->Y_0[i]^=g->stateX[i];
		for (i=0;i<16;i++) {tag[i]=g->Y_0[i];g->Y_0[i]=g->stateX[i]=0;}
	}
	g->status=GCM_FINISHED;
	AES_end(&(g->a));
}
Exemplo n.º 15
0
uint32_t __stdcall aes_crypt(AES_KEY *aes, uint32_t type, uint32_t direction, uint8_t *ivec, uint32_t length, uint8_t *data, uint8_t *buffer){
	if (direction != AES_ENCRYPT && direction != AES_DECRYPT)
		return AES_UNKNOWN_DIRECTION;

	switch(type){
		case AES_NORMAL:
			if (direction == AES_ENCRYPT)
				AES_encrypt(data, buffer, aes);
			else
				AES_decrypt(data, buffer, aes);
			break;
		case AES_ECB: AES_ecb_encrypt(data, buffer, aes, direction); break;
		case AES_CBC: AES_cbc_encrypt(data, buffer, length, aes, ivec, direction); break;
		default: return AES_UNKNOWN_TYPE;
	}
	return AES_SUCCESS;
}
Exemplo n.º 16
0
int main(int argc, char *argv[]){
	char key[33] = "0123456789abcdef0123456789abcdef";
	unsigned char iv[16] = {0x2d, 0xa1, 0x20, 0xca, 0xc8, 0xa4, 0x80, 0x47, 0x13, 0x25, 0x0a, 0x1c, 0x5a, 0x58, 0xc3, 0xe6};
	// unsigned char uncrypted_contents[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
	unsigned char uncrypted_contents[16] = {0x74, 0xab, 0x7c, 0x2e, 0xf4, 0x76, 0x24, 0x40, 0xfd, 0x0f, 0xdb, 0x75, 0x17, 0xe7, 0x96, 0xf5};
	unsigned char encrypted_contents_cbc[16];
	unsigned char encrypted_contents_ecb[16];
	
	
	AES_KEY akey;
	if(AES_set_encrypt_key((unsigned char *)key, 256, &akey) < 0) {
		fprintf(stderr, "AES_set_derypt_key failed!\n");
	}

#ifdef DEBUG
printf("key -> akey.rd_key: ");
for(int i=0; i<sizeof(akey.rd_key)/4; i++)
	printf("%.8x ", akey.rd_key[i]);
printf("\n");
#endif

	AES_cbc_encrypt((unsigned char *)uncrypted_contents, (unsigned char *)encrypted_contents_cbc, 16, &akey, iv, AES_ENCRYPT); // AES_DECRYPT or AES_ENCRYPT
	AES_ecb_encrypt((unsigned char *)uncrypted_contents, (unsigned char *)encrypted_contents_ecb, &akey, AES_ENCRYPT); // AES_DECRYPT or AES_ENCRYPT

#ifdef DEBUG
printf("uncrypted_contents: ");
for(int i=0; i<16; i++)
	printf("%.2x", uncrypted_contents[i]);
printf("\n");

printf("encrypted_contents_cbc: ");
for(int i=0; i<16; i++)
	printf("%.2x", encrypted_contents_cbc[i]);
printf("\n");

printf("encrypted_contents_ecb: ");
for(int i=0; i<16; i++)
	printf("%.2x", encrypted_contents_ecb[i]);
printf("\n");
#endif
	
	return 0;
}
Exemplo n.º 17
0
std::string AESDecrypt(const std::string& key, const std::string& in)
{
    std::string out;
    if (key.length() != AES_KEY_LENGTH || in.length() % AES_BLOCK_SIZE != 0)
    {
        return out;
    }

    std::string dec = Hex2String(in);

    AES_KEY aes_key;
    AES_set_decrypt_key((unsigned char *)key.c_str(), AES_KEY_LENGTH * 8, &aes_key);

    unsigned char *p = (unsigned char *)dec.c_str();
    for (int i = 0; i < dec.length() / AES_BLOCK_SIZE; ++i)
    {
        unsigned char src[AES_BLOCK_SIZE + 1] = {'\0'};
        AES_ecb_encrypt(p + i * AES_BLOCK_SIZE, src, &aes_key, AES_DECRYPT);
        out.append((char *)src);
    }

    return PKCS5UnPadding(out);
}
Exemplo n.º 18
0
std::string AESEncrypt(const std::string& key, const std::string& in)
{
    std::string out;
    if (key.length() != AES_KEY_LENGTH)
    {
        return out;
    }

    std::string src =  PKCS5Padding(in);

    AES_KEY aes_key;
    AES_set_encrypt_key((unsigned char *)key.c_str(), AES_KEY_LENGTH * 8, &aes_key);

    unsigned char *p = (unsigned char *)src.c_str();
    for (int i = 0; i < src.length() / AES_BLOCK_SIZE; ++i)
    {
        unsigned char dec[AES_BLOCK_SIZE] = {'\0'};
        AES_ecb_encrypt(p + i * AES_BLOCK_SIZE, dec, &aes_key, AES_ENCRYPT);
        out.append(String2Hex(dec, AES_BLOCK_SIZE));
    }

    return out;
}
Exemplo n.º 19
0
static void openssl_encrypt_block(struct openssl_encrypt *s, unsigned char *plaintext,
                                  unsigned char *ciphertext, char *nonce_plus_counter, int len)
{
#ifndef HAVE_CRYPTO
    UNUSED(ciphertext);
    UNUSED(plaintext);
#endif
    if(nonce_plus_counter) {
        memcpy(nonce_plus_counter, (char *) s->ivec, 16);
        /* We do start a new block so we zero the byte counter
         * Please NB that counter doesn't need to be incremented
         * because the counter is incremented everytime s->num == 0,
         * presumably before encryption, so setting it to 0 forces
         * counter increment as well.
         */
        if(s->num != 0) {
            s->num = 0;
        }
    }

    switch(s->mode) {
    case MODE_AES128_CTR:
#ifdef HAVE_CRYPTO
        AES_ctr128_encrypt(plaintext, ciphertext, len, &s->key, s->ivec,
                           s->ecount, &s->num);
#endif
        break;
    case MODE_AES128_ECB:
        assert(len == AES_BLOCK_SIZE);
#ifdef HAVE_CRYPTO
        AES_ecb_encrypt(plaintext, ciphertext,
                        &s->key, AES_ENCRYPT);
#endif
        break;
    }
}
Exemplo n.º 20
0
BOOL CUserLoginDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	
	HICON m_hIcon;
	m_hIcon=AfxGetApp()->LoadIcon(IDR_MAINFRAME);//修改对话框的图标
	SetIcon(m_hIcon,TRUE);


	// TODO:  在此添加额外的初始化
	ModifyStyleEx(0,WS_EX_APPWINDOW);
	GetClientRect(&m_rect);
	SetBackgroundColor(RGB(247,252,255));
	//初始化全局变量g_CurrentDir(当前目录绝对路径)
	GetCurrentDirectory(MAX_PATH,g_CurrentDir.GetBuffer(MAX_PATH));
	g_CurrentDir.ReleaseBuffer(); 
	//提取之前保存的用户信息,如果有的话,利用sqlite数据库
	sqlite3* UserInfodb = NULL;
	//打开数据库,不存在则创建
	if(sqlite3_open("User\\UserInfo.db3", &UserInfodb) != 0)
	{
		MessageBox(L"打开用户信息列表失败",L"初始化",MB_ICONERROR);
		return FALSE;
	}
	//创建用户信息表格,已存在则创建失败,错误信息保存在szErrmsg中
	char *sqlcmd = "CREATE TABLE UserInfo(\
				     UserName string PRIMARY KEY,\
					 PassWord string )";

	char *szErrmsg = NULL;
	sqlite3_exec(UserInfodb,sqlcmd,0,0,&szErrmsg);
	//查询用户数据
	char *zsql = "SELECT * FROM UserInfo";
	char **szResult = NULL;
	int nRow = 0;
	int nColumn = 0;
	sqlite3_get_table(UserInfodb,zsql,&szResult,&nRow,&nColumn,&szErrmsg);

	char username[MAX_USERNAMELEN] = {0};
	char passwordEncodeHex[500] = {0};
	char *passwordEncode = NULL;
	memset(username,0,MAX_USERNAMELEN);
	memset(passwordEncodeHex,0,500);
	int l = strlen(szResult[3]);
	if(nRow)
	{
		memcpy(username,szResult[2],strlen(szResult[2]));
		memcpy(passwordEncodeHex,szResult[3],strlen(szResult[3]));
	}

	m_UserIniFilePath = g_CurrentDir;
	m_UserIniFilePath+=L"\\config\\config.ini";
	m_UserName = Char2CString(username);
	m_CheckPW=GetPrivateProfileInt(L"UserSetting",L"SavePassword",0,m_UserIniFilePath);
	if(strlen(passwordEncodeHex))
	{
		//如果密码不为空,则解密
		char *userKey = "1234567887654321"; // 原始密钥128位,16字节
		CString sDataLen;
		GetPrivateProfileString(L"UserSetting",L"PasswordEnCodeLen",L"",sDataLen.GetBuffer(10),10,m_UserIniFilePath);
		int datalen = _wtoi(sDataLen);
		char keyDecode[256]; //解密密钥
		char *PassWordDecoded = NULL;
		//char *dataSrc = NULL; //补齐的数据
		//将转为16进制串的密码转为初始状态

		int L=strlen(passwordEncodeHex);
		passwordEncode = (char*)malloc(sizeof(char)*(L));
		memset(passwordEncode,0,L);
		int  x;
		for(int i=0;i<L/2;i++) 
		{		
			sscanf(passwordEncodeHex+i*2,"%02X",&x);
			passwordEncode[i]=(char)x;//(x&0xFFu);
		}
		unsigned char in[16];
		unsigned char out[16];
		PassWordDecoded = (char*)malloc(sizeof(char)*datalen);
		if(!PassWordDecoded)
		{
			return -1;
		}
		memset(PassWordDecoded,0,datalen);
		//设置解密密钥
		if(0!=AES_set_decrypt_key((const unsigned char *)userKey,128,(AES_KEY *)keyDecode))
		{
			return -1;
		}
		//循环加密,以16字节为单位
		int count = datalen/16;
		//解密
		for(int i = 0;i<count;++i)
		{
			memset(in, 0, 16);             
			memset(out, 0, 16); 	
			memcpy(in,passwordEncode+i*16,16);
			AES_ecb_encrypt(in,out,(AES_KEY *)keyDecode,AES_DECRYPT);
			memcpy(PassWordDecoded+i*16,out,16);
		}
		//初始化密码
		m_PassWord = Char2CString(PassWordDecoded);
		//释放资源
		free(passwordEncode);
		passwordEncode = NULL;
		free(PassWordDecoded);
		PassWordDecoded = NULL;
	}
	//关闭数据库
	sqlite3_close(UserInfodb);
	UserInfodb = NULL;	
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}
Exemplo n.º 21
0
void Renderer::decrypt(unsigned char *in, unsigned char *out)
{
  AES_ecb_encrypt(in, out, &decryptKey, AES_DECRYPT);
  AES_ecb_encrypt(&in[AES_BLOCK_SIZE], &out[AES_BLOCK_SIZE], &decryptKey, AES_DECRYPT);
}
Exemplo n.º 22
0
int airplay_decrypt(AES_KEY *ctx, unsigned char *in, unsigned int len, unsigned char *out)
{
	unsigned char *pin,*pout;
	unsigned int n;
	unsigned char k;
	int i,remain = 0;
	int l = len, len1 = 0;

	if (l == 0) return 0;

	pin = in; pout = out;

	fprintf(stderr, "remain=%d\n", ctx->remain_bytes);

	if (ctx->remain_bytes) {
		n = ctx->remain_bytes;
		do {
			*pout = *pin ^ ctx->out[n];
			n = (n + 1) & 0xf;
			ctx->remain_bytes = n;
			l--;
			pout++;
			pin++;
			if (l == 0) return 0;
		} while (n != 0);
	}

	if (l <= 15) {
		remain = l;
		AES_ecb_encrypt(&ctx->in, &ctx->out, ctx, AES_ENCRYPT);
	} else {
		len1 = l;
		do {
			AES_ecb_encrypt(&ctx->in, &ctx->out, ctx, AES_ENCRYPT);
			i = 15;
			do {
				k = ctx->in[i] + 1;
				ctx->in[i] = k;
				if (k) break;
				-- i;
			} while (i != -1);
			for (i=0; i<16; i++)
			{
				pout[i] = pin[i] ^ ctx->out[i];
			}
			pout += 16;
			pin += 16;
			l -= 16;
		} while (l > 15);
		if (l == 0) return 0;

/*
		i = (len1 - 16) & 0xfffffff0 + 16;

		pin = in + i;
		pout = out + i;
*/
		AES_ecb_encrypt(&ctx->in, &ctx->out, ctx, 1);
		remain = l;
	}

	i = 15;
	do {
		k = ctx->in[i] + 1;
		ctx->in[i] = k;
		if (k) break;
		-- i;
	} while (i != -1);

	for (i=0; i<remain; i++)
	{
		pout[i] = pin[i] ^ ctx->out[i];
	}
	if (ctx->remain_flags == 0) 
		ctx->remain_bytes += remain;

	return 0;
}
Exemplo n.º 23
0
void regDialog::DecryptK0()
{
    unsigned char InBuff[16], OutBuff[16];

    char *plain;
    int len_plain;
    len_plain = len_cipher2;
    plain = (char *)malloc(len_plain * sizeof(char));
    memset(plain, '\0', len_plain);

    unsigned char Seed[32];
    strcpy((char *)Seed, "AbCdEfGhIjKlMnOpQrStUvWxYz12345");

    AES_KEY AESEncryptKey, AESDecryptKey;
    AES_set_encrypt_key(Seed, 256, &AESEncryptKey);
    AES_set_decrypt_key(Seed, 256, &AESDecryptKey);

    int i;
    int num1;
    num1 = len_cipher2 / 16 + 1;

    char instead[2];
    memset(instead, '\0', 2);
    if(len_cipher2 % 16 == 1)
    {
        memcpy(instead, cipher2 + (num1 - 1) * 16, 1);
    }

    int cnt = 0;
    for(int i = 0; i < (num1) * 16; i++)
    {
        if(cipher2[i] == instead[0])
        {
            cipher2[i] = 0;
            printf("%d, ", i);
            cnt++;
        }
    }

    printf("\n%d\n", cnt);









    for (i = 0; i < num1; i++) {
             memset((char *)InBuff, '\0', 16);
             memset((char *)OutBuff, '\0', 16);

             memcpy((char *)OutBuff, cipher2 + 16 * i, 16);

             AES_ecb_encrypt(OutBuff, InBuff, &AESDecryptKey, AES_DECRYPT);
             if(i == (num1 - 1)) {
                     memcpy(plain + 16 * i, (char *)InBuff, len_plain % 16);
             }else {
                     memcpy(plain + 16 * i, (char *)InBuff, 16);
             }
    }

    len_msg2 = 0;
    memset(msg2, '\0', 256);
    len_msg2 = strlen(plain);
//    msg2 = (char *)malloc(len_msg2 * sizeof(char));
//    memset(msg2, '\0', len_msg2);
    memcpy(msg2, plain, len_plain);
    qDebug()<<"[DecryptK0]: "<<msg2;
}
Exemplo n.º 24
0
void regDialog::EncryptK0()
{
    unsigned char InBuff[16], OutBuff[16];

    char *plain;
    int len_plain;
    len_plain = strlen(msg1);
    plain = (char *)malloc(len_plain * sizeof(char));
    memset(plain, '\0', len_plain);

    memcpy(plain, msg1, strlen(msg1));


    len_cipher1 = (len_plain / 16 + 1) * 16;
    qDebug()<<"[EncryptK0]: "<<len_cipher1;
    cipher1 = (char *)malloc(len_cipher1 * sizeof(char));
    memset(cipher1, '\0', len_cipher1);

    unsigned char Seed[32];
    strcpy((char *)Seed, "AbCdEfGhIjKlMnOpQrStUvWxYz12345");

    AES_KEY AESEncryptKey, AESDecryptKey;
    AES_set_encrypt_key(Seed, 256, &AESEncryptKey);
    AES_set_decrypt_key(Seed, 256, &AESDecryptKey);


    int i;
    int num1;
    num1 = len_plain / 16 + 1;

    for(i = 0; i < num1; i++) {
        memset((char *)InBuff, '\0', 16);
        memset((char *)OutBuff, '\0', 16);

        if (i == (num1 - 1)) {
            memcpy((char *)InBuff, plain + 16 * i, len_plain % 16);
        } else {
            memcpy((char *)InBuff, plain + 16 * i, 16);
        }

        AES_ecb_encrypt(InBuff, OutBuff, &AESEncryptKey, AES_ENCRYPT);
        memcpy(cipher1 + 16 * i, (char *)OutBuff, 16);
    }

    int cnt = 0;
    char instead[2];
    memset(instead, '\0', 2);

    for(int i = 0; i < num1 * 16; i++)
    {
        printf("[%d], ", cipher1[i]);
    }
    printf("\n");

    int Count[256] = {0};
    for(int i = 0; i < num1 * 16; i++)
    {
        Count[cipher1[i] + 128]++;
    }

    for(int i = 0; i < 256; i++)
    {
        if(Count[i] == 0)
        {
            instead[0] = i - 128;
            printf("\ninstead: %d\n", instead[0]);
            break;
        }
    }

    for(int i = 0; i < num1 * 16; i++)
    {

        if(cipher1[i] == 0)
        {
            cnt++;
            cipher1[i] = instead[0];
            printf("%d, ", i);
        }
//            printf("{%d}, ", cipher1[i]);
    }
    printf("\n%d\n", cnt);

    qDebug()<<"[EncryptK0]: "<<cnt;
    qDebug()<<"[EncryptK0]: "<<strlen(cipher1);
    if (cnt != 0)
    {
        memcpy(cipher1 + num1 * 16, instead, 1);
    }

}
Exemplo n.º 25
0
void CUserLoginDlg::OnBnClickedOk()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	//保存用户信息
	//保存密码选项
	CString szCheckPW;
	szCheckPW.Format(L"%d",m_CheckPW);
	WritePrivateProfileString(L"UserSetting",L"SavePassword",szCheckPW,m_UserIniFilePath);


	sqlite3* pUserInfodb = NULL;
	char *pErrMsg;
	//打开数据库,不存在则创建
	if(sqlite3_open("User\\UserInfo.db3", &pUserInfodb) != 0)
	{
		MessageBox(L"打开用户信息列表失败",L"初始化",MB_ICONERROR);
		return;
	}

	char *pUserName = CString2Char(m_UserName);

	char UserName[MAX_USERNAMELEN] = {0};
	char *PassWordEncode = NULL;
	memcpy(UserName,pUserName,strlen(pUserName));
	//释放资源
	free(pUserName);
	pUserName = NULL;
	//删除已有数据
	sqlite3_exec(pUserInfodb,"DELETE from UserInfo",0,0,&pErrMsg);
	if(m_CheckPW)
	{
		//保存密码
		char *pPassWord = CString2Char(m_PassWord);
		char PassWord[MAX_PASSWORDLEN];
		memset(PassWord,0,MAX_PASSWORDLEN);
		memcpy(PassWord,pPassWord,strlen(pPassWord));
		free(pPassWord);
		char *userKey = "1234567887654321"; // 原始密钥128位,16字节
		unsigned char keyEncode[256] = {0};   // 加密密钥
		unsigned char keyDecode[256] = {0}; //解密密钥

		char *PassWordSrc = NULL; //补齐的数据
		unsigned char in[16];
		unsigned char out[16];
		int datalen = strlen(PassWord);
		int data_rest = datalen%16;
		//如果除不尽,则补齐16位
		datalen+=16-data_rest;
		PassWordSrc = (char*)malloc(sizeof(char)*datalen);
		if(!PassWordSrc)
		{
			return;
		}
		PassWordEncode = (char*)malloc(sizeof(char)*datalen);
		if(!PassWordEncode)
		{
			return;
		}
		//补齐之后的明文
		memset(PassWordSrc,0,datalen);
		memcpy(PassWordSrc,PassWord,strlen(PassWord));
		//设置加密密钥
		if(0!=AES_set_encrypt_key((const unsigned char *)userKey,128,(AES_KEY *)keyEncode))
		{
			return;
		}
		//循环加密,以16字节为单位
		int count = datalen/16;
		for(int i = 0;i<count;++i)
		{
			memset(in, 0, 16);             
			memset(out, 0, 16); 
			memcpy(in,PassWordSrc+i*16,16);
			AES_ecb_encrypt(in,out,(AES_KEY *)keyEncode,AES_ENCRYPT);
			memcpy(PassWordEncode+i*16,out,16);
		}
		//保存密码长度
		CString szPasswordEnCodeLen;
		szPasswordEnCodeLen.Format(L"%d",datalen);
		WritePrivateProfileString(L"UserSetting",L"PasswordEnCodeLen",szPasswordEnCodeLen,m_UserIniFilePath);
		free(PassWordSrc);
		PassWordSrc = NULL;
	}
    //插入新用户
	//将加密后的密码转为16进制
	char PassWordEncodeHex[500];
	memset(PassWordEncodeHex,0,500);
	for (int i = 0; i < strlen(PassWordEncode); i++)
	{
		_snprintf(PassWordEncodeHex +2*i,500-2*i,"%02X",(unsigned char)PassWordEncode[i]);
	}
	char sqlcmd[500];
	_snprintf(sqlcmd,500,"INSERT INTO UserInfo(UserName,PassWord)values('%s','%s')",UserName,PassWordEncodeHex);
	sqlite3_exec(pUserInfodb,sqlcmd,0,0,&pErrMsg);
	sqlite3_close(pUserInfodb);

	if(!ConnectServer())
	{
		return;
	}
	CDialogEx::OnOK();
}