Signed-off-by: JDM170 <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2020-10-11 22:04:01 +07:00
parent f50aca6691
commit abc57ae392
8 changed files with 63 additions and 167 deletions

View File

@@ -9,34 +9,22 @@
using System; using System;
using System.IO; using System.IO;
using System.Text; using System.Text;
using System.Runtime.InteropServices;
using System.Reflection;
using Tea; using Tea;
using Utils; 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 namespace ModelCoder
{ {
class Program class Program
{ {
private static IUtil util; private static IUtil util;
private static ITeaCoding tea; private static ITeaCoding tea;
private static dllimport dlltest;
private static readonly uint blockSize = 12; private static readonly uint blockSize = 12;
private static readonly uint blockNums = 3000; private static readonly uint blockNums = 3000;
private static readonly uint encodeSize = blockSize * blockNums; private static readonly uint encodeSize = blockSize * blockNums;
const int byteCount = 4;
private static string checkBlockBytes(byte block) private static string checkBlockBytes(byte block)
{ {
const int byteCount = 8;
string newBlock = block.ToString(); string newBlock = block.ToString();
int size = Encoding.UTF8.GetByteCount(newBlock); int size = Encoding.UTF8.GetByteCount(newBlock);
//Console.WriteLine("newBlock: " + newBlock); //Console.WriteLine("newBlock: " + newBlock);
@@ -61,7 +49,8 @@ namespace ModelCoder
} }
#endregion #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) foreach (var i in test)
{ {
Console.WriteLine(i + " bytes: " + Encoding.UTF8.GetByteCount(i)); Console.WriteLine(i + " bytes: " + Encoding.UTF8.GetByteCount(i));
@@ -72,41 +61,51 @@ namespace ModelCoder
Console.WriteLine("first file block size: " + Encoding.UTF8.GetByteCount(blockCheck)); Console.WriteLine("first file block size: " + Encoding.UTF8.GetByteCount(blockCheck));
blockCheck = util.Base64Encode(blockCheck); blockCheck = util.Base64Encode(blockCheck);
Console.WriteLine("first file block b64e: " + 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 // TEA and Base64 encoding
var block = new uint[2]; var block1 = new uint[2];
string result = ""; string result = string.Empty;
for (uint i = 0; i < 10; i += 2) for (uint i = 0; i < 8; i += 2)
{ {
block[0] = Convert.ToUInt32(checkBlockBytes(file_bytes[i])); //block1[0] = file_bytes[i];
block[1] = Convert.ToUInt32(checkBlockBytes(file_bytes[i + 1])); //block1[1] = file_bytes[i + 1];
Console.WriteLine("st block0: " + block[0]); block1[0] = util.ConvertStringToUInt(checkBlockBytes(file_bytes[i]));
Console.WriteLine("st block1: " + block[1]); 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();
Console.WriteLine("enc block1: " + block[1]);
Console.WriteLine("block0 bytes:"); foreach (var b1 in block1)
foreach (var j in BitConverter.GetBytes(block[0])) 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(" " + j);
Console.WriteLine("block1 bytes:"); Console.WriteLine("block1[1] bytes:");
foreach (var j in BitConverter.GetBytes(block[1])) foreach (var j in BitConverter.GetBytes(block1[1]))
Console.WriteLine(" " + j); Console.WriteLine(" " + j);
Console.WriteLine(util.Base64Encode(block[0].ToString())); //Console.WriteLine(util.Base64Encode(block1[0].ToString()));
Console.WriteLine(util.Base64Encode(block[1].ToString())); //Console.WriteLine(util.Base64Encode(block1[1].ToString()));
//Console.WriteLine("b64: " + util.Base64Encode(block1[0].ToString() + block1[1].ToString()));
Console.WriteLine(); Console.WriteLine();
//result += util.Base64Encode(BitConverter.GetBytes(block[0])) + //result += util.Base64Encode(BitConverter.GetBytes(block[0])) +
// util.Base64Encode(BitConverter.GetBytes(block[1])); // util.Base64Encode(BitConverter.GetBytes(block[1]));
result += util.Base64Encode(block[0].ToString()) + result += util.Base64Encode(block1[0].ToString()) +
util.Base64Encode(block[1].ToString()); util.Base64Encode(block1[1].ToString());
} }
Console.WriteLine("Result: " + result); Console.WriteLine("Result: " + result);
Console.WriteLine("[OUTPUT] File '" + path + "' has been TEA and Base64 encoded"); Console.WriteLine("[OUTPUT] File '" + path + "' has been TEA and Base64 encoded");
@@ -132,8 +131,9 @@ namespace ModelCoder
{ {
util = new Util(); util = new Util();
tea = new TeaCoding(); tea = new TeaCoding();
dlltest = new dllimport();
//Console.WriteLine(dlltest.test("22")); Console.WriteLine(util.Base64Decode("osz/GcOVC5s="));
Console.WriteLine(util.Base64Decode("XsXA4M0B2X0="));
string fpath; string fpath;
if (args.Length > 0) if (args.Length > 0)

View File

@@ -1 +0,0 @@
<OpenCoverSettings />

View File

@@ -48,10 +48,6 @@
<DefineConstants>TRACE</DefineConstants> <DefineConstants>TRACE</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="base64">
<HintPath>base64.dll</HintPath>
<EmbedInteropTypes>True</EmbedInteropTypes>
</Reference>
<Reference Include="Microsoft.CSharp"> <Reference Include="Microsoft.CSharp">
<RequiredTargetFramework>4.0</RequiredTargetFramework> <RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference> </Reference>

View File

@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<Properties />

7
tea.cs
View File

@@ -23,13 +23,14 @@ namespace Tea
public void encode(uint[] v, uint[] k) { public void encode(uint[] v, uint[] k) {
uint v0 = v[0], v1 = v[1]; uint v0 = v[0], v1 = v[1];
uint sum = 0; 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--) { //for (uint i = 32; i > 0; i--) {
unchecked { // unchecked {
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]); v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
sum += c_delta; sum += c_delta;
v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]); v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
} }
//sum += c_delta; //sum += c_delta;
//v0 += (v1 << 4) + k[0] ^ v1 + sum ^ (v1 >> 5) + k[1]; //v0 += (v1 << 4) + k[0] ^ v1 + sum ^ (v1 >> 5) + k[1];
//v1 += (v0 << 4) + k[2] ^ v0 + sum ^ (v0 >> 5) + k[3]; //v1 += (v0 << 4) + k[2] ^ v0 + sum ^ (v0 >> 5) + k[3];

Binary file not shown.

Binary file not shown.

144
utils.cs
View File

@@ -15,34 +15,15 @@ namespace Utils
{ {
public interface IUtil public interface IUtil
{ {
uint[] ConvertKey(string input);
uint ConvertStringToUInt(string input); uint ConvertStringToUInt(string input);
string ConvertUIntToString(uint input); string ConvertUIntToString(uint input);
uint[] ConvertKey(string input);
string Base64Encode(string plainText); string Base64Encode(string plainText);
//string Base64Encode(byte[] plainText); //string Base64Encode(byte[] plainText);
string Base64Decode(string base64EncodedData); string Base64Decode(string base64EncodedData);
} }
public partial class Util: IUtil 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 #region Convert String to UInt and reverse
public uint ConvertStringToUInt(string input) public uint ConvertStringToUInt(string input)
{ {
@@ -63,6 +44,25 @@ namespace Utils
return output.ToString(); return output.ToString();
} }
#endregion #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 #region Base64 encoding/decoding
public string Base64Encode(string plainText) public string Base64Encode(string plainText)
{ {
@@ -71,110 +71,12 @@ namespace Utils
} }
/*public string Base64Encode(byte[] data) /*public string Base64Encode(byte[] data)
return Convert.ToBase64String(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) public string Base64Decode(string base64EncodedData)
{ {
var base64EncodedBytes = Convert.FromBase64String(base64EncodedData); var base64EncodedBytes = Convert.FromBase64String(base64EncodedData);
foreach (var i in base64EncodedBytes)
Console.WriteLine(" " + i);
return Encoding.UTF8.GetString(base64EncodedBytes); return Encoding.UTF8.GetString(base64EncodedBytes);
} }
#endregion #endregion