Hola, perdón x la tardanza, estuve analizando un poco tu algoritmo, hice este programita (Perdón si está un poco desorganizado, preguntá lo q no entiendas).
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define RAND_MAX = 0xFFFFFFFF
float CalcEntropy(const unsigned int *buffer, const unsigned int size);
unsigned int const1[32];
unsigned int const2[32];
using namespace std;
void FillConsts();
unsigned int countCommonBits(const unsigned int input1, const unsigned int const1);
unsigned int Encrypt(unsigned int input1, unsigned int input2);
const unsigned int MAX_VAL = 10000;
int main()
{
FillConsts();
unsigned int i = 0;
unsigned int *buffer = new unsigned int[MAX_VAL / 2 + 1];
for(i = 0; i < MAX_VAL; i+= 8)
{
unsigned int word1 = (i%256) * 256*256*256 + ((i)%256)*256*256 + ((i)%256)*256 + ((i)%256);
unsigned int word2 = ((i)%256) * 256*256*256 + ((i)%256)*256*256 + ((i)%256)*256 + ((i)%256);
buffer[i/8] = word1;
}
cout << "Non-Entropic Input Before:" << CalcEntropy(buffer, MAX_VAL / 2) << endl;
for(i = 0; i < MAX_VAL; i+= 8)
{
unsigned int word1 = (i%256) * 256*256*256 + ((i)%256)*256*256 + ((i)%256)*256 + ((i)%256);
unsigned int word2 = ((i)%256) * 256*256*256 + ((i)%256)*256*256 + ((i)%256)*256 + ((i)%256);
buffer[i / 8] = Encrypt(word1,word2);
}
cout << "Non-Entropic Input After:" << CalcEntropy(buffer, MAX_VAL / 2) << endl;
for(i = 0; i < MAX_VAL; i+= 8)
{
unsigned int word1 = (rand()%256) * 256*256*256 + (rand()%256)*256*256 + (rand()%256)*256 + (rand()%256);
unsigned int word2 = (rand()%256) * 256*256*256 + (rand()%256)*256*256 + (rand()%256)*256 + (rand()%256);
buffer[i/8] = word1;
}
cout << "Entropic Input Before:" << CalcEntropy(buffer, MAX_VAL / 2) << endl;
for(i = 0; i < MAX_VAL; i+= 8)
{
unsigned int word1 = (rand()%256) * 256*256*256 + (rand()%256)*256*256 + (rand()%256)*256 + (rand()%256);
unsigned int word2 = (rand()%256) * 256*256*256 + (rand()%256)*256*256 + (rand()%256)*256 + (rand()%256);
buffer[i / 8] = Encrypt(word1,word2);
}
cout << "Entropic Input After:" << CalcEntropy(buffer, MAX_VAL / 2) << endl;
return 0;
}
void FillConsts()
{
srand(1);
for(unsigned int i = 0; i < 32; i++)
{
const1[i] = (unsigned int) rand();
const2[i] = (unsigned int) rand();
}
}
unsigned int countCommonBits(const unsigned int input1, const unsigned int const1)
{
char szInput1[33];
char szConst1[33];
unsigned int retVal;
itoa((int) input1, szInput1, 2);
itoa((int) const1, szConst1, 2);
for(int i = 0; i < 32; i++) if(szInput1[i] == szConst1[i]) retVal++;
return retVal;
}
unsigned int Encrypt(unsigned int input1, unsigned int input2)
{
unsigned int i = 0;
unsigned int x1 = 0;
unsigned int x2 = 0;
unsigned int res1 = 0;
unsigned int res2 = 0;
unsigned int res = 0;
for(i=0;i<32;i++)
{
x1 = countCommonBits(input1, const1[i]);
res1 = (res1*2) + (x1 & 1);
}
for(i=0;i<32;i++)
{
x2 = countCommonBits(input2, const2[i]);
res2 = (res2*2) + (x2 & 1);
}
res = res1^res2;
return res;
}
float CalcEntropy(const unsigned int *buffer, const unsigned int size)
{
unsigned int i = 0;
float entropy = 0.0;
long count[256];
float freq[256];
for(i = 0; i < 256; i++)
{
count[i] = 0;
}
for(unsigned int j = 0; j < (size); j++)
{
unsigned int val = buffer[j];
for(int t = 0; t < 4; t++)
{
unsigned int valbyte = val%256;
count[valbyte]++;
val = val/256;
}
}
for(i = 1; i <= 255; i++)
{
freq[i] = static_cast<double>(count[i]) / static_cast<double>((size * 4) - count [0]);
#ifdef DEBUG
cout << "Prob(" << i << ") = " << static_cast<double>(freq[i], 10) << " (" << count[i] << " Times)\n";
#endif
if (freq[i] > 0.0){entropy -= freq[i] * (log(freq[i]) / log(2.0));}
}
return entropy;
}
Cabe decir que el máximo de la entropía es 8.
Este programa es el q uso para analizar la entropía de un algoritmo de cifrado. Tener buena entropía es escencial para la fortaleza de un algoritmo. Como vez este programa pone a prueba el algoritmo dandole primero una input con entropía baja (4.95). Y luego una entropía alta creada por el generador pseudoaleatorio de c++ (7.95). Vemos entonces que la salida del algoritmo, da 4.55 de entropía con el input de entropía baja y 7.54 con el de entropía alta. Concluyendo entonces que este algoritmo disminuye la entropía de la entrada, en vez de aumentarla.
Filtré todos los "0" ya que arruinaban el resultado, debido a que es posible que en 4 bytes aleatorios los primeros bytes sean "0". De todas maneras el resultado es el mismo incluyendo los bytes, pero en este caso aparecen muchisimos "0".
Bueno, espero haberte ayudado, y esperamos con ansias tu proximo algoritmo, o quizas una v2.0 de este.
Un abrazo
APOKLIPTICO