Unfortunately I am being forced to re-invent the wheel because high usage of AES encryption on
a windows machine falls over, leaks memory and programs go bang and I needed something that
does not use Microsoft little black box software that is fast and can encrypt 2mb chunks of data blocks.
RSA encryption from Microsoft cannot be trusted.
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(512); // Key bits length
rsa.PersistKeyInCsp = false;
The second line of code is needed because it use to default to true and resulted in the keys being
stored in windows certificate store and put quite simply Microsoft now steels anything it can lay it's hands on
and goes as far as to run web-dav at night to scan remote disk drives on your LAN.
Here is the function and it works by shifting byte code to the right based on the key with a bit of salt thrown
in for good measure and works with a variable length key that can be HEX or even XYZ
private static ushort[] MakeShiftKeys(string Key)
{//Conver HEX keys to array of numbers but will also work for other letters too
ushort[] Shifts = new ushort[Key.Length];
for (int f = 0; f <= Key.Length - 1; f++)
if (Key[f] >= '0' && Key[f] <= '9') Shifts[f] = ushort.Parse(Key[f].ToString()); else Shifts[f] = (ushort)(Key[f] - 55);
return Shifts;
}
public static byte[] EncryptFast(byte[] Text, string Key)
{//About 10 seconds per gigabyte on I7 PC using 1mb blocks
ushort[] ShiftKeys = MakeShiftKeys(Key.ToUpper());
ushort Count = 0; ushort Shift = 0;
ushort R = (ushort)(Text.Length % 8);
if (R == 0) R = (ushort)(Text.Length % 3);
bool Even = R == 5;
for (uint f = 0; f <= Text.Length - 1; f++)
{
if (Count != R) Shift = ShiftKeys[Count];
else if (Even) Shift = 77;
else Shift = 66;
if ((byte)Text[f] + Shift + R <= 255) Text[f] = (byte)(Text[f] + Shift + R);
else Text[f] = (byte)((Text[f] + Shift + R)-256);
Even = !Even; Count++;
if (Count > Key.Length - 1) Count = 0;
}
return Text;
}
During tests this code was found to be 40% slower than using Microsoft AES function and this is because
using C# and byte arrays you have to cast the byte to an INT32 to preform sums so maybe I can fix it using
UN-SAFE section of code if I can remember enough C++.
Previously I have played around with BigINT and MOD but this was not fast enough for big blocks of data
and I know the above is not military grade encryption but how would you rate it and what can be done to
make it better without taking too much of a performance hit.
I was thinking of compressing the data using Microsoft's GZip before using this encryption function which can be done
by using just four lines of code.
MemoryStream MS=new MemoryStream();
using (GZipStream gz = new GZipStream(MS, CompressionMode.Compress, false))
gz.Write(Data, 0, Data.Length);
return MS.ToArray();
But given the amount of network activity that I see coming out from my machine from millions of Dll's
running inside surrogate processes that we have no control over anymore I think I would roll my own compression
if I could do it in under a months work.