diff --git a/Program.cs b/Program.cs index 0ccb554..8569174 100644 --- a/Program.cs +++ b/Program.cs @@ -9,34 +9,22 @@ using System; using System.IO; using System.Text; -using System.Runtime.InteropServices; -using System.Reflection; using Tea; using Utils; -public class dllimport -{ - Assembly asm = Assembly.LoadFrom(@".\base64.dll"); - - //public string test(string str) - //{ - // return Base64Encode(Encoding.UTF8.GetBytes(str), 8); - //} -} namespace ModelCoder { class Program { private static IUtil util; private static ITeaCoding tea; - private static dllimport dlltest; private static readonly uint blockSize = 12; private static readonly uint blockNums = 3000; private static readonly uint encodeSize = blockSize * blockNums; + const int byteCount = 4; private static string checkBlockBytes(byte block) { - const int byteCount = 8; string newBlock = block.ToString(); int size = Encoding.UTF8.GetByteCount(newBlock); //Console.WriteLine("newBlock: " + newBlock); @@ -61,7 +49,8 @@ namespace ModelCoder } #endregion - /*string[] test = {"22\0\0\0\0\0\0", "1\0\0\0\0\0\0\0"}; + /* + string[] test = {"22\0\0\0\0\0\0", "1\0\0\0\0\0\0\0"}; foreach (var i in test) { Console.WriteLine(i + " bytes: " + Encoding.UTF8.GetByteCount(i)); @@ -72,41 +61,51 @@ namespace ModelCoder Console.WriteLine("first file block size: " + Encoding.UTF8.GetByteCount(blockCheck)); blockCheck = util.Base64Encode(blockCheck); Console.WriteLine("first file block b64e: " + blockCheck); - Console.WriteLine("first file block b64d: " + util.Base64Decode(blockCheck));*/ + Console.WriteLine("first file block b64d: " + util.Base64Decode(blockCheck)); + */ // TEA and Base64 encoding - var block = new uint[2]; - string result = ""; - for (uint i = 0; i < 10; i += 2) + var block1 = new uint[2]; + string result = string.Empty; + for (uint i = 0; i < 8; i += 2) { - block[0] = Convert.ToUInt32(checkBlockBytes(file_bytes[i])); - block[1] = Convert.ToUInt32(checkBlockBytes(file_bytes[i + 1])); + //block1[0] = file_bytes[i]; + //block1[1] = file_bytes[i + 1]; - Console.WriteLine("st block0: " + block[0]); - Console.WriteLine("st block1: " + block[1]); + block1[0] = util.ConvertStringToUInt(checkBlockBytes(file_bytes[i])); + block1[1] = util.ConvertStringToUInt(checkBlockBytes(file_bytes[i + 1])); - tea.encode(block, key); + //block1[0] = Convert.ToUInt32(checkBlockBytes(file_bytes[i])); + //block1[1] = Convert.ToUInt32(checkBlockBytes(file_bytes[i + 1])); - Console.WriteLine("enc block0: " + block[0]); - Console.WriteLine("enc block1: " + block[1]); + Console.WriteLine(); - Console.WriteLine("block0 bytes:"); - foreach (var j in BitConverter.GetBytes(block[0])) + foreach (var b1 in block1) + Console.WriteLine("st block1: " + b1); + + tea.encode(block1, key); + + foreach (var b1 in block1) + Console.WriteLine("enc block1: " + b1); + + Console.WriteLine("block1[0] bytes:"); + foreach (var j in BitConverter.GetBytes(block1[0])) Console.WriteLine(" " + j); - Console.WriteLine("block1 bytes:"); - foreach (var j in BitConverter.GetBytes(block[1])) + Console.WriteLine("block1[1] bytes:"); + foreach (var j in BitConverter.GetBytes(block1[1])) Console.WriteLine(" " + j); - - Console.WriteLine(util.Base64Encode(block[0].ToString())); - Console.WriteLine(util.Base64Encode(block[1].ToString())); + + //Console.WriteLine(util.Base64Encode(block1[0].ToString())); + //Console.WriteLine(util.Base64Encode(block1[1].ToString())); + //Console.WriteLine("b64: " + util.Base64Encode(block1[0].ToString() + block1[1].ToString())); Console.WriteLine(); //result += util.Base64Encode(BitConverter.GetBytes(block[0])) + // util.Base64Encode(BitConverter.GetBytes(block[1])); - result += util.Base64Encode(block[0].ToString()) + - util.Base64Encode(block[1].ToString()); + result += util.Base64Encode(block1[0].ToString()) + + util.Base64Encode(block1[1].ToString()); } Console.WriteLine("Result: " + result); Console.WriteLine("[OUTPUT] File '" + path + "' has been TEA and Base64 encoded"); @@ -132,8 +131,9 @@ namespace ModelCoder { util = new Util(); tea = new TeaCoding(); - dlltest = new dllimport(); - //Console.WriteLine(dlltest.test("22")); + + Console.WriteLine(util.Base64Decode("osz/GcOVC5s=")); + Console.WriteLine(util.Base64Decode("XsXA4M0B2X0=")); string fpath; if (args.Length > 0) diff --git a/model_coder.Settings b/model_coder.Settings deleted file mode 100644 index 6f1dade..0000000 --- a/model_coder.Settings +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/model_coder.csproj b/model_coder.csproj index 9dd7531..27d88f3 100644 --- a/model_coder.csproj +++ b/model_coder.csproj @@ -48,10 +48,6 @@ TRACE - - base64.dll - True - 4.0 diff --git a/model_coder.sdsettings b/model_coder.sdsettings deleted file mode 100644 index 38f7991..0000000 --- a/model_coder.sdsettings +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/tea.cs b/tea.cs index dc27678..0d78908 100644 --- a/tea.cs +++ b/tea.cs @@ -23,13 +23,14 @@ namespace Tea public void encode(uint[] v, uint[] k) { uint v0 = v[0], v1 = v[1]; uint sum = 0; - for (int i = 0; i < 32; i++) { + unchecked { + for (uint i = 0; i < 32; i++) { //for (uint i = 32; i > 0; i--) { - unchecked { + // unchecked { v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); sum += c_delta; v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]); - } + } //sum += c_delta; //v0 += (v1 << 4) + k[0] ^ v1 + sum ^ (v1 >> 5) + k[1]; //v1 += (v0 << 4) + k[2] ^ v0 + sum ^ (v0 >> 5) + k[3]; diff --git a/test/partial_file_encode b/test/partial_file_encode index 45d633a..44eceb3 100644 Binary files a/test/partial_file_encode and b/test/partial_file_encode differ diff --git a/test/partial_file_encode.enc b/test/partial_file_encode.enc index f166b4f..1ebf3a8 100644 Binary files a/test/partial_file_encode.enc and b/test/partial_file_encode.enc differ diff --git a/utils.cs b/utils.cs index af01b0c..8dab825 100644 --- a/utils.cs +++ b/utils.cs @@ -15,34 +15,15 @@ namespace Utils { public interface IUtil { - uint[] ConvertKey(string input); uint ConvertStringToUInt(string input); string ConvertUIntToString(uint input); + uint[] ConvertKey(string input); string Base64Encode(string plainText); //string Base64Encode(byte[] plainText); string Base64Decode(string base64EncodedData); } public partial class Util: IUtil { - #region Convert Key to MD5, then to UInt - public uint[] ConvertKey(string input) - { - // MD5 Hashing - var md5 = MD5.Create(); - byte[] inputBytes = Encoding.ASCII.GetBytes(input); - byte[] hashBytes = md5.ComputeHash(inputBytes); - var sb = new StringBuilder(); - for (int i = 0; i < hashBytes.Length; i++) - sb.Append(hashBytes[i].ToString("X2")); - // Converting to uint[] - string key = sb.ToString().ToLower().Substring(0, 16); - var formattedKey = new uint[4]; - int j = 0; - for (int i = 0; i < key.Length; i += 4) - formattedKey[j++] = ConvertStringToUInt(key.Substring(i, 4)); - return formattedKey; - } - #endregion #region Convert String to UInt and reverse public uint ConvertStringToUInt(string input) { @@ -63,6 +44,25 @@ namespace Utils return output.ToString(); } #endregion + #region Convert Key to MD5, then to UInt + public uint[] ConvertKey(string input) + { + // MD5 Hashing + var md5 = MD5.Create(); + byte[] inputBytes = Encoding.ASCII.GetBytes(input); + byte[] hashBytes = md5.ComputeHash(inputBytes); + var sb = new StringBuilder(); + for (int i = 0; i < hashBytes.Length; i++) + sb.Append(hashBytes[i].ToString("X2")); + // Converting to uint[] + string key = sb.ToString().ToLower().Substring(0, 16); + var formattedKey = new uint[4]; + int j = 0; + for (int i = 0; i < key.Length; i += 4) + formattedKey[j++] = ConvertStringToUInt(key.Substring(i, 4)); + return formattedKey; + } + #endregion #region Base64 encoding/decoding public string Base64Encode(string plainText) { @@ -71,110 +71,12 @@ namespace Utils } /*public string Base64Encode(byte[] data) return Convert.ToBase64String(data); - public char[] Base64Encode(byte[] data) - { - int length, length2; - int blockCount; - int paddingCount; - - length = data.Length; - - if ((length % 3) == 0) - { - paddingCount = 0; - blockCount = length / 3; - } - else - { - paddingCount = 3 - (length % 3); - blockCount = (length + paddingCount) / 3; - } - - length2 = length + paddingCount; - - Console.WriteLine("length: " + length); - Console.WriteLine("length2: " + length2); - Console.WriteLine("blockCount: " + blockCount); - Console.WriteLine("paddingCount: " + paddingCount); - - byte[] source2; - source2 = new byte[length2]; - - for (int x = 0; x < length2; x++) - source2[x] = x < length ? data[x] : (byte)0; - - byte b1, b2, b3; - byte temp, temp1, temp2, temp3, temp4; - var buffer = new byte[blockCount * 4]; - var result = new char[blockCount * 4]; - - for (int x = 0; x < blockCount; x++) - { - b1 = source2[x * 3]; - b2 = source2[x * 3 + 1]; - b3 = source2[x * 3 + 2]; - - temp1 = (byte)((b1 & 252) >> 2); - - temp = (byte)((b1 & 3) << 4); - temp2 = (byte)((b2 & 240) >> 4); - temp2 += temp; - - temp = (byte)((b2 & 15) << 2); - temp3 = (byte)((b3 & 192) >> 6); - temp3 += temp; - - temp4 = (byte)(b3 & 63); - - buffer[x * 4] = temp1; - buffer[x * 4 + 1] = temp2; - buffer[x * 4 + 2] = temp3; - buffer[x * 4 + 3] = temp4; - } - - for (int x = 0; x < blockCount * 4; x++) - result[x] = SixBitToChar(buffer[x]); - - switch (paddingCount) - { - case 0: - break; - case 1: - result[blockCount * 4 - 1] = '='; - break; - case 2: - result[blockCount * 4 - 1] = '='; - result[blockCount * 4 - 2] = '='; - break; - default: - break; - } - - return result; - } - private static char SixBitToChar(byte b) - { - var lookupTable = new char[64] { - 'A','B','C','D','E','F','G','H','I','J','K','L','M', - 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z', - 'a','b','c','d','e','f','g','h','i','j','k','l','m', - 'n','o','p','q','r','s','t','u','v','w','x','y','z', - '0','1','2','3','4','5','6','7','8','9','+','/' - }; - - if ((b >= 0) && (b <= 63)) - { - return lookupTable[(int)b]; - } - else - { - return ' '; - } - }*/ - + */ public string Base64Decode(string base64EncodedData) { var base64EncodedBytes = Convert.FromBase64String(base64EncodedData); + foreach (var i in base64EncodedBytes) + Console.WriteLine(" " + i); return Encoding.UTF8.GetString(base64EncodedBytes); } #endregion