Permalink
Browse files

Updates

  • Loading branch information...
1 parent 07b596d commit fc430f4cdccf35858ab0ae9bc3f13e713b4fab29 @eoftedal committed Oct 1, 2010
View
@@ -1 +1,2 @@
-*.suo
+*.suo
+
@@ -1,3 +1,6 @@
-*.user
-obj
-bin
+*.user
+obj
+bin
+*.suo
+*.user
+
@@ -4,105 +4,22 @@
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
+using System.Web;
namespace PoetAndDidntKnowIt
{
- class MachineCrypt
+ class MachineCrypt : ICrypt
{
- public static int requests = 0;
- private const int blockSize = 16;
- public static void Crypt()
- {
- byte[] encOnly = HttpEncode.UrlTokenDecode(MachineKeyWrapper.EncryptString("Jeg liker donuts! Og jeg er en diger fisk med eggesmør!"));
- var enc = Enumerable.Repeat(0, 16).Select(i => (byte)i).Concat(encOnly).ToArray();
-
- var blocks = (int)Math.Ceiling((double)enc.Count() / blockSize);
- var lastBlock = enc.GetBlock(blocks - 1, blockSize);
- var mixBlock = enc.GetBlock(blocks - 2, blockSize);
-
- var padLength = FindPaddingLength(mixBlock, lastBlock, lastBlock.Length - 1, 0);
- Console.WriteLine("Padding : " + padLength + " (" + requests + " requests)");
-
- var res = new byte[blocks - 1][];
- Parallel.For<int>(1, blocks,
- () => 0,
- (ixx, loop, k) =>
- {
- res[ixx - 1] = DecryptBlock(enc, ixx, padLength);
- return k;
- },
- x => { });
-
- var result = res.SelectMany(x => x).ToList();
- result = result.TakeWhile((b, ix) => ix < (result.Count - padLength)).ToList();
-
- Console.WriteLine();
- Console.WriteLine("Requests: " + requests + " (" + ((double)requests / enc.Length) + " req/byte)");
- Console.WriteLine("Dec : " + UTF8Encoding.UTF8.GetString(result.ToArray(), 0, result.Count));
-
- var decrypted = MachineKeyWrapper.DecryptString(HttpEncode.UrlTokenEncode(encOnly));
- Console.WriteLine("Real : " + decrypted);
- }
- private static byte[] DecryptBlock(byte[] encrypted, int blockNum, int padLength)
+ public byte[] Encrypt(string clearText)
{
- var block = encrypted.GetBlock(blockNum, blockSize);
- var iv = encrypted.GetBlock(blockNum - 1, blockSize);
- var rnd = new Random();
- var newIv = (new byte[blockSize]).Select(x => (byte)rnd.Next(0, 255)).ToArray();
-
- var result = new byte[block.Length];
- for (var i = 1; i <= block.Length; i++)
- {
- var r = newIv[newIv.Length - i];
- byte lastByte = 0;
- int curByte = newIv.Length - i;
- for (var j = 255; j >= 0; j--)
- {
- newIv[curByte] = (byte)(r ^ j);
- if (IsPaddingValid(newIv, block))
- {
- lastByte = (byte)(newIv[curByte] ^ i ^ iv[curByte]);
- Console.WriteLine((blockSize - i).ToString("00") + ": " + lastByte.ToString("000") + " - rounds: " + (256 - j) + "");
- break;
- }
- }
- result[curByte] = (byte)lastByte;
- for (var k = 1; k <= i; k++)
- {
- newIv[result.Length - k] = (byte)((newIv[newIv.Length - k] ^ (i)) ^ (i + 1));
- }
- }
- return result;
+ var bytes = HttpServerUtility.UrlTokenDecode(MachineKeyWrapper.EncryptString(clearText));
+ return Enumerable.Repeat<byte>(0, Program.BlockSize).Concat(bytes).ToArray();
}
- private static bool IsPaddingValid(byte[] iv, byte[] byteArray)
+ public string Decrypt(byte[] iv, byte[] cipher)
{
- try
- {
- requests++;
- MachineKeyWrapper.DecryptString(HttpEncode.UrlTokenEncode(iv.Concat(byteArray).ToArray()));
- return true;
- }
- catch (Exception ex)
- {
- return false;
- }
-
+ return MachineKeyWrapper.DecryptString(HttpServerUtility.UrlTokenEncode(iv.Concat(cipher).ToArray()));
}
- private static int FindPaddingLength(byte[] iv, byte[] dec, int i, int j)
- {
- if (i == j) return i;
- var m = (int)Math.Ceiling((i + j) / 2.0);
- var ivm = iv.Select(x => x).ToArray();
- ivm[iv.Length - m] = (byte)(~ivm[iv.Length - m]);
- if (!IsPaddingValid(ivm, dec))
- {
- return FindPaddingLength(iv, dec, i, m);
- }
- return FindPaddingLength(iv, dec, m - 1, j);
- }
-
-
}
}
@@ -46,7 +46,8 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
- <Compile Include="HttpEncode.cs" />
+ <Compile Include="AesCrypt.cs" />
+ <Compile Include="ICrypt.cs" />
<Compile Include="MachineCrypt.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -12,65 +12,74 @@ namespace PoetAndDidntKnowIt
class Program
{
static int requests = 0;
+ public const int BlockSize = 16;
+ private static ICrypt cryptoProvider = new MachineCrypt();
static void Main(string[] args)
{
- MachineCrypt.Crypt();
- //AesCrypt();
+ var clearText = "This is a sample message";
+ byte[] enc = cryptoProvider.Encrypt(clearText);
- Console.ReadKey();
- }
-
-
-
- static void AesCrypt() {
-
- var aes = Aes.Create();
- aes.Mode = CipherMode.CBC;
- aes.GenerateIV();
- aes.GenerateKey();
- aes.Padding = PaddingMode.PKCS7;
- var encryptor = aes.CreateEncryptor();
- var clearText = Encoding.UTF8.GetBytes("Hello world! I am a bastard");
- var encrypted = encryptor.TransformFinalBlock(clearText, 0, clearText.Length);
-
- var enc = aes.IV.Concat(encrypted).ToArray();
+ var blocks = (int)Math.Ceiling((double)enc.Count() / BlockSize);
+ var lastBlock = enc.GetBlock(blocks - 1, BlockSize);
+ var mixBlock = enc.GetBlock(blocks - 2, BlockSize);
- var blocks = (int)Math.Ceiling(enc.Count() / 16.0);
- var lastBlock = enc.GetBlock(blocks - 1, 16);
- var mixBlock = enc.GetBlock(blocks - 2, 16);
-
- var padLength = FindPaddingLength(aes, mixBlock, lastBlock, lastBlock.Length - 1, 0);
+ var padLength = FindPaddingLength(mixBlock, lastBlock, lastBlock.Length - 1, 0);
Console.WriteLine("Padding : " + padLength + " (" + requests + " requests)");
- //var result = new List<byte>();
- var res = new byte[blocks -1][];
- Parallel.For<int>(1, blocks, () => 0, (ixx, loop, k) =>
- {
- res[ixx -1] = DecryptBlock(aes, enc, ixx, padLength);
- return k;
- }
- , x => { }
- );
+
+ var res = new byte[blocks - 1][];
+ Parallel.For<int>(1, blocks,
+ () => 0,
+ (ixx, loop, k) =>
+ {
+ res[ixx - 1] = DecryptBlock(enc, ixx, padLength);
+ return k;
+ },
+ x => { });
var result = res.SelectMany(x => x).ToList();
result = result.TakeWhile((b, ix) => ix < (result.Count - padLength)).ToList();
Console.WriteLine();
- Console.WriteLine("Requests: " + requests + " (" + ((double)requests / encrypted.Length) + " req/byte)");
- Console.WriteLine("Dec : " + UTF8Encoding.UTF8.GetString(result.ToArray(), 0, result.Count));
+ Console.WriteLine("Requests : " + requests + " (" + ((double)requests / enc.Length) + " req/byte)");
+ Console.WriteLine("Decrypted : " + UTF8Encoding.UTF8.GetString(result.ToArray(), 0, result.Count));
+
+ Console.ReadKey();
+ }
- var decrypted = aes.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
- Console.WriteLine("Real : " + UTF8Encoding.UTF8.GetString(decrypted, 0, decrypted.Length));
+ public static bool IsPaddingValid(byte[] iv, byte[] byteArray)
+ {
+ try
+ {
+ requests++;
+ cryptoProvider.Decrypt(iv, byteArray);
+ return true;
+ }
+ catch (Exception)
+ {
+ return false;
+ }
}
-
- private static byte[] DecryptBlock(Aes aes, byte[] encrypted, int blockNum, int padLength)
+ private static int FindPaddingLength(byte[] iv, byte[] dec, int i, int j)
{
- var block = encrypted.GetBlock(blockNum, 16);
- var iv = encrypted.GetBlock(blockNum - 1, 16);
+ if (i == j) return i;
+ var m = (int)Math.Ceiling((i + j) / 2.0);
+ var ivm = iv.Select(x => x).ToArray();
+ ivm[iv.Length - m] = (byte)(~ivm[iv.Length - m]);
+ if (!IsPaddingValid(ivm, dec))
+ {
+ return FindPaddingLength(iv, dec, i, m);
+ }
+ return FindPaddingLength(iv, dec, m - 1, j);
+ }
+ private static byte[] DecryptBlock(byte[] encrypted, int blockNum, int padLength)
+ {
+ var block = encrypted.GetBlock(blockNum, BlockSize);
+ var iv = encrypted.GetBlock(blockNum - 1, BlockSize);
var rnd = new Random();
- var newIv = aes.IV.Select(x => (byte) rnd.Next(0, 255)).ToArray();
+ var newIv = (new byte[BlockSize]).Select(x => (byte)rnd.Next(0, 255)).ToArray();
var result = new byte[block.Length];
for (var i = 1; i <= block.Length; i++)
@@ -81,10 +90,10 @@ private static byte[] DecryptBlock(Aes aes, byte[] encrypted, int blockNum, int
for (var j = 255; j >= 0; j--)
{
newIv[curByte] = (byte)(r ^ j);
- if (IsPaddingValid(aes.CreateDecryptor(aes.Key, newIv), block))
+ if (IsPaddingValid(newIv, block))
{
lastByte = (byte)(newIv[curByte] ^ i ^ iv[curByte]);
- Console.WriteLine((16 - i).ToString("00") + ": " + lastByte.ToString("000") + " - rounds: " + (256 -j) + "");
+ Console.WriteLine((BlockSize - i).ToString("00") + ": " + lastByte.ToString("000") + " - rounds: " + (256 - j) + "");
break;
}
}
@@ -96,36 +105,6 @@ private static byte[] DecryptBlock(Aes aes, byte[] encrypted, int blockNum, int
}
return result;
}
- private static bool IsPaddingValid(ICryptoTransform decryptor, byte[] byteArray)
- {
- try
- {
- lock (typeof(Program))
- {
- requests++;
- }
- decryptor.TransformFinalBlock(byteArray, 0, byteArray.Length);
- return true;
- }
- catch(CryptographicException ex)
- {
- return false;
- }
-
- }
- private static int FindPaddingLength(Aes aes, byte[] iv, byte[] dec, int i, int j)
- {
- if (i == j) return i;
- var m = (int)Math.Ceiling((i + j)/2.0);
- var ivm = iv.Select(x => x).ToArray();
- ivm[iv.Length - m] = (byte)(~ivm[iv.Length - m]);
- if (!IsPaddingValid(aes.CreateDecryptor(aes.Key, ivm), dec))
- {
- return FindPaddingLength(aes, iv, dec, i, m);
- }
- return FindPaddingLength(aes, iv, dec, m-1, j);
- }
-
}
public static class ByteHelper

0 comments on commit fc430f4

Please sign in to comment.