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

📄 beeo_encrypt.c

📁 基于beeo标准的数据加解密算法
💻 C
字号:
#include "stdafx.h"

#include <string.h>
#include <stdio.h>
#include <ctype.h>

#include "beeo_Define.h"
#include "beeo/beeo_Basic.h"

#if BEEO_MATRIX_BYTES != 16 
#error "Please rewrite code, change '&0x0f' to '%BEEO_MATRIX_BYTES' "
#endif

BEEO_INLINE uChar beeo_EncodeColByteDo(int cData, int cMatrix)
{
	int i;
	uChar tmp;
	uChar newData=0;
	
	tmp = cData & cMatrix;
	i = 0;
	newData |= (tmp & 0x80)<<(0-i);		i += (cMatrix&0x80)>>7;	 
	newData |= (tmp & 0x40)<<(1-i);		i += (cMatrix&0x40)>>6;	 
	newData |= (tmp & 0x20)<<(2-i);		i += (cMatrix&0x20)>>5;	 
	newData |= (tmp & 0x10)<<(3-i);		i += (cMatrix&0x10)>>4;	 
	newData |= (tmp & 0x08)<<(4-i);		i += (cMatrix&0x08)>>3;	 
	newData |= (tmp & 0x04)<<(5-i);		i += (cMatrix&0x04)>>2;	 
	newData |= (tmp & 0x02)<<(6-i);		i += (cMatrix&0x02)>>1;	 
	newData |= (tmp & 0x01)<<(7-i);		i += (cMatrix&0x01)>>0;	 

	cMatrix ^= 0xff;
	tmp = cData & cMatrix;
	i = 0;
	newData |= (tmp & 0x01)>>(0-i);		i += (cMatrix&0x01)>>0;	 
	newData |= (tmp & 0x02)>>(1-i);		i += (cMatrix&0x02)>>1;	 
	newData |= (tmp & 0x04)>>(2-i);		i += (cMatrix&0x04)>>2;	 
	newData |= (tmp & 0x08)>>(3-i);		i += (cMatrix&0x08)>>3;	 
	newData |= (tmp & 0x10)>>(4-i);		i += (cMatrix&0x10)>>4;	 
	newData |= (tmp & 0x20)>>(5-i);		i += (cMatrix&0x20)>>5;	 
	newData |= (tmp & 0x40)>>(6-i);		i += (cMatrix&0x40)>>6;	 
	newData |= (tmp & 0x80)>>(7-i);		i += (cMatrix&0x80)>>7;	 

	return newData;
}

BEEO_INLINE uChar beeo_DecodeColByteDo(int cData, int cMatrix)
{
	int i;
	uChar newData=0;
	
	i = 0;
	newData |= (cData>>(0-i)) & cMatrix & 0x80;		i += (cMatrix&0x80)>>7;
	newData |= (cData>>(1-i)) & cMatrix & 0x40;		i += (cMatrix&0x40)>>6;
	newData |= (cData>>(2-i)) & cMatrix & 0x20;		i += (cMatrix&0x20)>>5;
	newData |= (cData>>(3-i)) & cMatrix & 0x10;		i += (cMatrix&0x10)>>4;
	newData |= (cData>>(4-i)) & cMatrix & 0x08;		i += (cMatrix&0x08)>>3;
	newData |= (cData>>(5-i)) & cMatrix & 0x04;		i += (cMatrix&0x04)>>2;
	newData |= (cData>>(6-i)) & cMatrix & 0x02;		i += (cMatrix&0x02)>>1;
	newData |= (cData>>(7-i)) & cMatrix & 0x01;		i += (cMatrix&0x01)>>0;
	
	cMatrix ^= 0xff;
	i = 0;
	newData |= (cData<<(0-i)) & cMatrix & 0x01;		i += (cMatrix&0x01)>>0;
	newData |= (cData<<(1-i)) & cMatrix & 0x02;		i += (cMatrix&0x02)>>1;
	newData |= (cData<<(2-i)) & cMatrix & 0x04;		i += (cMatrix&0x04)>>2;
	newData |= (cData<<(3-i)) & cMatrix & 0x08;		i += (cMatrix&0x08)>>3;
	newData |= (cData<<(4-i)) & cMatrix & 0x10;		i += (cMatrix&0x10)>>4;
	newData |= (cData<<(5-i)) & cMatrix & 0x20;		i += (cMatrix&0x20)>>5;
	newData |= (cData<<(6-i)) & cMatrix & 0x40;		i += (cMatrix&0x40)>>6;
	newData |= (cData<<(7-i)) & cMatrix & 0x80;		i += (cMatrix&0x80)>>7;
	
	return newData;
}

#if BEEO_USE_COLUMN_INDEX

static uChar colEncodeIndex[256*256];
static uChar colDecodeIndex[256*256];

