Originally this class was intended for just encryptions / decryptions but I happened to expand it bit by bit of what I needed for OS and Encryptions. Figured it could help a few people out.
Credits to Chris because he originally started this. I just picked it up and added a lot of stuff.
Character Offset Encryption & Decryption ( if you want to encrypt "A" by two characters it'll return "C" -> Lots room to expand on for special characters and etc)
MD5 Encryption
Base64 Encryption and Decryption
Flip String (ABC becomes CBA)
isNumeric (Is this value really numeric or nah?)
A bunch of Hex stuff (str2Hex, Hex2Str, etc etc)
Sha1 Encryption
A ton of Windows OS information from registry (username, user profile, homedrive, etc etc)
Timestamp (Could upgrade / update add more functionality)
PHP Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Security.Cryptography;
using Microsoft.Win32;
static class Tools
{
#region Object To Hex
public static String Int2Hex(Int32 Input)
{
return Input.ToString("x").PadLeft(4, '0');
}
public static String Long2Hex(Int64 Input)
{
return Input.ToString("x").PadLeft(8, '0');
}
public static String String2Hex(String Input)
{
String Output = "";
for (Int32 i = 0; i < Input.Length; i++)
{
Output += String.Format("{0:x2}", System.Convert.ToUInt32(Input[i]));
}
return Output.PadLeft(Input.Length * 2, '0');
}
#endregion
#region Hex To Object
public static Int32 Hex2Int(String Input)
{
return Int32.Parse(Input, NumberStyles.HexNumber);
}
public static Int64 Hex2Long(String Input)
{
return Int64.Parse(Input, NumberStyles.HexNumber);
}
public static String Hex2String(String Input)
{
if (Input.Length % 2 == 0)
{
Input = Input.TrimStart('0');
byte[] Bytes = new byte[Input.Length / 2];
for (int i = 0; i < Bytes.Length; i++)
{
Bytes[i] = Convert.ToByte(Input.Substring(i * 2, 2), 16);
}
return Encoding.ASCII.GetString(Bytes);
}
return "";
}
#endregion
#region Offset Encryption
static public string Offset_Encryption(string Input, int Distance)
{
string Output = "";
for (int i = 0; i < Input.Length; i++)
{
int Letter = ((int)Input[i]);
if ((Letter + Distance) > 122)
{
Letter = ((Letter + Distance) - 123) + 97;
}
else
{
Letter += Distance;
}
Output += (char)Letter;
}
return Output;
}
static public string Offset_Decryption(string Input, int Distance)
{
string Output = "";
for (int i = 0; i < Input.Length; i++)
{
int Letter = ((int)Input[i]);
if ((Letter - Distance) < 97)
{
Letter = 122 - (96 - (Letter - Distance));
}
else
{
Letter -= Distance;
}
Output += (char)Letter;
}
return Output;
}
#endregion
#region Base64
public static String BaseEn(string data)
{
try
{
byte[] encData_byte = new byte[data.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
string encodedData = Convert.ToBase64String(encData_byte);
return encodedData;
}
catch (Exception e)
{
throw new Exception("Error in base64Encode" + e.Message);
}
}
public static String BaseDe(string data)
{
try
{
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
byte[] todecode_byte = Convert.FromBase64String(data);
int charCount = utf8Decode.GetCharCount(todecode_byte, 0, todecode_byte.Length);
char[] decoded_char = new char[charCount];
utf8Decode.GetChars(todecode_byte, 0, todecode_byte.Length, decoded_char, 0);
string result = new String(decoded_char);
return result;
}
catch (Exception e)
{
throw new Exception("Error in base64Decode" + e.Message);
}
}
#endregion
#region MD5
public static String md5(String Data_To_Encrypt)
{
return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString((new MD5CryptoServiceProvider()).ComputeHash(ASCIIEncoding.Default.GetBytes(Data_To_Encrypt))), "-", "").ToLower();
}
#endregion
#region Sha1
public static string sha1(string inputString)
{
HashAlgorithm algorithm = SHA1.Create(); // SHA1.Create()
StringBuilder sb = new StringBuilder();
foreach (byte b in algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString)))
sb.Append(b.ToString("X2"));
return sb.ToString().ToLower();
}
#endregion
#region Flip
public static string Flip(string Text)
{
char[] character = Text.ToCharArray();
Array.Reverse(character);
return new string(character);
}
#endregion
#region TimeStamp
public static String GetTimestamp(DateTime value)
{
return value.ToString("HHmmssffff");
}
#endregion
#region Random String
public static string Random_String(int Length)
{
Random random = new Random();
string characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890()*&^!@#$%_-=+{}[]|/?><.,~`'\"";
string rand = string.Empty;
for (int i = 0; i < Length; i++)
{
rand += characters[random.Next(0, characters.Count())];
}
return rand;
}
#endregion
#region Computer Information
private static RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Volatile Environment");
public static String AppData()
{
return registryKey.GetValue("APPDATA").ToString();
}
public static String HomeDrive()
{
return registryKey.GetValue("HOMEDRIVE").ToString();
}
public static String HomePath()
{
return registryKey.GetValue("HOMEPATH").ToString();
}
public static String LocalAppData()
{
return registryKey.GetValue("LOCALAPPDATA").ToString();
}
public static String Username()
{
return registryKey.GetValue("USERNAME").ToString();
}
public static String UserProfile()
{
return registryKey.GetValue("USERPROFILE").ToString();
}
#endregion
#region Other
public static Boolean isNumeric(String Value)
{
double num;
return double.TryParse(Value, out num);
}
#endregion
}
Results 1 to 1 of 1
Thread: [C#] "Tool" class.
- 15 Aug. 2014 10:42pm #1
[C#] "Tool" class.