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

📄 dedistbuffer.cpp

📁 aes的原代码
💻 CPP
字号:
#include "StdAfx.h"
#include ".\dedistbuffer.h"
/**
 * 解扰缓冲区公共构造器
 * 参数:
 *	pcID 机器码
 *	secLen 段长度
 *	secNum 第一次需要追溯到的段数,文件开始为第一段即为 0
 *	blckNum 当前段需要移到的块,必须小于没段的总块,其中块长为128bit,即16 byte
 */
DeDistBuffer::DeDistBuffer(unsigned char* pcID,int secLen,int secNum,int blckNum)
{
	this->pcID = new unsigned char[ID_LEN];
	memcpy(this->pcID,pcID,ID_LEN);
	//this->pcID = pcID;
	this->secLen = secLen;
	this->secNum = secNum;
	this->blckNum = blckNum;
	this->iniPK();
	gdk = new GenDistKey(pR,qR,xR);
	Ri = gdk->SectionSeed(secNum);
	/*memset(ss,'\0',sizeof(ss));
	Ri.GetStr(ss);
	printf("%s\n",ss);*/
	this->initialKey(Ri);
	tosclen=(blckNum)*KBL;
	this->setPreferredBlckSeed();
}
/**
 * 解扰缓冲区,由于是对缓存区直接异或解密,所有如有需要请备份好原缓冲区内容
 * 参数:
 *	buf 源缓冲区
 *	len 源缓冲区长度
 * 返回:解扰后的缓冲区指针
 */
unsigned char * DeDistBuffer::DeBuffer(unsigned char *buf,int len)
{
	tosclen+=len;
	for(int i =0; i<len; i++)
	{
		buf[i]^=blckKey[i%KBL];
		buf[i]^=blckKey[KBL-i%KBL-1];
		if((i+1)%KBL==0) nextKey();  //待改进,可以少做一次
	}
	if(tosclen == secLen)
	{ 
		initialKey(Ri);
		tosclen =0;
		secNum++;
	}
	return buf;
}
/**
 * 设置须跳到的段数和块数,用户不断拉流媒体时使用
 * 如果文件指针指向不足一块,则须把指针移向上块的末尾
 * 参数:
 *	newSecNum 要跳到的段数
 *	newBlckNum 要跳到的块数
 */
void DeDistBuffer::setSectionBlck(int newSecNum,int newBlckNum)
{
	if(newSecNum == secNum)
	{
		this->blckNum = newBlckNum;
		tosclen=(blckNum)*KBL;
		this->setPreferredBlckSeed();
		return;
	}
	this->secNum = newSecNum;
	this->initialKey(gdk->SectionSeed(secNum));
	this->blckNum = newBlckNum;
	tosclen=(blckNum)*KBL;
	this->setPreferredBlckSeed();
}
/**
 * 初始化密钥
 */
void DeDistBuffer::initialKey(BigInt &Rx)
{
	long len = Rx.m_nLength;
	unsigned long *val = Rx.m_ulValue;
	memset(blckKey,0,sizeof(blckKey));
	if(len>=KBL)
	{
		for(int i=0; i<len; i++)
		{
			blckKey[i%KBL]^=(unsigned char)(((unsigned char)((__int64)val[i]*PRM3%PRM2))^blckKey[KBL-i%KBL-1]);
		}
	}
	else
	{
		for(int i=0; i<KBL; i++)
		{
			blckKey[i]^=(unsigned char)(((unsigned char)((__int64)val[i%KBL]*PRM3%PRM2))^blckKey[KBL-i-1]);
		}
	}
	Ri=gdk->NextSeed(Rx);
}
/**
 * 求当前密钥的下一个密钥
 */
void DeDistBuffer::nextKey()
{
	for(int i=0; i<KBL; i++)
	{
		blckKey[i]^=(unsigned char)(PRM1*blckKey[KBL-i-1]%PRM2);
	}
}

/**
 * 根据快号,计算对应的密钥
 */
void DeDistBuffer::setPreferredBlckSeed()
{
	for(int i=0;i<blckNum;i++)
	{
		nextKey();
	}
}

void DeDistBuffer::iniPK()
{
	int l1,l2,l3;
	l1 = strlen(PP);
	l2 = strlen(PQ);
	l3 = strlen(PX);
	pR = new char[l1+1];
	qR = new char[l2+1];
	xR = new char[l3+1];
	memset(pR,'\0',sizeof(pR));
	memset(qR,'\0',sizeof(qR));
	memset(xR,'\0',sizeof(xR));
	strcpy(pR,PP);
	strcpy(qR,PQ);
	strcpy(xR,PX);
	/*for(int i=1; i<l1; i++)
	{
		for(int j=i; j<l1; j++)
			pR[j]=(char)((int)pR[j]*pcID[j%KBL]%DECI);
	}
	for(int i=l2-1; i>0; i--)
	{
		for(int j=i; j<l2; j++)
			qR[i]=(char)((int)pR[j]*pcID[j%KBL]%DECI);
	}*/
	for(int i=1; i<l3; i++)
	{
		for(int j=l3-1; j>=i; j--)
			xR[j]=(char)((int)xR[j]*pcID[j%KBL]%DECI)+'0';
	}
}
DeDistBuffer::~DeDistBuffer(void)
{
	delete(gdk);
	delete[] pcID;
}

⌨️ 快捷键说明

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