mirror of
https://github.com/JDM170/model_coder
synced 2025-12-10 05:57:19 +07:00
Code changes, added base64 proj on c++
Signed-off-by: JDM170 <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
131
Program.cs
131
Program.cs
@@ -8,17 +8,49 @@
|
|||||||
*/
|
*/
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
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 readonly int encodeSize = 3000;
|
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;
|
||||||
|
|
||||||
|
private static string checkBlockBytes(byte block)
|
||||||
|
{
|
||||||
|
const int byteCount = 8;
|
||||||
|
string newBlock = block.ToString();
|
||||||
|
int size = Encoding.UTF8.GetByteCount(newBlock);
|
||||||
|
//Console.WriteLine("newBlock: " + newBlock);
|
||||||
|
//Console.WriteLine("newBlock size: " + size);
|
||||||
|
if (size < byteCount)
|
||||||
|
for (int i = 0; i < (byteCount - size); i++)
|
||||||
|
newBlock += '\0';
|
||||||
|
//Console.WriteLine("newblock size: " + System.Text.ASCIIEncoding.UTF8.GetByteCount(newBlock));
|
||||||
|
return newBlock;
|
||||||
|
}
|
||||||
|
|
||||||
private static bool encodeFile(string path, uint[] key)
|
private static bool encodeFile(string path, uint[] key)
|
||||||
{
|
{
|
||||||
|
#region Reading file
|
||||||
Console.WriteLine("[OUTPUT] Reading: " + path);
|
Console.WriteLine("[OUTPUT] Reading: " + path);
|
||||||
byte[] file_bytes;
|
byte[] file_bytes;
|
||||||
try {
|
try {
|
||||||
@@ -27,16 +59,70 @@ namespace ModelCoder
|
|||||||
Console.WriteLine("[OUTPUT] File '" + path + "' not found");
|
Console.WriteLine("[OUTPUT] File '" + path + "' not found");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
ITeaCoding tea = new TeaCoding();
|
|
||||||
var block = new uint[2];
|
/*string[] test = {"22\0\0\0\0\0\0", "1\0\0\0\0\0\0\0"};
|
||||||
for (uint i = 0; i < encodeSize; i += 2) {
|
foreach (var i in test)
|
||||||
Console.WriteLine(i + " start iteration");
|
{
|
||||||
block[0] = file_bytes[i];
|
Console.WriteLine(i + " bytes: " + Encoding.UTF8.GetByteCount(i));
|
||||||
block[1] = file_bytes[i + 1];
|
Console.WriteLine(i + " b64: " + util.Base64Encode(i));
|
||||||
Console.WriteLine(i + " start encoding");
|
|
||||||
tea.code(block, key);
|
|
||||||
}
|
}
|
||||||
|
string blockCheck = checkBlockBytes(file_bytes[0]);
|
||||||
|
Console.WriteLine("first file block: " + blockCheck);
|
||||||
|
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));*/
|
||||||
|
|
||||||
|
// TEA and Base64 encoding
|
||||||
|
var block = new uint[2];
|
||||||
|
string result = "";
|
||||||
|
for (uint i = 0; i < 10; i += 2)
|
||||||
|
{
|
||||||
|
block[0] = Convert.ToUInt32(checkBlockBytes(file_bytes[i]));
|
||||||
|
block[1] = Convert.ToUInt32(checkBlockBytes(file_bytes[i + 1]));
|
||||||
|
|
||||||
|
Console.WriteLine("st block0: " + block[0]);
|
||||||
|
Console.WriteLine("st block1: " + block[1]);
|
||||||
|
|
||||||
|
tea.encode(block, key);
|
||||||
|
|
||||||
|
Console.WriteLine("enc block0: " + block[0]);
|
||||||
|
Console.WriteLine("enc block1: " + block[1]);
|
||||||
|
|
||||||
|
Console.WriteLine("block0 bytes:");
|
||||||
|
foreach (var j in BitConverter.GetBytes(block[0]))
|
||||||
|
Console.WriteLine(" " + j);
|
||||||
|
|
||||||
|
Console.WriteLine("block1 bytes:");
|
||||||
|
foreach (var j in BitConverter.GetBytes(block[1]))
|
||||||
|
Console.WriteLine(" " + j);
|
||||||
|
|
||||||
|
Console.WriteLine(util.Base64Encode(block[0].ToString()));
|
||||||
|
Console.WriteLine(util.Base64Encode(block[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());
|
||||||
|
}
|
||||||
|
Console.WriteLine("Result: " + result);
|
||||||
|
Console.WriteLine("[OUTPUT] File '" + path + "' has been TEA and Base64 encoded");
|
||||||
|
|
||||||
|
#region Writing file with encoded and non-encoded part
|
||||||
|
string encodedPath = path + ".enc";
|
||||||
|
// encoded part
|
||||||
|
File.WriteAllText(encodedPath, result);
|
||||||
|
// non-encoded part
|
||||||
|
FileStream ofile = File.OpenWrite(encodedPath);
|
||||||
|
ofile.Seek(encodeSize, 0);
|
||||||
|
for (uint i = blockNums; i < file_bytes.Length; i++)
|
||||||
|
ofile.WriteByte(file_bytes[i]);
|
||||||
|
ofile.Close();
|
||||||
|
Console.WriteLine("[OUTPUT] File '" + encodedPath + "' successfully written");
|
||||||
|
#endregion
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -44,28 +130,31 @@ namespace ModelCoder
|
|||||||
[STAThread]
|
[STAThread]
|
||||||
static void Main(string[] args)
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
IUtil util = new Util();
|
util = new Util();
|
||||||
|
tea = new TeaCoding();
|
||||||
|
dlltest = new dllimport();
|
||||||
|
//Console.WriteLine(dlltest.test("22"));
|
||||||
|
|
||||||
string fpath;
|
string fpath;
|
||||||
if (args.Length > 0) {
|
if (args.Length > 0)
|
||||||
fpath = args[0];
|
fpath = args[0];
|
||||||
} else {
|
else
|
||||||
Console.WriteLine("[OUTPUT] You can open file(s) with this program");
|
{
|
||||||
Console.WriteLine("[OUTPUT] Or drag'n'drop on it");
|
Console.WriteLine("[OUTPUT] You can open file(s) with this program");
|
||||||
Console.Write("[INPUT] Enter filename (without spaces): ");
|
Console.WriteLine("[OUTPUT] Or drag'n'drop on it");
|
||||||
fpath = Console.ReadLine();
|
Console.Write("[INPUT] Enter filename (without spaces): ");
|
||||||
|
fpath = Console.ReadLine();
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.Write("[INPUT] Enter key: ");
|
Console.Write("[INPUT] Enter key: ");
|
||||||
uint[] uKey = util.ConvertKey(Console.ReadLine());
|
uint[] uKey = util.ConvertKey(Console.ReadLine());
|
||||||
|
|
||||||
Console.WriteLine("[OUTPUT] Reading file(s)...");
|
Console.WriteLine("[OUTPUT] Reading file(s)...");
|
||||||
if (args.Length > 1) {
|
if (args.Length > 0)
|
||||||
foreach (string i in args)
|
foreach (string i in args)
|
||||||
encodeFile(i, uKey);
|
encodeFile(i, uKey);
|
||||||
} else {
|
else
|
||||||
encodeFile(fpath, uKey);
|
encodeFile(fpath, uKey);
|
||||||
}
|
|
||||||
|
|
||||||
Console.Write("Press any key to continue . . . ");
|
Console.Write("Press any key to continue . . . ");
|
||||||
Console.ReadKey(true);
|
Console.ReadKey(true);
|
||||||
|
|||||||
51
base64/base64.cbp
Normal file
51
base64/base64.cbp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_project_file>
|
||||||
|
<FileVersion major="1" minor="6" />
|
||||||
|
<Project>
|
||||||
|
<Option title="base64" />
|
||||||
|
<Option pch_mode="2" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Build>
|
||||||
|
<Target title="Debug">
|
||||||
|
<Option output="bin/Debug/base64" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj/Debug/" />
|
||||||
|
<Option type="3" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Option createDefFile="1" />
|
||||||
|
<Option createStaticLib="1" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-Wall" />
|
||||||
|
<Add option="-DBUILD_DLL" />
|
||||||
|
<Add option="-g" />
|
||||||
|
</Compiler>
|
||||||
|
<Linker>
|
||||||
|
<Add library="user32" />
|
||||||
|
</Linker>
|
||||||
|
</Target>
|
||||||
|
<Target title="Release">
|
||||||
|
<Option output="bin/Release/base64" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj/Release/" />
|
||||||
|
<Option type="3" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Option createDefFile="1" />
|
||||||
|
<Option createStaticLib="1" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-Wall" />
|
||||||
|
<Add option="-DBUILD_DLL" />
|
||||||
|
<Add option="-O2" />
|
||||||
|
</Compiler>
|
||||||
|
<Linker>
|
||||||
|
<Add option="-s" />
|
||||||
|
<Add library="user32" />
|
||||||
|
</Linker>
|
||||||
|
</Target>
|
||||||
|
</Build>
|
||||||
|
<Unit filename="base64.cpp" />
|
||||||
|
<Unit filename="base64.h" />
|
||||||
|
<Unit filename="main.cpp" />
|
||||||
|
<Unit filename="main.h" />
|
||||||
|
<Extensions>
|
||||||
|
<lib_finder disable_auto="1" />
|
||||||
|
</Extensions>
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
||||||
93
base64/base64.cpp
Normal file
93
base64/base64.cpp
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
#include "base64.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
static const std::string base64_chars =
|
||||||
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
"abcdefghijklmnopqrstuvwxyz"
|
||||||
|
"0123456789+/";
|
||||||
|
|
||||||
|
|
||||||
|
static inline bool is_base64(BYTE c) {
|
||||||
|
return (isalnum(c) || (c == '+') || (c == '/'));
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string base64encode(BYTE const* buf, unsigned int bufLen) {
|
||||||
|
std::string ret;
|
||||||
|
int i = 0, j = 0;
|
||||||
|
BYTE char_array_3[3], char_array_4[4];
|
||||||
|
|
||||||
|
while(bufLen--) {
|
||||||
|
char_array_3[i++] = *(buf++);
|
||||||
|
if(i == 3) {
|
||||||
|
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||||
|
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||||
|
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||||
|
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||||
|
|
||||||
|
for(i = 0; i < 4; i++)
|
||||||
|
ret += base64_chars[char_array_4[i]];
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i) {
|
||||||
|
for(j = i; j < 3; j++)
|
||||||
|
char_array_3[j] = '\0';
|
||||||
|
|
||||||
|
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||||
|
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||||
|
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||||
|
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||||
|
|
||||||
|
for(j = 0; j < (i + 1); j++)
|
||||||
|
ret += base64_chars[char_array_4[j]];
|
||||||
|
|
||||||
|
while(i++ < 3)
|
||||||
|
ret += '=';
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<BYTE> base64decode(std::string const& encoded_string) {
|
||||||
|
int in_len = encoded_string.size(),
|
||||||
|
i = 0,
|
||||||
|
j = 0,
|
||||||
|
in_ = 0;
|
||||||
|
BYTE char_array_4[4], char_array_3[3];
|
||||||
|
std::vector<BYTE> ret;
|
||||||
|
|
||||||
|
while(in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
|
||||||
|
char_array_4[i++] = encoded_string[in_];
|
||||||
|
in_++;
|
||||||
|
if(i == 4) {
|
||||||
|
for(i = 0; i < 4; i++)
|
||||||
|
char_array_4[i] = base64_chars.find(char_array_4[i]);
|
||||||
|
|
||||||
|
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||||
|
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||||
|
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||||
|
|
||||||
|
for(i = 0; i < 3; i++)
|
||||||
|
ret.push_back(char_array_3[i]);
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(i) {
|
||||||
|
for(j = i; j < 4; j++)
|
||||||
|
char_array_4[j] = 0;
|
||||||
|
|
||||||
|
for(j = 0; j < 4; j++)
|
||||||
|
char_array_4[j] = base64_chars.find(char_array_4[j]);
|
||||||
|
|
||||||
|
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
|
||||||
|
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
|
||||||
|
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
|
||||||
|
|
||||||
|
for(j = 0; j < (i - 1); j++)
|
||||||
|
ret.push_back(char_array_3[j]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
17
base64/base64.depend
Normal file
17
base64/base64.depend
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# depslib dependency file v1.0
|
||||||
|
1600530854 source:c:\users\jdm17\desktop\base64\base64.cpp
|
||||||
|
"base64.h"
|
||||||
|
<iostream>
|
||||||
|
|
||||||
|
1600530854 c:\users\jdm17\desktop\base64\base64.h
|
||||||
|
<vector>
|
||||||
|
<string>
|
||||||
|
|
||||||
|
1601296249 source:c:\users\jdm17\desktop\base64\main.cpp
|
||||||
|
"main.h"
|
||||||
|
"base64.h"
|
||||||
|
|
||||||
|
1601297599 c:\users\jdm17\desktop\base64\main.h
|
||||||
|
<windows.h>
|
||||||
|
<string>
|
||||||
|
|
||||||
11
base64/base64.h
Normal file
11
base64/base64.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef _BASE64_H_
|
||||||
|
#define _BASE64_H_
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
typedef unsigned char BYTE;
|
||||||
|
|
||||||
|
std::string base64encode(BYTE const*, unsigned int);
|
||||||
|
std::vector<BYTE> base64decode(std::string const&);
|
||||||
|
|
||||||
|
#endif
|
||||||
20
base64/base64.layout
Normal file
20
base64/base64.layout
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_layout_file>
|
||||||
|
<FileVersion major="1" minor="0" />
|
||||||
|
<ActiveTarget name="Release" />
|
||||||
|
<File name="main.h" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="334" topLine="6" />
|
||||||
|
</Cursor>
|
||||||
|
</File>
|
||||||
|
<File name="main.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="242" topLine="0" />
|
||||||
|
</Cursor>
|
||||||
|
</File>
|
||||||
|
<File name="base64.cpp" open="0" top="0" tabpos="0" split="0" active="1" splitpos="0" zoom_1="0" zoom_2="0">
|
||||||
|
<Cursor>
|
||||||
|
<Cursor1 position="959" topLine="0" />
|
||||||
|
</Cursor>
|
||||||
|
</File>
|
||||||
|
</CodeBlocks_layout_file>
|
||||||
37
base64/main.cpp
Normal file
37
base64/main.cpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#include "main.h"
|
||||||
|
#include "base64.h"
|
||||||
|
|
||||||
|
// a sample exported function
|
||||||
|
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
|
||||||
|
{
|
||||||
|
MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" DLL_EXPORT std::string Base64Encode(BYTE const* buf, unsigned int bufLen)
|
||||||
|
{
|
||||||
|
return base64encode(buf, bufLen);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
|
||||||
|
{
|
||||||
|
switch (fdwReason)
|
||||||
|
{
|
||||||
|
case DLL_PROCESS_ATTACH:
|
||||||
|
// attach to process
|
||||||
|
// return FALSE to fail DLL load
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DLL_PROCESS_DETACH:
|
||||||
|
// detach from process
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DLL_THREAD_ATTACH:
|
||||||
|
// attach to thread
|
||||||
|
break;
|
||||||
|
|
||||||
|
case DLL_THREAD_DETACH:
|
||||||
|
// detach from thread
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return TRUE; // succesful
|
||||||
|
}
|
||||||
30
base64/main.h
Normal file
30
base64/main.h
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef __MAIN_H__
|
||||||
|
#define __MAIN_H__
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
|
|
||||||
|
/* To use this exported function of dll, include this header
|
||||||
|
* in your project.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifdef BUILD_DLL
|
||||||
|
#define DLL_EXPORT __declspec(dllexport)
|
||||||
|
#else
|
||||||
|
#define DLL_EXPORT __declspec(dllimport)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//#include <string>
|
||||||
|
void DLL_EXPORT SomeFunction(const LPCSTR sometext);
|
||||||
|
//std::string DLL_EXPORT Base64Encode(unsigned char const* buf, unsigned int bufLen);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // __MAIN_H__
|
||||||
@@ -48,6 +48,10 @@
|
|||||||
<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>
|
||||||
|
|||||||
63
tea.cs
63
tea.cs
@@ -12,62 +12,67 @@ namespace Tea
|
|||||||
{
|
{
|
||||||
public interface ITeaCoding
|
public interface ITeaCoding
|
||||||
{
|
{
|
||||||
void code(uint[] v, uint[] k);
|
void encode(uint[] v, uint[] k);
|
||||||
//void decode(uint[] v, uint[] k);
|
//void decode(uint[] v, uint[] k);
|
||||||
}
|
}
|
||||||
public partial class TeaCoding: ITeaCoding
|
public partial class TeaCoding: ITeaCoding
|
||||||
{
|
{
|
||||||
private readonly uint c_delta = 0x9E3779B9;
|
private readonly uint c_delta = 0x9E3779B9;
|
||||||
|
|
||||||
/*
|
|
||||||
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++) {
|
for (int i = 0; i < 32; i++) {
|
||||||
for(uint i = 32; i > 0; i--) {
|
//for (uint i = 32; i > 0; i--) {
|
||||||
//v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
|
unchecked {
|
||||||
//sum += c_delta;
|
v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
|
||||||
//v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
|
sum += c_delta;
|
||||||
|
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];
|
||||||
|
|
||||||
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];
|
||||||
}
|
}
|
||||||
v[0] = v0;
|
v[0] = v0;
|
||||||
v[1] = v1;
|
v[1] = v1;
|
||||||
}
|
}
|
||||||
*/
|
/*
|
||||||
public void code(uint[] v, uint[] k)
|
public void encode(uint[] v, uint[] k)
|
||||||
{
|
{
|
||||||
uint y = v[0];
|
uint y = v[0];
|
||||||
uint z = v[1];
|
uint z = v[1];
|
||||||
uint sum = 0;
|
uint sum = 0;
|
||||||
uint n = 32;
|
uint n = 32;
|
||||||
|
|
||||||
while (n-- > 0)
|
unchecked {
|
||||||
{
|
while (n-- > 0)
|
||||||
y += (z << 4 ^ z >> 5) + z ^ sum + k[sum & 3];
|
{
|
||||||
sum += c_delta;
|
y += (z << 4 ^ z >> 5) + z ^ sum + k[sum & 3];
|
||||||
z += (y << 4 ^ y >> 5) + y ^ sum + k[sum >> 11 & 3];
|
sum += c_delta;
|
||||||
}
|
z += (y << 4 ^ y >> 5) + y ^ sum + k[sum >> 11 & 3];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
v[0] = y;
|
v[0] = y;
|
||||||
v[1] = z;
|
v[1] = z;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
/*
|
/*
|
||||||
private readonly uint c_sum = 0xC6EF3720;
|
private readonly uint c_sum = 0xC6EF3720;
|
||||||
public void decode(uint[] v, uint[] k) {
|
public void decode(uint[] v, uint[] k) {
|
||||||
uint v0 = v[0], v1 = v[1];
|
uint v0 = v[0], v1 = v[1];
|
||||||
uint sum = c_sum;
|
uint sum = c_sum;
|
||||||
for (int i = 0; i < 32; i++) {
|
for (int i = 0; i < 32; i++) {
|
||||||
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
|
unchecked {
|
||||||
sum -= c_delta;
|
v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + k[(sum >> 11) & 3]);
|
||||||
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
|
sum -= c_delta;
|
||||||
|
v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + k[sum & 3]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
v[0] = v0;
|
v[0] = v0;
|
||||||
v[1] = v1;
|
v[1] = v1;
|
||||||
|
|||||||
Binary file not shown.
160
utils.cs
160
utils.cs
@@ -18,43 +18,165 @@ namespace Utils
|
|||||||
uint[] ConvertKey(string input);
|
uint[] ConvertKey(string input);
|
||||||
uint ConvertStringToUInt(string input);
|
uint ConvertStringToUInt(string input);
|
||||||
string ConvertUIntToString(uint input);
|
string ConvertUIntToString(uint input);
|
||||||
|
string Base64Encode(string plainText);
|
||||||
|
//string Base64Encode(byte[] plainText);
|
||||||
|
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)
|
public uint[] ConvertKey(string input)
|
||||||
{
|
{
|
||||||
// MD5 Hashing
|
// MD5 Hashing
|
||||||
var md5 = MD5.Create();
|
var md5 = MD5.Create();
|
||||||
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
|
byte[] inputBytes = Encoding.ASCII.GetBytes(input);
|
||||||
byte[] hashBytes = md5.ComputeHash(inputBytes);
|
byte[] hashBytes = md5.ComputeHash(inputBytes);
|
||||||
var sb = new StringBuilder();
|
var sb = new StringBuilder();
|
||||||
for (int i = 0; i < hashBytes.Length; i++)
|
for (int i = 0; i < hashBytes.Length; i++)
|
||||||
sb.Append(hashBytes[i].ToString("X2"));
|
sb.Append(hashBytes[i].ToString("X2"));
|
||||||
// Converting to uint[]
|
// Converting to uint[]
|
||||||
string key = sb.ToString().ToLower().Substring(0, 16);
|
string key = sb.ToString().ToLower().Substring(0, 16);
|
||||||
var formattedKey = new uint[4];
|
var formattedKey = new uint[4];
|
||||||
int j = 0;
|
int j = 0;
|
||||||
for (int i = 0; i < key.Length; i += 4)
|
for (int i = 0; i < key.Length; i += 4)
|
||||||
formattedKey[j++] = ConvertStringToUInt(key.Substring(i, 4));
|
formattedKey[j++] = ConvertStringToUInt(key.Substring(i, 4));
|
||||||
return formattedKey;
|
return formattedKey;
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
#region Convert String to UInt and reverse
|
||||||
public uint ConvertStringToUInt(string input)
|
public uint ConvertStringToUInt(string input)
|
||||||
{
|
{
|
||||||
uint output;
|
uint output;
|
||||||
output = ((uint)input[0]);
|
output = ((uint)input[0]);
|
||||||
output += ((uint)input[1] << 8);
|
output += ((uint)input[1] << 8);
|
||||||
output += ((uint)input[2] << 16);
|
output += ((uint)input[2] << 16);
|
||||||
output += ((uint)input[3] << 24);
|
output += ((uint)input[3] << 24);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
public string ConvertUIntToString(uint input)
|
public string ConvertUIntToString(uint input)
|
||||||
{
|
{
|
||||||
var output = new StringBuilder();
|
var output = new StringBuilder();
|
||||||
output.Append((char)((input & 0xFF)));
|
output.Append((char)((input & 0xFF)));
|
||||||
output.Append((char)((input >> 8) & 0xFF));
|
output.Append((char)((input >> 8) & 0xFF));
|
||||||
output.Append((char)((input >> 16) & 0xFF));
|
output.Append((char)((input >> 16) & 0xFF));
|
||||||
output.Append((char)((input >> 24) & 0xFF));
|
output.Append((char)((input >> 24) & 0xFF));
|
||||||
return output.ToString();
|
return output.ToString();
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
#region Base64 encoding/decoding
|
||||||
|
public string Base64Encode(string plainText)
|
||||||
|
{
|
||||||
|
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
|
||||||
|
return Convert.ToBase64String(plainTextBytes);
|
||||||
|
}
|
||||||
|
/*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);
|
||||||
|
return Encoding.UTF8.GetString(base64EncodedBytes);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user