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

📄 caessbox.cpp

📁 这是我编的一个AES加密算法中的SBOX的实现
💻 CPP
字号:
#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "math.h"
#include "CAESSBox.h"

using namespace std;

CAthwartElement::CAthwartElement ()
{
	int temp[8] = 
	{0,0,0,1,1,1,1,1};
	for (int i = 0; i < 8; i++)
		Matrix [i] = temp [i];
}

int* CAthwartElement::CycLeft(int *Array, int length, int digit)//悬环左移digit位
{
	int i, *temp = new int [length];
	for (i = 0; i < length; i++)
	{
		temp [i] = Array [i];
	}
	for (i = 0; i < length; i++)
	{
		Array [i] = temp [(i + digit) % length];
	}
	delete []temp;
	return Array;
}

int* CAthwartElement::DivOperation (const int dividend, const int divisor)//模2除
{//DivResult[0]存商,DivResult[1]存余数
	if ((int)(log10 (dividend) / log10 (2)) < (int)(log10 (divisor) / log10 (2))) 
	{
		DivResult[0] = 0;
		DivResult[1] = dividend;
		return DivResult;
	}
    int result, times[2], temp;
	DivResult[1] = dividend;
	DivResult[0] = 0;
	times[0] = 0;
	while (true)
	{
		temp = (int) ( log10 (DivResult[1]) / log10 (2) );
		times[1] = (int) ( log10 (divisor) / log10 (2) );
		times[1] = temp - times[1];
		if (times[1] < 0) 
		{
			DivResult[0] = DivResult[0] << times[0];
			break;
		}
		if (DivResult[0] != 0)
		{
			DivResult[0] = DivResult[0] << (times[0] - times[1] - 1);
		}
		DivResult[0] = DivResult[0] << 1;
		DivResult[0]++;
		result = divisor << times[1];
		DivResult[1] = DivResult[1] ^ result;
		times[0] = times[1];
		if (DivResult[1] == 0) break;
	}
	return DivResult;
}

int CAthwartElement::MulOperation (const int multiplicand, const int multiplier)//模2乘
{
	unsigned int result = 0, times = 0, place = multiplier, temp;
	while (true)
	{
		if (place == 0) break;
		if (place & 1)
		{
			temp = multiplicand << times;
			result = temp ^ result;
		}
		place = place >> 1;
		times++;
	}
	result = DivOperation (result, 283)[1];
	return result;
}

int CAthwartElement::AddOperation (const int augend, const int addend)//模2减
{
	int result;
	result = augend ^ addend;
	return result;
}

int CAthwartElement::SubOperation (const int minuend, const int subtrahend)//模2减
{
	return AddOperation (minuend, subtrahend);
}

int CAthwartElement::Euclid(int a, int b)//扩展欧几里德算法
{
	int x1 = 1, x2 = 0, y1 = 0, y2 = 1, q, r, h, A, B;
	int *Result;
	A = a;
	B = b;
	while (true)
	{
		Result = DivOperation (a,b);
		q = Result[0];
		r = Result[1];
		q = DivOperation (q, 283)[1];
		r = DivOperation (r, 283)[1];
		if (r == 0) break;
		else
		{
			a = b;
			b = r;
			h = x2;
			x2 = MulOperation (q, x2);
			x2 = SubOperation (x1, x2);
			x1 = h;
			h = y2;
			y2 = MulOperation (q, y2);
			y2 = SubOperation (y1, y2);
			y1 = h;
		}
	}
	return x2;
}

int CAthwartElement::AthwartElement (int element, int DataMod)//求逆元
{
	int Temp;
	Temp = Euclid(element, DataMod);
	Temp = DivOperation (Temp, DataMod)[1];
	return Temp;
}

int CAthwartElement::Affinetransform (int Data)//仿射变换
{
	int Array [8];
	int temp, i, j, result = 0;
	temp = Data;
	for (i = 0; i < 8; i++)
	{
		if ((temp & 1) == 0) Array[i] = 0;
		else Array[i] = 1;
		temp = temp >> 1;
	}
	for (i = 0; i < 8; i++)
	{
		temp = 0;
		for (j = 0; j < 8; j++)
		{
			temp = (Array [j] * Matrix [j]) ^ temp;
		}
		result = result << 1;	
		if (temp == 1) result++;
		CycLeft (Matrix, 8, 1);
	}
	result = result ^ 99;
	return result;
}

⌨️ 快捷键说明

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