static uChar beeo_EncodeColByte(int cData, int cMatrix)
{
	int index;
	index = ((cData&0xff)<<8) + (cMatrix&0xff);
	return colEncodeIndex[index];
}

static uChar beeo_DecodeColByte(int cData, int cMatrix)
{
	int index;
	index = ((cData&0xff)<<8) + (cMatrix&0xff);
	return colDecodeIndex[index];
}

void PASCAL beeo_InitColIndex()
{
	int i,j;
	int index;
	uChar data;
	for(i=0;i<256;i++)
	{
		for(j=0;j<256;j++)
		{
			index = i<<8 | j;
			data = beeo_EncodeColByteDo(i,j);
			colEncodeIndex[index]=data;
			data = beeo_DecodeColByteDo(i,j);
			colDecodeIndex[index]=data;
		}
	}
}

#else

static uChar beeo_EncodeColByte(int cData, int cMatrix)
{
	return beeo_EncodeColByteDo(cData, cMatrix);
}

static uChar beeo_DecodeColByte(int cData, int cMatrix)
{
	return beeo_DecodeColByteDo(cData, cMatrix);
}

void beeo_InitColIndex()
{
	int i,o,m,x;
	
	i=0x23;
	m=0x55;
	o=beeo_EncodeColByte(i,m);
	x=beeo_DecodeColByte(o,m);
}

#endif



void PASCAL beeo_EncodeCol(void* inputBuff, void* matrix, void* outputBuff, int buffLen)
{
	uChar *pD, *pM, *pO;

	pD = (uChar*)inputBuff;
	pM = (uChar*)matrix;
	pO = (uChar*)outputBuff;
	while(buffLen>0)
	{
		buffLen--;
		pO[buffLen]=beeo_EncodeColByte(pD[buffLen], pM[buffLen&0x0f]);
	}
}

void PASCAL beeo_DecodeCol(void* inputBuff, void* matrix, void* outputBuff, int buffLen)
{
	uChar *pD, *pM, *pO;
	
	pD = (uChar*)inputBuff;
	pM = (uChar*)matrix;
	pO = (uChar*)outputBuff;
	while(buffLen>0)
	{
		buffLen--;
		pO[buffLen]=beeo_DecodeColByte(pD[buffLen], pM[buffLen&0x0f]);
	}
}

void PASCAL beeo_EncodeRow(void* inputBuff, void* matrix, void* outputBuff, int buffLen)
{
	uChar *pD, *pM, *pO;
	uChar m, tmp1, tmp2, newData, nextData=0;
	int i;

	pD = (uChar*)inputBuff;
	pM = (uChar*)matrix;
	pO = (uChar*)outputBuff;

	buffLen--;
	nextData = pD[0];
	for(i=0;i<buffLen;i++)
	{
		newData=nextData;
		nextData = pD[i+1];
		m = pM[i&0x0f];
		tmp1 = newData & m;
		tmp2 = nextData & m;
		m ^= 0xff;
		pO[i] = (newData&m) | tmp2;
		nextData=(nextData&m) | tmp1;
	}

	newData=nextData;
	nextData = pO[0];
	m = pM[i&0x0f];
	tmp1 = newData & m;
	tmp2 = nextData & m;
	m ^= 0xff;
	pO[i] = (newData&m) | tmp2;
	nextData=(nextData&m) | tmp1;

	pO[0]=nextData;
}

void PASCAL beeo_DecodeRow(void* inputBuff, void* matrix, void* outputBuff, int buffLen)
{
	uChar *pD, *pM, *pO;
	uChar m, tmp1, tmp2, newData, lastData=0;
	int i;
	
	pD = (uChar*)inputBuff;
	pM = (uChar*)matrix;
	pO = (uChar*)outputBuff;
	
	lastData = pD[0];
	i=buffLen-1;
	newData = pD[i];
	m = pM[i&0x0f];
	tmp1 = newData & m;
	tmp2 = lastData & m;
	m ^= 0xff;
	pD[0]=(lastData&m) | tmp1;
	lastData = (newData&m) | tmp2;
	i--;
	for(; i>=0; i--)
	{
		newData = pD[i];
		m = pM[i&0x0f];
		tmp1 = newData & m;
		tmp2 = lastData & m;
		m ^= 0xff;
		pO[i+1]=(lastData&m) | tmp1;
		lastData = (newData&m) | tmp2;
	}
	pO[0]=lastData;
}

void PASCAL beeo_EncodeXor(void* inputBuff, void* matrix, void* outputBuff, int buffLen)
{
	uChar *pD, *pM, *pO;
	int i;
	
	pD = (uChar*)inputBuff;
	pM = (uChar*)matrix;
	pO = (uChar*)outputBuff;
	for(i=0;i<buffLen;i++)
		pO[i]=pD[i]^pM[i&0x0f];
}

⌨️ 快捷键说明

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