Basically, here's the idea, we have a 'key' that contains every value a byte supports (0 to 255). When you encrypt a byte for instance 171 (0xAB) it creates an "x" using the first 4 bits of the byte, and "y" using the last for bits of the byte
Value = 171 (0xAB)
X = 10 (0xA)
Y = 11 (0xB)
Then in the output of the encrypt routine, it it'll fill that index as Key[X, Y]
Here's an illustration to make it simpler;
[Only registered and activated users can see links. Click Here To Register...]
Now, when an encryption (or decryption) has been completed, the key is rolled, here's an example of what rolling a key looks like
[Only registered and activated users can see links. Click Here To Register...]
Now, lets get down to our code;
Value = 171 (0xAB)
X = 10 (0xA)
Y = 11 (0xB)
Then in the output of the encrypt routine, it it'll fill that index as Key[X, Y]
Here's an illustration to make it simpler;
[Only registered and activated users can see links. Click Here To Register...]
Now, when an encryption (or decryption) has been completed, the key is rolled, here's an example of what rolling a key looks like
[Only registered and activated users can see links. Click Here To Register...]
Now, lets get down to our code;
Code:
public class HybridCipher
{
private byte[,] pEncryptionKey;
private byte[,] pDecryptionKey;
private void RollHorizontal(ref byte[,] Key, byte rVertical, ref byte[] Buffer)
private void RollVertical(ref byte[,] Key, byte rHorizontal, ref byte[] Buffer)
private void RollKey(ref byte[,] Key)
private void FindValue(ref byte[,] Key, byte Value, out byte rVertical, out byte rHorizontal)
public HybridCipher(byte CipherKey)
public byte[] Encrypt(byte[] Data)
public byte[] Decrypt(byte[] Data)
public byte[,] EncryptionKey
public byte[,] DecryptionKey
}
Code:
// Our roll key methods
private void RollHorizontal(ref byte[,] Key, byte rVertical, ref byte[] Buffer)
{
byte rHorizontal = 1;
byte i;
for (i = 0; i < 16; i++)
{
Buffer[rHorizontal] = Key[rVertical, (rHorizontal + 1) % 16];
rHorizontal = (byte)((rHorizontal + 1) % 16);
}
for (i = 0; i < 16; i++)
{
Key[rVertical, i] = Buffer[i];
}
}
private void RollVertical(ref byte[,] Key, byte rHorizontal, ref byte[] Buffer)
{
byte rVertical = 1;
byte i;
for (i = 0; i < 16; i++)
{
Buffer[rVertical] = Key[(rVertical + 1) % 16, rHorizontal];
rVertical = (byte)((rVertical + 1) % 16);
}
for (i = 0; i < 16; i++)
{
Key[i, rHorizontal] = Buffer[i];
}
}
Code:
// Our function to completely roll a key
private void RollKey(ref byte[,] Key)
{
byte rHorizontal = 0;
byte rVertical = 0;
byte[] Buffer = new byte[16];
for (byte i = 0; i < 16; i++)
{
RollHorizontal(ref Key, rVertical++, ref Buffer);
RollVertical(ref Key, rHorizontal++, ref Buffer);
}
}
Code:
// Our function to find a value inside a key
private void FindValue(ref byte[,] Key, byte Value, out byte rVertical, out byte rHorizontal)
{
bool Found = false;
byte x, y;
rVertical = 0;
rHorizontal = 0;
for (x = 0; x < 16; x++)
{
for (y = 0; y < 16; y++)
{
if (Key[x, y] == Value)
{
rVertical = x;
rHorizontal = y;
Found = true;
break;
}
}
if (Found)
break;
}
}
Code:
// constructor to initialize our cipher
public HybridCipher(byte CipherKey)
{
byte c = 0;
pEncryptionKey = new byte[16, 16];
pDecryptionKey = new byte[16, 16];
for (byte x = 0; x < 16; x++)
{
for (byte y = 0; y < 16; y++)
{
pEncryptionKey[x, y] = (byte)(c++ ^ CipherKey);
pDecryptionKey[x, y] = pEncryptionKey[x, y];
}
}
}
Code:
public byte[] Encrypt(byte[] Data)
{
byte[] Output = new byte[Data.Length];
byte x, y;
for (int i = 0; i < Data.Length; i++)
{
x = (byte)(Data[i] >> 4);
y = (byte)(Data[i] & 0xF);
Output[i] = pEncryptionKey[x, y];
}
RollKey(ref pEncryptionKey);
return Output;
}
public byte[] Decrypt(byte[] Data)
{
byte[] Output = new byte[Data.Length];
byte x, y;
for (int i = 0; i < Data.Length; i++)
{
FindValue(ref pDecryptionKey, Data[i], out x, out y);
Output[i] = (byte)((x << 4) + y);
}
RollKey(ref pDecryptionKey);
return Output;
}