Initial Commit
This commit is contained in:
86
TomcatServer/CrossProduct.Core/BinaryEncoder.cs
Normal file
86
TomcatServer/CrossProduct.Core/BinaryEncoder.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace CrossProduct.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// the binary encoder class
|
||||
/// </summary>
|
||||
public class BinaryEncoder
|
||||
{
|
||||
/// <summary>
|
||||
/// Binaries to string.
|
||||
/// </summary>
|
||||
/// <param name="BinaryConvert">The binary convert.</param>
|
||||
/// <returns></returns>
|
||||
public static string BinaryToString(byte[] BinaryConvert)
|
||||
{
|
||||
return BinaryToString(BinaryConvert, BinaryConvert.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Binaries to string.
|
||||
/// </summary>
|
||||
/// <param name="BinaryConvert">The binary convert.</param>
|
||||
/// <param name="lConvertLength">Length of the l convert.</param>
|
||||
/// <returns></returns>
|
||||
public static string BinaryToString(byte[] BinaryConvert, int lConvertLength)
|
||||
{
|
||||
if (BinaryConvert == null)
|
||||
return "";
|
||||
|
||||
int nIndex;
|
||||
StringBuilder buildString = new StringBuilder(lConvertLength * 2);
|
||||
|
||||
for (nIndex = 0; nIndex < lConvertLength; nIndex++)
|
||||
{
|
||||
buildString.AppendFormat("{0:X2}", BinaryConvert[nIndex]);
|
||||
}
|
||||
|
||||
return buildString.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strings to binary.
|
||||
/// </summary>
|
||||
/// <param name="BinaryConvert">The binary convert.</param>
|
||||
/// <param name="bAssumeString">if set to <c>true</c> [b assume string].</param>
|
||||
/// <returns></returns>
|
||||
public static byte[] StringToBinary(string BinaryConvert, bool bAssumeString)
|
||||
{
|
||||
if (BinaryConvert == null || BinaryConvert == "")
|
||||
return new byte[0];
|
||||
|
||||
int nLength = BinaryConvert.Length / 2;
|
||||
|
||||
if (bAssumeString)
|
||||
{
|
||||
if (BinaryConvert.Substring(BinaryConvert.Length - 2, 2) == "00")
|
||||
nLength--;
|
||||
}
|
||||
|
||||
int nIndex;
|
||||
int nPos;
|
||||
byte[] ReturnArray = new byte[nLength];
|
||||
|
||||
for (nPos = 0, nIndex = 0; nPos < nLength; nIndex += 2, nPos++)
|
||||
{
|
||||
ReturnArray[nPos] = byte.Parse(BinaryConvert.Substring(nIndex, 2), System.Globalization.NumberStyles.HexNumber);
|
||||
}
|
||||
|
||||
return ReturnArray;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strings to binary.
|
||||
/// </summary>
|
||||
/// <param name="BinaryConvert">The binary convert.</param>
|
||||
/// <returns></returns>
|
||||
public static byte[] StringToBinary(string BinaryConvert)
|
||||
{
|
||||
return StringToBinary(BinaryConvert, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
58
TomcatServer/CrossProduct.Core/CrossProduct.Core.csproj
Normal file
58
TomcatServer/CrossProduct.Core/CrossProduct.Core.csproj
Normal file
@@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>8.0.30703</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{D6CC42F0-0F08-457F-84D1-44D0D25715F6}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>CrossProduct.Core</RootNamespace>
|
||||
<AssemblyName>CrossProduct.Core</AssemblyName>
|
||||
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>..\Target\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>..\Target\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BinaryEncoder.cs" />
|
||||
<Compile Include="DataEncryption.cs" />
|
||||
<Compile Include="Encryption.cs" />
|
||||
<Compile Include="PerSeCryptography.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="SimpleStringBinaryConverter.cs" />
|
||||
<Compile Include="Utilities.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
||||
167
TomcatServer/CrossProduct.Core/DataEncryption.cs
Normal file
167
TomcatServer/CrossProduct.Core/DataEncryption.cs
Normal file
@@ -0,0 +1,167 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace CrossProduct.Core
|
||||
{
|
||||
public class DataEncryption
|
||||
{
|
||||
#region Encrypt
|
||||
#region Encrypt String
|
||||
public static string EncryptString(string EncryptString)
|
||||
{
|
||||
return DataEncryption.EncryptBinary(SimpleStringBinaryConverter.GetBytes(EncryptString));
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Encrypt Binary
|
||||
public static string EncryptBinary(byte[] EncryptBuffer)
|
||||
{
|
||||
int lStringLen = EncryptBuffer.Length;
|
||||
int lAdjust = 17;
|
||||
int nIndex;
|
||||
int lMid;
|
||||
int lTemp;
|
||||
byte cTemp;
|
||||
byte cXor;
|
||||
|
||||
lTemp = lStringLen;
|
||||
cXor = (byte)(0x59 ^ (byte)lStringLen);
|
||||
|
||||
lMid = lStringLen;
|
||||
lMid -= lMid % 2;
|
||||
lMid = lMid / 2;
|
||||
lTemp = lMid;
|
||||
|
||||
// "scramble" the original before encrypting
|
||||
// inverse the left half
|
||||
for (nIndex = lTemp - 1; nIndex >= 0 && nIndex >= lTemp - nIndex - 1; nIndex--)
|
||||
{
|
||||
cTemp = EncryptBuffer[nIndex];
|
||||
EncryptBuffer[nIndex] = EncryptBuffer[lTemp - nIndex - 1];
|
||||
EncryptBuffer[lTemp - nIndex - 1] = cTemp;
|
||||
}
|
||||
|
||||
// inverse the left half
|
||||
for (nIndex = lTemp - 1; nIndex >= 0 && nIndex >= lTemp - nIndex - 1; nIndex--)
|
||||
{
|
||||
cTemp = EncryptBuffer[lStringLen - lTemp + nIndex];
|
||||
EncryptBuffer[lStringLen - lTemp + nIndex] = EncryptBuffer[lStringLen - nIndex - 1];
|
||||
EncryptBuffer[lStringLen - nIndex - 1] = cTemp;
|
||||
}
|
||||
|
||||
// encrypt individual values from the center out
|
||||
// i.e. Center, Center + 1, Center - 1, Center + 2, Center - 2, etc...
|
||||
for (nIndex = 1; ; nIndex++)
|
||||
{
|
||||
if (nIndex % 2 != 0)
|
||||
{
|
||||
lTemp = lMid + (nIndex - 1) / 2;
|
||||
if (lTemp >= lStringLen)
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
lTemp = lMid - nIndex / 2;
|
||||
if (lTemp < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
cTemp = EncryptBuffer[lTemp];
|
||||
EncryptBuffer[lTemp] = (byte)(EncryptBuffer[lTemp] ^ cXor);
|
||||
if (EncryptBuffer[lTemp] == 0)
|
||||
EncryptBuffer[lTemp] = (byte)(EncryptBuffer[lTemp] ^ cXor);
|
||||
|
||||
lAdjust += 17 + cXor;
|
||||
lTemp = lAdjust + cXor + cTemp + EncryptBuffer[lTemp];
|
||||
cXor = (byte)((byte)lTemp % 223);
|
||||
}
|
||||
|
||||
return BinaryEncoder.BinaryToString(EncryptBuffer);
|
||||
}
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Decrypt
|
||||
#region Decrypt String
|
||||
public static string DecryptString(string EncryptString)
|
||||
{
|
||||
byte[] EncryptBuffer = DataEncryption.DecryptBinary(EncryptString);
|
||||
|
||||
return SimpleStringBinaryConverter.GetString(EncryptBuffer);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Decrypt Binary
|
||||
|
||||
public static byte[] DecryptBinary(string EncryptString)
|
||||
{
|
||||
byte[] EncryptBuffer = BinaryEncoder.StringToBinary(EncryptString);
|
||||
return DecryptBinary(EncryptBuffer);
|
||||
}
|
||||
|
||||
public static byte[] DecryptBinary(byte[] EncryptBuffer)
|
||||
{
|
||||
int lStringLen = EncryptBuffer.Length;
|
||||
int lAdjust = 17;
|
||||
int nIndex;
|
||||
int lMid;
|
||||
int lTemp;
|
||||
byte cTemp;
|
||||
byte cXor;
|
||||
|
||||
lTemp = lStringLen;
|
||||
cXor = (byte)(0x59 ^ (byte)lStringLen);
|
||||
|
||||
lMid = lStringLen;
|
||||
lMid -= lMid % 2;
|
||||
lMid = lMid / 2;
|
||||
|
||||
for (nIndex = 1; ; nIndex++)
|
||||
{
|
||||
if (nIndex % 2 != 0)
|
||||
{
|
||||
lTemp = lMid + (nIndex - 1) / 2;
|
||||
if (lTemp >= lStringLen)
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
lTemp = lMid - nIndex / 2;
|
||||
if (lTemp < 0)
|
||||
break;
|
||||
}
|
||||
|
||||
cTemp = EncryptBuffer[lTemp];
|
||||
EncryptBuffer[lTemp] = (byte)(EncryptBuffer[lTemp] ^ cXor);
|
||||
if (EncryptBuffer[lTemp] == 0)
|
||||
EncryptBuffer[lTemp] = (byte)(EncryptBuffer[lTemp] ^ cXor);
|
||||
|
||||
lAdjust += 17 + cXor;
|
||||
lTemp = lAdjust + cXor + cTemp + EncryptBuffer[lTemp];
|
||||
cXor = (byte)((byte)lTemp % 223);
|
||||
}
|
||||
|
||||
lTemp = lMid;
|
||||
for (nIndex = lTemp - 1; nIndex >= 0 && nIndex >= lTemp - nIndex - 1; nIndex--)
|
||||
{
|
||||
cTemp = EncryptBuffer[nIndex];
|
||||
EncryptBuffer[nIndex] = EncryptBuffer[lTemp - nIndex - 1];
|
||||
EncryptBuffer[lTemp - nIndex - 1] = cTemp;
|
||||
}
|
||||
|
||||
for (nIndex = lTemp - 1; nIndex >= 0 && nIndex >= lTemp - nIndex - 1; nIndex--)
|
||||
{
|
||||
cTemp = EncryptBuffer[lStringLen - lTemp + nIndex];
|
||||
EncryptBuffer[lStringLen - lTemp + nIndex] = EncryptBuffer[lStringLen - nIndex - 1];
|
||||
EncryptBuffer[lStringLen - nIndex - 1] = cTemp;
|
||||
}
|
||||
|
||||
return EncryptBuffer;
|
||||
}
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
60
TomcatServer/CrossProduct.Core/Encryption.cs
Normal file
60
TomcatServer/CrossProduct.Core/Encryption.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace CrossProduct.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Wrapper functions around our CrossProducts Encrypt Functionallity
|
||||
/// </summary>
|
||||
public static class Encryption
|
||||
{
|
||||
/// <summary>
|
||||
/// Encrypt Text
|
||||
/// </summary>
|
||||
/// <param name="Text">Text To Encrypt</param>
|
||||
/// <returns>Encrypted Text or String.Empty if Error Occured</returns>
|
||||
public static string EncryptText(string Text)
|
||||
{
|
||||
try
|
||||
{
|
||||
PerSeCryptography crypto = new PerSeCryptography();
|
||||
string EncryptedText = crypto.EncryptText(Text);
|
||||
return EncryptedText;
|
||||
}
|
||||
catch (Exception) { /* ignore */ }
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypt Text for use as a Command-Line Parameter - avoids "
|
||||
/// </summary>
|
||||
/// <param name="Text">Text To Encrypt</param>
|
||||
public static string EncryptTextParameter(string Text)
|
||||
{
|
||||
string EncryptedText;
|
||||
// Keep calling Encrypt Text until it productes a string without a double quote
|
||||
do { EncryptedText = EncryptText(Text); } while (EncryptedText.IndexOf('\"') != -1);
|
||||
return EncryptedText;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypt Text
|
||||
/// </summary>
|
||||
/// <param name="Text">Text To Decrypt</param>
|
||||
/// <returns>Decrypted Text or String.Empty if Error Occured</returns>
|
||||
public static string DecryptText(string Text)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
PerSeCryptography crypto = new PerSeCryptography();
|
||||
string EncryptedText = crypto.DecryptText(Text);
|
||||
return EncryptedText;
|
||||
}
|
||||
catch (Exception) { /* ignore */ }
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
875
TomcatServer/CrossProduct.Core/PerSeCryptography.cs
Normal file
875
TomcatServer/CrossProduct.Core/PerSeCryptography.cs
Normal file
@@ -0,0 +1,875 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Security.Cryptography;
|
||||
using System.IO;
|
||||
//using System.Windows.Forms;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace CrossProduct.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// Text Encryption Types that are Supported by the PerSeCryptography class.
|
||||
/// </summary>
|
||||
internal enum PerSeTextEncryptionTypes
|
||||
{
|
||||
Code,
|
||||
Filename,
|
||||
Text
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The PerSeCryptography class contains functionality to perform
|
||||
/// custom PerSe cryptography routines
|
||||
/// </summary>
|
||||
public class PerSeCryptography
|
||||
{
|
||||
#region Member Variables
|
||||
private const string KeyEncryptionString = "7:i#t~<Tm*I{cC1-+AygowsP}Zeb,WRp5^k?>QzV_BMF4u3!" +
|
||||
"XH;/)=(8dhEUnD.OJ6%0@G2&rNfqSv\\ja[LxYK$]l9";
|
||||
private const string ValidCodeCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
private const string ValidFilenameCharacters = "!#$%&'()-0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
|
||||
"^_`{}~ƒ¡¢£¥ª°±²µ·º»¼½¿ÄÅÆÇÉÑÖÜß÷";
|
||||
private const string ValidTextCharacters = "!#$%&()-0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +
|
||||
"^_`{}~ƒ¡¢£¥ª°±²µ·º»¼½¿ÄÅÆÇÉÑÖÜß÷ ;:\",.*/\\<>?|+=";
|
||||
|
||||
private const int KeyStringLength = 5;
|
||||
private const int MaxExtraEncryptionCharacters = 7;
|
||||
private const int SplitKeyIndex = 4;
|
||||
private const int InvalidStringCharacters = 50;
|
||||
private const int EncryptionKeyBitMask = 15;
|
||||
private const int EncryptionKeyShift = 4;
|
||||
private const int EncryptionKeyMaxUnchangedCount = 10;
|
||||
private const int EncryptionKeySkipKeys = 5;
|
||||
private const int EncryptionKeySeed = 32767;
|
||||
private const int RandomNumberBitMask = 65535;
|
||||
private const int RandomNumberMultiplier = 17405;
|
||||
private const int RandomNumberExtraValue = 196;
|
||||
private const float RandomNumberDivisor = 65536.0f;
|
||||
|
||||
private int _currentRandomNumber;
|
||||
private Random _randomNumGen;
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PerSeCryptography"/> class.
|
||||
/// </summary>
|
||||
/// Change Made On: 6/23/2009
|
||||
/// Change Made By: Shane.Calhoun
|
||||
/// ==============================================
|
||||
public PerSeCryptography()
|
||||
{
|
||||
_randomNumGen = new Random();
|
||||
_currentRandomNumber = _randomNumGen.Next() & RandomNumberBitMask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts a String with an Embedded Key
|
||||
/// </summary>
|
||||
/// <param name="encryptedText">encrypted text string to decrypt</param>
|
||||
/// <returns>the decrypted text string</returns>
|
||||
public string DecryptText(string encryptedText)
|
||||
{
|
||||
return Decrypt(encryptedText, PerSeTextEncryptionTypes.Text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts a Text string using a key
|
||||
/// </summary>
|
||||
/// <param name="encryptedText">text string to decrypt</param>
|
||||
/// <param name="key">key to use to decrypt the string</param>
|
||||
/// <returns>decrypted text</returns>
|
||||
public string DecryptText(string encryptedText, int key)
|
||||
{
|
||||
return Decrypt(encryptedText, key, PerSeTextEncryptionTypes.Text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts a String to Binary
|
||||
/// </summary>
|
||||
/// <param name="decryptedText">text to encrypt</param>
|
||||
/// <returns>encrypted text</returns>
|
||||
public string EncryptBinaryString(string decryptedText)
|
||||
{
|
||||
try
|
||||
{
|
||||
return DataEncryption.EncryptString(decryptedText);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error Encrypting binary String", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts a text string from binary to text
|
||||
/// </summary>
|
||||
/// <param name="encryptedText">The encrypted text.</param>
|
||||
/// <returns>decrypted text</returns>
|
||||
public string DecryptBinaryString(string encryptedText)
|
||||
{
|
||||
try
|
||||
{
|
||||
return DataEncryption.DecryptString(encryptedText);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error Decrypting Binary String", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts a Text string using a key and industry standard encryption modes - Rijndael will be default which is the same as AES 256 bit encryption.
|
||||
/// </summary>
|
||||
/// <param name="text">text string to encrypt</param>
|
||||
/// <param name="key">key to use to encrypt the string</param>
|
||||
/// <param name="encryptionMode">Mode of encryption of choice - "DES" or "RC2" or "Rijndael" or "TripleDES" - Default will be "Rijndael"</param>
|
||||
/// <returns>the text encrypted</returns>
|
||||
public string EncryptText(string text, string key, string encryptionMode)
|
||||
{
|
||||
byte[] encryptedData = Encrypt(text, key, encryptionMode);
|
||||
return Convert.ToBase64String(encryptedData);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts a Text string using a key and industry standard encryption modes - Rijndael will be default which is the same as AES 256 bit encryption.
|
||||
/// </summary>
|
||||
/// <param name="text">text string to decrypt</param>
|
||||
/// <param name="key">key to use to decrypt the string</param>
|
||||
/// <param name="encryptionMode">Mode of encryption of choice - "DES" or "RC2" or "Rijndael" or "TripleDES" - Default will be "Rijndael"</param>
|
||||
/// <returns>decrypted text</returns>
|
||||
public string DecryptText(string text, string key, string encryptionMode)
|
||||
{
|
||||
string decryptedData = string.Empty;
|
||||
decryptedData = Decrypt(Convert.FromBase64String(text), key, encryptionMode);
|
||||
return decryptedData;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts Key Text
|
||||
/// </summary>
|
||||
/// <param name="text">key text</param>
|
||||
/// <param name="key">key to use when encrypting the key</param>
|
||||
/// <returns></returns>
|
||||
public string EncryptText(string text, int key)
|
||||
{
|
||||
return Encrypt(text, key, PerSeTextEncryptionTypes.Text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts text
|
||||
/// </summary>
|
||||
/// <param name="text">Text to encrypt</param>
|
||||
/// <returns>the text encrypted</returns>
|
||||
public string EncryptText(string text)
|
||||
{
|
||||
string strEncryptValue;
|
||||
|
||||
strEncryptValue = Encrypt(text, PerSeTextEncryptionTypes.Text);
|
||||
|
||||
return strEncryptValue;
|
||||
|
||||
//return Encrypt( text, PerSeTextEncryptionTypes.Text );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts text
|
||||
/// </summary>
|
||||
/// <param name="text">Text to encrypt</param>
|
||||
/// <returns>the text encrypted</returns>
|
||||
public string EncryptText(string text, bool EnsureUniqueKey, string assemblyname)
|
||||
{
|
||||
string strEncryptValue;
|
||||
|
||||
strEncryptValue = Encrypt(text, PerSeTextEncryptionTypes.Text);
|
||||
|
||||
if (EnsureUniqueKey && !String.IsNullOrEmpty(assemblyname))
|
||||
strEncryptValue = SimpleStringBinaryConverter.GetString(strEncryptValue, assemblyname);
|
||||
|
||||
return strEncryptValue;
|
||||
|
||||
//return Encrypt( text, PerSeTextEncryptionTypes.Text );
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts the text.
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 7/15/2010
|
||||
/// Change Made By: Shane.Calhoun
|
||||
/// ==============================================
|
||||
public ReadOnlyCollection<String> EncryptText(ReadOnlyCollection<String> text)
|
||||
{
|
||||
List<string> result = new List<string>();
|
||||
foreach (string value in text)
|
||||
{
|
||||
result.Add(Encrypt(value, PerSeTextEncryptionTypes.Text));
|
||||
}
|
||||
ReadOnlyCollection<string> temp = new ReadOnlyCollection<string>(result);
|
||||
return temp;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts a Text string using an Embedded key
|
||||
/// </summary>
|
||||
/// <param name="encryptedText">encrypted text to decrypt</param>
|
||||
/// <param name="encryptionType"></param>
|
||||
/// <returns>decrypted text string</returns>
|
||||
private string Decrypt(string encryptedText, PerSeTextEncryptionTypes encryptionType)
|
||||
{
|
||||
try
|
||||
{
|
||||
StringBuilder decryptedText = new StringBuilder();
|
||||
int key = GetEmbeddedKey(encryptedText);
|
||||
int index = 0;
|
||||
|
||||
if (key != -1)
|
||||
{
|
||||
index = encryptedText.Length - (KeyStringLength - SplitKeyIndex) + 1;
|
||||
if (index >= (SplitKeyIndex + 1))
|
||||
{
|
||||
string encryptedTextWithoutKey = encryptedText.Substring(SplitKeyIndex, (index - SplitKeyIndex - 1));
|
||||
decryptedText.Append(Decrypt(encryptedTextWithoutKey, key, encryptionType));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (index = 0; index < InvalidStringCharacters; index++)
|
||||
{
|
||||
int charIndex = RandomNumber(ValidTextCharacters.Length);
|
||||
decryptedText.Append(ValidTextCharacters[charIndex]);
|
||||
}
|
||||
}
|
||||
|
||||
return decryptedText.ToString();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error Decrypting string", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts a text string
|
||||
/// </summary>
|
||||
/// <param name="encryptedText">Encrypted Text to decrypt</param>
|
||||
/// <param name="key">key to use when decrypting the text</param>
|
||||
/// <param name="encryptionType">the type of text to decrypt</param>
|
||||
/// <returns>the encrypted string decrypted</returns>
|
||||
private string Decrypt(string encryptedText, int key, PerSeTextEncryptionTypes encryptionType)
|
||||
{
|
||||
try
|
||||
{
|
||||
string validCharacters;
|
||||
string encryptionCodeCharacters;
|
||||
int extraCharacerOdds;
|
||||
int extraCharacters;
|
||||
int realCharacters;
|
||||
int originalKey;
|
||||
|
||||
encryptionCodeCharacters = GenerateEncryptionCode(key, encryptionType);
|
||||
|
||||
originalKey = _currentRandomNumber;
|
||||
_currentRandomNumber = key;
|
||||
|
||||
switch (encryptionType)
|
||||
{
|
||||
case PerSeTextEncryptionTypes.Code:
|
||||
{
|
||||
validCharacters = ValidCodeCharacters;
|
||||
extraCharacters = 0;
|
||||
realCharacters = encryptedText.Length;
|
||||
extraCharacerOdds = 0;
|
||||
break;
|
||||
}
|
||||
case PerSeTextEncryptionTypes.Filename:
|
||||
{
|
||||
validCharacters = ValidFilenameCharacters;
|
||||
extraCharacters = 0;
|
||||
realCharacters = encryptedText.Length;
|
||||
extraCharacerOdds = 0;
|
||||
break;
|
||||
}
|
||||
default: // Text
|
||||
{
|
||||
validCharacters = ValidTextCharacters;
|
||||
extraCharacters = RandomNumber(MaxExtraEncryptionCharacters + 1);
|
||||
realCharacters = encryptedText.Length - extraCharacters;
|
||||
if (realCharacters > 0)
|
||||
extraCharacerOdds = (int)Decimal.Truncate((decimal)((50 * extraCharacters) / realCharacters));
|
||||
else
|
||||
extraCharacerOdds = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int index = 0;
|
||||
int characterPositionIndex;
|
||||
StringBuilder decryptedText = new StringBuilder();
|
||||
bool isFilename = (encryptionType == PerSeTextEncryptionTypes.Filename);
|
||||
|
||||
while (realCharacters > 0)
|
||||
{
|
||||
if (extraCharacters > 0)
|
||||
{
|
||||
if (extraCharacerOdds >= (RandomNumber(100) + 1))
|
||||
{
|
||||
RandomNumber(0); // Need (unused) call here to keep in synch with encrypt side
|
||||
index++;
|
||||
--extraCharacters;
|
||||
}
|
||||
}
|
||||
|
||||
characterPositionIndex = encryptionCodeCharacters.IndexOf(encryptedText[index]);
|
||||
if (characterPositionIndex < 0)
|
||||
{
|
||||
decryptedText.Append(encryptedText[index]);
|
||||
}
|
||||
else
|
||||
{
|
||||
characterPositionIndex = characterPositionIndex - RandomNumber(validCharacters.Length) - 1;
|
||||
while (characterPositionIndex < 0)
|
||||
{
|
||||
characterPositionIndex += validCharacters.Length;
|
||||
}
|
||||
decryptedText.Append(validCharacters[characterPositionIndex]);
|
||||
}
|
||||
|
||||
// Since all subdirectories of a directory path need to be encrypted/decrypted
|
||||
// identically, we reset the seed value whenever we start a new subdirectory.
|
||||
if (isFilename && (encryptedText[index] == '\\'))
|
||||
{
|
||||
_currentRandomNumber = key;
|
||||
}
|
||||
|
||||
index++;
|
||||
realCharacters--;
|
||||
}
|
||||
_currentRandomNumber = originalKey;
|
||||
|
||||
return decryptedText.ToString();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error decrypting string", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts an encrypted key
|
||||
/// </summary>
|
||||
/// <param name="encryptedKey">The encrypted key.</param>
|
||||
/// <returns>the key in it's original integer form</returns>
|
||||
private int DecryptKey(string encryptedKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
int count;
|
||||
int index;
|
||||
int maxIndex;
|
||||
int tempKey;
|
||||
int decryptedKey;
|
||||
int value;
|
||||
|
||||
maxIndex = KeyEncryptionString.Length;
|
||||
tempKey = 0;
|
||||
|
||||
for (count = 1; count < encryptedKey.Length; count++)
|
||||
{
|
||||
index = KeyEncryptionString.IndexOf(encryptedKey[count]);
|
||||
value = index - KeyEncryptionString.IndexOf(encryptedKey[count - 1]);
|
||||
|
||||
if (value < 0)
|
||||
{
|
||||
value += maxIndex;
|
||||
}
|
||||
|
||||
if (value == 16)
|
||||
{
|
||||
value = 0;
|
||||
}
|
||||
|
||||
tempKey = (tempKey << EncryptionKeyShift) + (value & EncryptionKeyBitMask);
|
||||
}
|
||||
|
||||
decryptedKey = 0;
|
||||
for (count = 0; count < (16 / 4); count++)
|
||||
{
|
||||
decryptedKey = (decryptedKey << EncryptionKeyShift) | (tempKey & EncryptionKeyBitMask);
|
||||
tempKey = tempKey >> EncryptionKeyShift;
|
||||
}
|
||||
|
||||
return decryptedKey;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error decrypting key", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves the embedded key out of Encrypted Text
|
||||
/// </summary>
|
||||
/// <param name="encryptedText">the encrypted text to retrieve the embedded key from</param>
|
||||
/// <returns>embedded encrypted key</returns>
|
||||
private int GetEmbeddedKey(string encryptedText)
|
||||
{
|
||||
int index;
|
||||
int decryptedKey;
|
||||
string encryptedKey;
|
||||
|
||||
decryptedKey = -1;
|
||||
index = encryptedText.Length - (KeyStringLength - SplitKeyIndex) + 1;
|
||||
|
||||
if (index >= (SplitKeyIndex + 1))
|
||||
{
|
||||
encryptedKey = encryptedText.Substring(0, SplitKeyIndex) +
|
||||
encryptedText.Substring(index - 1);
|
||||
decryptedKey = DecryptKey(encryptedKey);
|
||||
}
|
||||
|
||||
return decryptedKey;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a valid Encryption Key
|
||||
/// </summary>
|
||||
/// <returns>encryption key</returns>
|
||||
private int GenerateEncryptionKey()
|
||||
{
|
||||
string charString;
|
||||
string codeString;
|
||||
int key;
|
||||
int identicalCharacterCount;
|
||||
bool isKeyValid = false;
|
||||
|
||||
do
|
||||
{
|
||||
for (int i = 0; i < EncryptionKeySkipKeys; i++)
|
||||
{
|
||||
RandomNumber(EncryptionKeySeed);
|
||||
}
|
||||
key = _currentRandomNumber;
|
||||
|
||||
codeString = GenerateEncryptionCode(key);
|
||||
charString = ValidTextCharacters;
|
||||
|
||||
identicalCharacterCount = 0;
|
||||
for (int i = 0; i < codeString.Length; i++)
|
||||
{
|
||||
if (codeString[i] == charString[i])
|
||||
{
|
||||
identicalCharacterCount++;
|
||||
}
|
||||
}
|
||||
isKeyValid = ((identicalCharacterCount < EncryptionKeyMaxUnchangedCount) && (key != -1));
|
||||
}
|
||||
while (!isKeyValid);
|
||||
|
||||
return key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts text for the specified encryption type
|
||||
/// </summary>
|
||||
/// <param name="decryptedText">text to encrypt</param>
|
||||
/// <param name="encryptionType">encryption type to perform</param>
|
||||
/// <returns>the text encrypted</returns>
|
||||
private string Encrypt(string decryptedText, PerSeTextEncryptionTypes encryptionType)
|
||||
{
|
||||
int decryptedKey;
|
||||
string encryptedKey = string.Empty;
|
||||
|
||||
decryptedKey = GenerateEncryptionKey();
|
||||
encryptedKey = EncryptKey(decryptedKey);
|
||||
|
||||
string encryptedText = Encrypt(decryptedText, decryptedKey, encryptionType);
|
||||
encryptedText = encryptedKey.Substring(0, SplitKeyIndex) + encryptedText + encryptedKey.Substring(SplitKeyIndex, encryptedKey.Length - SplitKeyIndex);
|
||||
|
||||
return encryptedText;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts text for the specified encryption type
|
||||
/// </summary>
|
||||
/// <param name="decryptedText">text to encrypt</param>
|
||||
/// <param name="key">key to use for encryption</param>
|
||||
/// <param name="encryptionType">type of encryption to perform</param>
|
||||
/// <returns>decrypted text encrypted </returns>
|
||||
private string Encrypt(string decryptedText, int key, PerSeTextEncryptionTypes encryptionType)
|
||||
{
|
||||
try
|
||||
{
|
||||
string charString = string.Empty;
|
||||
string codeString = string.Empty;
|
||||
int extraCharacterOdds;
|
||||
int extraCharacters;
|
||||
int posIndex;
|
||||
int originalKey;
|
||||
StringBuilder encryptedText = new StringBuilder();
|
||||
|
||||
codeString = GenerateEncryptionCode(key);
|
||||
originalKey = _currentRandomNumber;
|
||||
|
||||
_currentRandomNumber = key;
|
||||
|
||||
charString = ValidTextCharacters;
|
||||
extraCharacters = RandomNumber(MaxExtraEncryptionCharacters + 1);
|
||||
|
||||
if (decryptedText.Length > 0)
|
||||
{
|
||||
extraCharacterOdds = (50 * extraCharacters) / decryptedText.Length;
|
||||
}
|
||||
else
|
||||
{
|
||||
extraCharacterOdds = 0;
|
||||
}
|
||||
|
||||
if (decryptedText.Length > 0)
|
||||
{
|
||||
for (int i = 0; i < decryptedText.Length; i++)
|
||||
{
|
||||
if (extraCharacters > 0)
|
||||
{
|
||||
if (extraCharacterOdds >= (RandomNumber(100) + 1))
|
||||
{
|
||||
posIndex = RandomNumber(codeString.Length) + 1;
|
||||
if (posIndex >= codeString.Length)
|
||||
{
|
||||
posIndex = codeString.Length - 1;
|
||||
}
|
||||
encryptedText.Append(codeString[posIndex]);
|
||||
extraCharacters--;
|
||||
}
|
||||
}
|
||||
posIndex = charString.IndexOf(decryptedText[i]);
|
||||
if (posIndex < 0)
|
||||
{
|
||||
encryptedText.Append(decryptedText[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
posIndex = ((posIndex + RandomNumber(codeString.Length) + 1) % codeString.Length);
|
||||
encryptedText.Append(codeString[posIndex]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
while (extraCharacters > 0)
|
||||
{
|
||||
posIndex = RandomNumber(codeString.Length) + 1;
|
||||
if (posIndex >= codeString.Length)
|
||||
{
|
||||
posIndex = codeString.Length - 1;
|
||||
}
|
||||
encryptedText.Append(codeString[posIndex]);
|
||||
extraCharacters--;
|
||||
}
|
||||
_currentRandomNumber = originalKey;
|
||||
|
||||
return encryptedText.ToString();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error Encrypting key", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts a Key
|
||||
/// </summary>
|
||||
/// <param name="decryptedKey">key to encrypt</param>
|
||||
/// <returns>encrypted key</returns>
|
||||
private string EncryptKey(int decryptedKey)
|
||||
{
|
||||
int count;
|
||||
int index;
|
||||
int maxIndex;
|
||||
StringBuilder encryptedKey = new StringBuilder();
|
||||
int value;
|
||||
|
||||
maxIndex = KeyEncryptionString.Length;
|
||||
index = RandomNumber(maxIndex);
|
||||
encryptedKey.Append(KeyEncryptionString[index]);
|
||||
|
||||
for (count = 0; count < (16 / 4); count++)
|
||||
{
|
||||
value = decryptedKey & EncryptionKeyBitMask;
|
||||
if (value == 0)
|
||||
{
|
||||
value = 16;
|
||||
}
|
||||
decryptedKey >>= EncryptionKeyShift;
|
||||
index = ((index + value) % maxIndex);
|
||||
encryptedKey.Append(KeyEncryptionString[index]);
|
||||
}
|
||||
return encryptedKey.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates a Random number based on the Value, seeds and other custom inhouse
|
||||
/// values
|
||||
/// </summary>
|
||||
/// <param name="seed">random number seed</param>
|
||||
/// <returns></returns>
|
||||
private int RandomNumber(int seed)
|
||||
{
|
||||
float newValue;
|
||||
int newRandomNumber;
|
||||
|
||||
_currentRandomNumber = ((_currentRandomNumber * RandomNumberMultiplier) + RandomNumberExtraValue) & RandomNumberBitMask;
|
||||
newValue = _currentRandomNumber / RandomNumberDivisor;
|
||||
newRandomNumber = (int)Decimal.Truncate((decimal)(seed * newValue));
|
||||
|
||||
return newRandomNumber;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates an Encryption Code String based on the Seed
|
||||
/// </summary>
|
||||
/// <param name="seed">seed to build the encryption code string for</param>
|
||||
/// <returns>encryption code string</returns>
|
||||
private string GenerateEncryptionCode(int seed)
|
||||
{
|
||||
return GenerateEncryptionCode(seed, PerSeTextEncryptionTypes.Text);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates an Encryption Code String based on the Seed and Encryption Type
|
||||
/// </summary>
|
||||
/// <param name="seed">seed to build the encryption code string for</param>
|
||||
/// <param name="encryptionType">Encryption type to build the encryption code string for</param>
|
||||
/// <returns>encryption code string</returns>
|
||||
private string GenerateEncryptionCode(int seed, PerSeTextEncryptionTypes encryptionType)
|
||||
{
|
||||
try
|
||||
{
|
||||
StringBuilder encryptionCodeString = new StringBuilder();
|
||||
StringBuilder validEncryptionCharacters = new StringBuilder();
|
||||
|
||||
switch (encryptionType)
|
||||
{
|
||||
case PerSeTextEncryptionTypes.Code:
|
||||
validEncryptionCharacters.Append(ValidCodeCharacters);
|
||||
break;
|
||||
|
||||
case PerSeTextEncryptionTypes.Filename:
|
||||
validEncryptionCharacters.Append(ValidFilenameCharacters);
|
||||
break;
|
||||
|
||||
default: // Text
|
||||
validEncryptionCharacters.Append(ValidTextCharacters);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
int index;
|
||||
int originalKey = _currentRandomNumber;
|
||||
|
||||
_currentRandomNumber = seed;
|
||||
while (validEncryptionCharacters.Length > 0)
|
||||
{
|
||||
index = RandomNumber(validEncryptionCharacters.Length);
|
||||
encryptionCodeString.Append(validEncryptionCharacters[index]);
|
||||
validEncryptionCharacters.Remove(index, 1);
|
||||
}
|
||||
_currentRandomNumber = originalKey;
|
||||
|
||||
return encryptionCodeString.ToString();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new Exception("Error Generating Encryption code", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Encrypts the specified text.
|
||||
/// </summary>
|
||||
/// <param name="text">The text.</param>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="encryptionMode">The encryption mode.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 5/6/2009
|
||||
/// Change Made By: Shane.Calhoun
|
||||
/// ==============================================
|
||||
private byte[] Encrypt(string text, string key, string encryptionMode)
|
||||
{
|
||||
SymmetricAlgorithm cryptoObj = null;
|
||||
cryptoObj = GetEncryptionProviderObject(encryptionMode);
|
||||
|
||||
byte[] bytIn = Encoding.ASCII.GetBytes(text);
|
||||
// create a MemoryStream so that the process can be done without I/O files
|
||||
MemoryStream ms = new MemoryStream();
|
||||
|
||||
// set the private key
|
||||
cryptoObj.Key = GetLegalKey(key, encryptionMode);
|
||||
cryptoObj.IV = GetLegalVectorSize(key, encryptionMode);
|
||||
|
||||
// create an Encryptor from the Provider Service instance
|
||||
ICryptoTransform encrypto = cryptoObj.CreateEncryptor();
|
||||
|
||||
// create Crypto Stream that transforms a stream using the encryption
|
||||
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
|
||||
|
||||
// write out encrypted content into MemoryStream
|
||||
cs.Write(bytIn, 0, bytIn.Length);
|
||||
cs.FlushFinalBlock();
|
||||
cs.Close();
|
||||
|
||||
return ms.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decrypts the specified ciphertext.
|
||||
/// </summary>
|
||||
/// <param name="ciphertext">The ciphertext.</param>
|
||||
/// <param name="key">The key.</param>
|
||||
/// <param name="encryptionMode">The encryption mode.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 5/6/2009
|
||||
/// Change Made By: Shane.Calhoun
|
||||
/// ==============================================
|
||||
private string Decrypt(byte[] ciphertext, string key, string encryptionMode)
|
||||
{
|
||||
SymmetricAlgorithm deCryptoObj = null;
|
||||
deCryptoObj = GetEncryptionProviderObject(encryptionMode);
|
||||
|
||||
// create a MemoryStream with the input
|
||||
MemoryStream ms = new MemoryStream(ciphertext, 0, ciphertext.Length);
|
||||
|
||||
// set the private key
|
||||
deCryptoObj.Key = GetLegalKey(key, encryptionMode);
|
||||
deCryptoObj.IV = GetLegalVectorSize(key, encryptionMode);
|
||||
|
||||
// create a Decryptor from the Provider Service instance
|
||||
ICryptoTransform encrypto = deCryptoObj.CreateDecryptor();
|
||||
|
||||
// create Crypto Stream that transforms a stream using the decryption
|
||||
CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
|
||||
|
||||
// read out the result from the Crypto Stream
|
||||
StreamReader sr = new StreamReader(cs);
|
||||
return sr.ReadToEnd();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the encryption provider object.
|
||||
/// </summary>
|
||||
/// <param name="encryptionMode">The encryption mode.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 5/6/2009
|
||||
/// Change Made By: Shane.Calhoun
|
||||
/// ==============================================
|
||||
private SymmetricAlgorithm GetEncryptionProviderObject(string encryptionMode)
|
||||
{
|
||||
SymmetricAlgorithm tmpCryptoObj = null;
|
||||
|
||||
switch (encryptionMode)
|
||||
{
|
||||
case "DES":
|
||||
tmpCryptoObj = new DESCryptoServiceProvider();
|
||||
break;
|
||||
case "RC2":
|
||||
tmpCryptoObj = new RC2CryptoServiceProvider();
|
||||
break;
|
||||
case "Rijndael":
|
||||
tmpCryptoObj = new RijndaelManaged();
|
||||
break;
|
||||
case "TripleDES":
|
||||
tmpCryptoObj = new TripleDESCryptoServiceProvider();
|
||||
break;
|
||||
default:
|
||||
tmpCryptoObj = new RijndaelManaged();
|
||||
break;
|
||||
}
|
||||
|
||||
return tmpCryptoObj;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the legal key.
|
||||
/// </summary>
|
||||
/// <param name="Key">The key.</param>
|
||||
/// <param name="encryptionMode">The encryption mode.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 5/6/2009
|
||||
/// Change Made By: Shane.Calhoun
|
||||
/// ==============================================
|
||||
private byte[] GetLegalKey(string Key, string encryptionMode)
|
||||
{
|
||||
string sTemp = Key;
|
||||
|
||||
switch (encryptionMode)
|
||||
{
|
||||
case "DES":
|
||||
case "RC2":
|
||||
if (Key.Length > 8)
|
||||
{
|
||||
sTemp = Key.Substring(0, 8);
|
||||
}
|
||||
break;
|
||||
case "TripleDES":
|
||||
if (Key.Length > 16)
|
||||
{
|
||||
sTemp = Key.Substring(0, 16);
|
||||
}
|
||||
break;
|
||||
case "Rijndael":
|
||||
default:
|
||||
if (Key.Length > 32)
|
||||
{
|
||||
sTemp = Key.Substring(0, 32);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// convert the secret key to byte array
|
||||
return ASCIIEncoding.ASCII.GetBytes(sTemp);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the size of the legal vector.
|
||||
/// </summary>
|
||||
/// <param name="Key">The key.</param>
|
||||
/// <param name="encryptionMode">The encryption mode.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 5/6/2009
|
||||
/// Change Made By: Shane.Calhoun
|
||||
/// ==============================================
|
||||
private byte[] GetLegalVectorSize(string Key, string encryptionMode)
|
||||
{
|
||||
string sTemp = Key;
|
||||
|
||||
switch (encryptionMode)
|
||||
{
|
||||
case "DES":
|
||||
case "RC2":
|
||||
if (Key.Length > 8)
|
||||
{
|
||||
sTemp = Key.Substring(0, 8);
|
||||
}
|
||||
break;
|
||||
case "TripleDES":
|
||||
if (Key.Length > 8)
|
||||
{
|
||||
sTemp = Key.Substring(0, 8);
|
||||
}
|
||||
break;
|
||||
case "Rijndael":
|
||||
default:
|
||||
if (Key.Length > 16)
|
||||
{
|
||||
sTemp = Key.Substring(0, 16);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
// convert the secret key to byte array
|
||||
return ASCIIEncoding.ASCII.GetBytes(sTemp);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
TomcatServer/CrossProduct.Core/Properties/AssemblyInfo.cs
Normal file
36
TomcatServer/CrossProduct.Core/Properties/AssemblyInfo.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("CrossProduct.Core")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("CrossProduct.Core")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2012")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("85f57c03-38ae-43ee-8fe8-c4e60457ae27")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
220
TomcatServer/CrossProduct.Core/SimpleStringBinaryConverter.cs
Normal file
220
TomcatServer/CrossProduct.Core/SimpleStringBinaryConverter.cs
Normal file
@@ -0,0 +1,220 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Reflection;
|
||||
|
||||
namespace CrossProduct.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// the simple string binary converter class
|
||||
/// * This class will only work with strings which use standard ASCII values (0-255)
|
||||
/// * Another version of this funciton will be needed for UNICODE.
|
||||
/// </summary>
|
||||
/// Change Made On: 12/4/2008
|
||||
/// Change Made By: shane.calhoun
|
||||
/// ==============================================
|
||||
public class SimpleStringBinaryConverter
|
||||
{
|
||||
private static StringBuilder ByteStringBuilder = new StringBuilder();
|
||||
|
||||
/*
|
||||
* NOTE:
|
||||
* This class will only work with strings which use standard ASCII values (0-255)
|
||||
* Another version of this funciton will be needed for UNICODE.
|
||||
*/
|
||||
|
||||
/// <summary>
|
||||
/// Gets the bytes.
|
||||
/// </summary>
|
||||
/// <param name="SourceString">The source string.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 12/4/2008
|
||||
/// Change Made By: shane.calhoun
|
||||
/// ==============================================
|
||||
public static byte[] GetBytes(string SourceString)
|
||||
{
|
||||
byte[] ReturnBytes = new byte[SourceString.Length];
|
||||
int nIndex;
|
||||
int nLength = SourceString.Length;
|
||||
|
||||
for (nIndex = 0; nIndex < nLength; nIndex++)
|
||||
ReturnBytes[nIndex] = (byte)SourceString[nIndex];
|
||||
|
||||
return ReturnBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the string.
|
||||
/// </summary>
|
||||
/// <param name="SourceBytes">The source bytes.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 12/4/2008
|
||||
/// Change Made By: shane.calhoun
|
||||
/// ==============================================
|
||||
public static string GetString(byte[] SourceBytes)
|
||||
{
|
||||
int nIndex;
|
||||
int nLength = SourceBytes.GetLength(0);
|
||||
|
||||
ByteStringBuilder.Length = 0;
|
||||
for (nIndex = 0; nIndex < nLength; nIndex++)
|
||||
{
|
||||
ByteStringBuilder.Append((char)(SourceBytes[nIndex]));
|
||||
}
|
||||
|
||||
return ByteStringBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the char bytes.
|
||||
/// </summary>
|
||||
/// <param name="SourceString">The source string.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 12/4/2008
|
||||
/// Change Made By: shane.calhoun
|
||||
/// ==============================================
|
||||
public static byte[] GetCharBytes(string SourceString)
|
||||
{
|
||||
byte[] ReturnBytes = new byte[SourceString.Length * 2];
|
||||
int nIndex;
|
||||
int nLength = SourceString.Length;
|
||||
char TempChar;
|
||||
|
||||
for (nIndex = 0; nIndex < nLength; nIndex++)
|
||||
{
|
||||
TempChar = SourceString[nIndex];
|
||||
ReturnBytes[2 * nIndex + 1] = (byte)(TempChar % 256);
|
||||
ReturnBytes[2 * nIndex] = (byte)((TempChar - ReturnBytes[2 * nIndex + 1]) / 256);
|
||||
}
|
||||
|
||||
return ReturnBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the char string.
|
||||
/// </summary>
|
||||
/// <param name="SourceBytes">The source bytes.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 12/4/2008
|
||||
/// Change Made By: shane.calhoun
|
||||
/// ==============================================
|
||||
public static string GetCharString(byte[] SourceBytes)
|
||||
{
|
||||
int nIndex;
|
||||
int nLength = SourceBytes.GetLength(0) / 2;
|
||||
|
||||
ByteStringBuilder.Length = 0;
|
||||
for (nIndex = 0; nIndex < nLength; nIndex++)
|
||||
{
|
||||
ByteStringBuilder.Append((char)(SourceBytes[2 * nIndex] * 256 + SourceBytes[2 * nIndex + 1]));
|
||||
}
|
||||
|
||||
return ByteStringBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the chars.
|
||||
/// </summary>
|
||||
/// <param name="SourceString">The source string.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 12/4/2008
|
||||
/// Change Made By: shane.calhoun
|
||||
/// ==============================================
|
||||
public static char[] GetChars(string SourceString)
|
||||
{
|
||||
char[] ReturnBytes = new char[SourceString.Length];
|
||||
int nIndex;
|
||||
int nLength = SourceString.Length;
|
||||
|
||||
for (nIndex = 0; nIndex < nLength; nIndex++)
|
||||
ReturnBytes[nIndex] = (char)SourceString[nIndex];
|
||||
|
||||
return ReturnBytes;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the string.
|
||||
/// </summary>
|
||||
/// <param name="SourceBytes">The source bytes.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 12/4/2008
|
||||
/// Change Made By: shane.calhoun
|
||||
/// ==============================================
|
||||
public static string GetString(string SourceString, string AdditionalInfo)
|
||||
{
|
||||
int nIndex;
|
||||
int nLength = SourceString.Length;
|
||||
const string COMMON_GET_STRING = "Common";
|
||||
|
||||
ByteStringBuilder.Length = 0;
|
||||
for (nIndex = 0; nIndex < nLength; nIndex++)
|
||||
{
|
||||
ByteStringBuilder.Append((char)(SourceString[nIndex]));
|
||||
}
|
||||
|
||||
//return ByteStringBuilder.ToString();
|
||||
if (!String.IsNullOrEmpty(AdditionalInfo))
|
||||
{
|
||||
try
|
||||
{
|
||||
Assembly asm = null;
|
||||
try { asm = Assembly.Load(AdditionalInfo.Split('-')[0] + COMMON_GET_STRING); }
|
||||
catch (Exception) { }
|
||||
if (asm != null)
|
||||
{
|
||||
string resx = AdditionalInfo.Split('-')[2].ToLower();
|
||||
string comm = AdditionalInfo.Split('-')[1] + COMMON_GET_STRING;
|
||||
resx = "." + resx[1] + resx[0];
|
||||
var stream = asm.GetManifestResourceStream(comm + "." + comm + resx);
|
||||
if (stream != null)
|
||||
{
|
||||
using (stream)
|
||||
{
|
||||
Byte[] assemblyData = new Byte[stream.Length];
|
||||
stream.Read(assemblyData, 0, assemblyData.Length);
|
||||
Assembly asm2 = null;
|
||||
try { asm2 = Assembly.Load(assemblyData); }
|
||||
catch (Exception) { }
|
||||
if (asm2 != null)
|
||||
{
|
||||
Type common = asm2.GetType(AdditionalInfo.Split('-')[0] + "Lib." + COMMON_GET_STRING);
|
||||
if (common != null)
|
||||
{
|
||||
object o = asm2.CreateInstance(common.ToString());
|
||||
MethodInfo m = common.GetMethod("SourceWithInfo");
|
||||
SourceString = (string)m.Invoke(o, new object[] { SourceString, AdditionalInfo });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception) { }
|
||||
}
|
||||
return SourceString;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the string.
|
||||
/// </summary>
|
||||
/// <param name="SourceBytes">The source bytes.</param>
|
||||
/// <returns></returns>
|
||||
/// Change Made On: 12/4/2008
|
||||
/// Change Made By: shane.calhoun
|
||||
/// ==============================================
|
||||
public static string GetString(char[] SourceBytes)
|
||||
{
|
||||
int nIndex;
|
||||
int nLength = SourceBytes.GetLength(0);
|
||||
|
||||
ByteStringBuilder.Length = 0;
|
||||
for (nIndex = 0; nIndex < nLength; nIndex++)
|
||||
{
|
||||
ByteStringBuilder.Append((char)(SourceBytes[nIndex]));
|
||||
}
|
||||
|
||||
return ByteStringBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
543
TomcatServer/CrossProduct.Core/Utilities.cs
Normal file
543
TomcatServer/CrossProduct.Core/Utilities.cs
Normal file
@@ -0,0 +1,543 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Data;
|
||||
using System.Collections;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Security.Cryptography;
|
||||
using System.Collections.Specialized;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.Win32;
|
||||
|
||||
namespace CrossProduct.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// generic utility functions available to all crossproducts
|
||||
/// </summary>
|
||||
public class Utils
|
||||
{
|
||||
|
||||
static private byte[] Key = { 0x31, 0x02, 0x03, 0x14, 0x05, 0x06, 0x17, 0x08, 0x09, 0x17, 0x11, 0x12, 0x13, 0x14, 0x35, 0x16, 0x31, 0x02, 0x03, 0x14, 0x05, 0x06, 0x17, 0x08, 0x09, 0x17, 0x11, 0x12, 0x13, 0x14, 0x35, 0x16 };
|
||||
static private byte[] IV = { 0x21, 0x22, 0x33, 0x14, 0x05, 0x06, 0x17, 0x08, 0x09, 0x47, 0x11, 0x52, 0x13, 0x74, 0x15, 0x26 };
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="Utilities"/> class.
|
||||
/// </summary>
|
||||
/// Change Made On: 6/4/2009
|
||||
public Utils()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the product version.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// Changed On: 1/24/20081
|
||||
static public byte[] GetProductVersion() // Named this way on purpose.
|
||||
{
|
||||
return Key;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the product serial.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// Changed On: 1/24/2008
|
||||
static public byte[] GetProductSerial() // Named this way on purpose.
|
||||
{
|
||||
return IV;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether [is alpha numberic] [the specified input].
|
||||
/// </summary>
|
||||
/// <param name="Input">The input.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if [is alpha numberic] [the specified input]; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
static public bool IsAlphaNumberic(string Input)
|
||||
{
|
||||
foreach (char c in Input)
|
||||
{
|
||||
if (char.IsDigit(c))
|
||||
continue;
|
||||
if (char.IsLetter(c))
|
||||
continue;
|
||||
if (char.IsWhiteSpace(c))
|
||||
continue;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Left trim a string to a Max Lenth.
|
||||
/// </summary>
|
||||
/// <param name="Input">The input.</param>
|
||||
/// <param name="MaxLen">The max len.</param>
|
||||
/// <returns></returns>
|
||||
static public string LeftTrimString(string Input, int MaxLen)
|
||||
{
|
||||
int pLen = Input.Length;
|
||||
string Output = Input;
|
||||
if (pLen > MaxLen)
|
||||
{
|
||||
Output = Input.Remove(0, pLen - MaxLen);
|
||||
}
|
||||
return Output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses Symmetric encryption to encrypt a String.
|
||||
/// </summary>
|
||||
/// <param name="sLicenseKey">The s license key.</param>
|
||||
/// <param name="Key">The key.</param>
|
||||
/// <param name="IV">The IV.</param>
|
||||
/// <returns></returns>
|
||||
static public string EncryptKey(string sLicenseKey, byte[] Key, byte[] IV)
|
||||
{
|
||||
SymmetricAlgorithm CryptoSvc = new RijndaelManaged();
|
||||
CryptoSvc.Key = Key;
|
||||
CryptoSvc.IV = IV;
|
||||
ICryptoTransform encrypto = CryptoSvc.CreateEncryptor();
|
||||
|
||||
byte[] bytIn = StringToByteArray(sLicenseKey);
|
||||
|
||||
System.IO.MemoryStream ms = new System.IO.MemoryStream();
|
||||
|
||||
CryptoStream cs = new CryptoStream(ms, encrypto,
|
||||
CryptoStreamMode.Write);
|
||||
|
||||
cs.Write(bytIn, 0, bytIn.Length);
|
||||
cs.FlushFinalBlock();
|
||||
|
||||
byte[] bytOut = ms.GetBuffer();
|
||||
int i = 0;
|
||||
for (i = 0; i < bytOut.Length; i++)
|
||||
if (bytOut[i] == 0)
|
||||
break;
|
||||
|
||||
string Encrypted = System.Convert.ToBase64String(bytOut, 0, i);
|
||||
return Encrypted;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// MD5 encrypt a string and then encode result as base64 string.
|
||||
/// </summary>
|
||||
/// <param name="sSource">The s source.</param>
|
||||
/// <param name="TrimLength">Length of the trim.</param>
|
||||
/// <returns></returns>
|
||||
static public string MD5EncryptMsg(string sSource, int TrimLength)
|
||||
{
|
||||
string s64Trimmed = "";
|
||||
|
||||
// Create encryption code.
|
||||
ASCIIEncoding ascii = new ASCIIEncoding();
|
||||
Byte[] data1ToHash = ascii.GetBytes(sSource);
|
||||
byte[] hashvalue = (new MD5CryptoServiceProvider()).ComputeHash(data1ToHash);
|
||||
string s64Encoded = Convert.ToBase64String(hashvalue);
|
||||
|
||||
if (s64Encoded.Length > TrimLength)
|
||||
{
|
||||
s64Trimmed = s64Encoded.Substring(0, TrimLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
s64Trimmed = s64Encoded;
|
||||
}
|
||||
return s64Trimmed;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Uses Symmetric Decryption to decrypt a key.
|
||||
/// </summary>
|
||||
/// <param name="Source">The source.</param>
|
||||
/// <param name="Key">The key.</param>
|
||||
/// <param name="IV">The IV.</param>
|
||||
/// <returns></returns>
|
||||
static public string DecryptKey(string Source, byte[] Key, byte[] IV)
|
||||
{
|
||||
try
|
||||
{
|
||||
SymmetricAlgorithm CryptoSvc = new RijndaelManaged();
|
||||
CryptoSvc.Key = Key;
|
||||
CryptoSvc.IV = IV;
|
||||
ICryptoTransform encrypto = CryptoSvc.CreateDecryptor();
|
||||
|
||||
byte[] bytIn = System.Convert.FromBase64String(Source);
|
||||
|
||||
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn,
|
||||
0, bytIn.Length);
|
||||
|
||||
|
||||
CryptoStream cs = new CryptoStream(ms, encrypto,
|
||||
CryptoStreamMode.Read);
|
||||
|
||||
System.IO.StreamReader sr = new System.IO.StreamReader(cs);
|
||||
return (sr.ReadToEnd());
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Strings to byte array.
|
||||
/// </summary>
|
||||
/// <param name="source">The source.</param>
|
||||
/// <returns></returns>
|
||||
/// Changed On: 1/24/2008
|
||||
static public byte[] StringToByteArray(string source)
|
||||
{
|
||||
ASCIIEncoding Encoder = new ASCIIEncoding();
|
||||
byte[] Buffer = Encoder.GetBytes(source);
|
||||
return Buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert standard string to base 64 encoded string.
|
||||
/// </summary>
|
||||
/// <param name="source">The source.</param>
|
||||
/// <returns></returns>
|
||||
static public string StringToBase64String(string source)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Convert our Message to Base 64 encoded string.
|
||||
byte[] ba = StringToByteArray(source);
|
||||
string b64Encoded = Convert.ToBase64String(ba);
|
||||
return b64Encoded;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new Exception("Can not encode message. Error = " + e.Message);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert a byte array to an ascii string.
|
||||
/// </summary>
|
||||
/// <param name="source">The source.</param>
|
||||
/// <returns></returns>
|
||||
static public string ByteArrayToString(byte[] source)
|
||||
{
|
||||
ASCIIEncoding Encoder = new ASCIIEncoding();
|
||||
char[] CharArray = Encoder.GetChars(source);
|
||||
string ret = new string(CharArray);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a random 32 character GUID for use as a unique key
|
||||
/// in HL7 tables.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
static public string CreateGUID()
|
||||
{
|
||||
Guid g = Guid.NewGuid();
|
||||
string s = g.ToString("N"); // 32 character guid.
|
||||
return s;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Opens a text file and reads into a string, which it returns.
|
||||
/// </summary>
|
||||
/// <param name="filePath">The file path.</param>
|
||||
/// <returns></returns>
|
||||
static public string LoadTextFileIntoString(string filePath)
|
||||
{
|
||||
FileInfo aFile = new FileInfo(filePath);
|
||||
if (aFile.Exists == false)
|
||||
{
|
||||
throw new ApplicationException("File not found: " + filePath);
|
||||
}
|
||||
|
||||
FileStream fs = null;
|
||||
StreamReader r = null;
|
||||
string s = "";
|
||||
|
||||
try
|
||||
{
|
||||
fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
|
||||
r = new StreamReader(fs);
|
||||
r.BaseStream.Seek(0, SeekOrigin.Begin);
|
||||
s = r.ReadToEnd();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
string t = e.Message;
|
||||
}
|
||||
finally
|
||||
{
|
||||
r.Close();
|
||||
fs.Close();
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Builds a list of comma seperated and bracket delimited column names
|
||||
/// from a passed enumeration.
|
||||
/// Replaces "_" with a space in the column name.
|
||||
/// Replaces "_0_" with a '#' character in the column name.
|
||||
/// Replaces "_1_" with a ' - ' string in the column name.
|
||||
/// (Bit ugly, but allows us to use Enumerations for Column names
|
||||
/// which greatly helps us reduce ordinal indexing errors when
|
||||
/// reading DataReader results. It's a tradeoff).
|
||||
/// </summary>
|
||||
/// <param name="src">The SRC.</param>
|
||||
/// <returns></returns>
|
||||
static public string BuildColumnList(Array src)
|
||||
{
|
||||
StringBuilder b = new StringBuilder();
|
||||
|
||||
foreach (string s in src)
|
||||
{
|
||||
string s0 = s.Replace("_0_", "#");
|
||||
string s1 = s0.Replace("_1_", " - ");
|
||||
string s2 = s1.Replace('_', ' ');
|
||||
b.AppendFormat("[{0}], ", s2);
|
||||
}
|
||||
|
||||
// Remove trailing comma
|
||||
string t = b.ToString();
|
||||
RemoveTrailingComma(ref t);
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the trailing comma from a string if it has one.
|
||||
/// </summary>
|
||||
/// <param name="id">The id.</param>
|
||||
static public void RemoveTrailingComma(ref string id)
|
||||
{
|
||||
if (id.LastIndexOf(",") != -1)
|
||||
id = id.Remove(id.LastIndexOf(","), 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats DateTime object to "MM/DD/YYYY HH:MM:SS AM"
|
||||
/// This is suitable for inserts into Sql Server.
|
||||
/// </summary>
|
||||
/// <param name="dt">The dt.</param>
|
||||
/// <returns></returns>
|
||||
static public string SqlDTString(DateTime dt)
|
||||
{
|
||||
if (dt.Year < 1753) // sql server limit check.
|
||||
{
|
||||
return "01/01/1753 01:01:01 AM";
|
||||
}
|
||||
|
||||
string s = dt.ToShortDateString() + " " + dt.ToLongTimeString();
|
||||
return s;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats DateTime object to "MM/DD/YYYY HH:MM:SS"
|
||||
/// Where HH is in military time.
|
||||
/// This is suitable for inserts into Advantage Server.
|
||||
/// Note, Advantage requires the leading 0 on hours, minutes, seconds
|
||||
/// </summary>
|
||||
/// <param name="dt">The dt.</param>
|
||||
/// <returns></returns>
|
||||
static public string AdvDTStringJustDate(DateTime dt)
|
||||
{
|
||||
// medisoft 15 is having a fit with the time, they only want the date
|
||||
//string td = dt.TimeOfDay.ToString();
|
||||
//string s = dt.ToShortDateString() + " " + td;
|
||||
//return s;
|
||||
return dt.ToShortDateString() + " 00:00:00";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// returns only the time for the given date in a format that Advantage can use
|
||||
/// </summary>
|
||||
/// <param name="dt">The dt.</param>
|
||||
/// <returns></returns>
|
||||
static public string AdvTimeOnlyString(DateTime dt)
|
||||
{
|
||||
return dt.ToLongTimeString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert "Mm/Dd/YYYY" and "HHMM" strings to a valid DateTime structure.
|
||||
/// </summary>
|
||||
/// <param name="Date">The date.</param>
|
||||
/// <param name="Time">The time.</param>
|
||||
/// <returns></returns>
|
||||
static public DateTime CreateDtFromStrings(string Date, string Time)
|
||||
{
|
||||
DateTime iDate = DateTime.Now;
|
||||
try
|
||||
{
|
||||
iDate = DateTime.Parse(Date);
|
||||
|
||||
string HH = Time.Substring(0, 2);
|
||||
int iHours = Int32.Parse(HH);
|
||||
|
||||
string MM = Time.Substring(2, 2);
|
||||
int iMins = Int32.Parse(MM);
|
||||
|
||||
iDate = iDate.AddHours((double)iHours);
|
||||
iDate = iDate.AddMinutes((double)iMins);
|
||||
|
||||
return iDate;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return iDate;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a comma seperated string list of the passed in collection.
|
||||
/// </summary>
|
||||
/// <param name="src">The SRC.</param>
|
||||
/// <returns></returns>
|
||||
static public string BuildValueList(StringCollection src)
|
||||
{
|
||||
StringBuilder b = new StringBuilder();
|
||||
|
||||
foreach (string s in src)
|
||||
{
|
||||
b.AppendFormat("{0},", s);
|
||||
}
|
||||
|
||||
// Remove trailing comma
|
||||
string t = b.ToString();
|
||||
RemoveTrailingComma(ref t);
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Formats DateTime object to "MM/DD/YYYY HH:MM:SS"
|
||||
/// Where HH is in military time.
|
||||
/// This is suitable for inserts into Advantage Server.
|
||||
/// Note, Advantage requires the leading 0 on hours, minutes, seconds
|
||||
/// </summary>
|
||||
/// <param name="dt"></param>
|
||||
/// <returns></returns>
|
||||
static public string AdvDTString(DateTime dt)
|
||||
{
|
||||
string td = dt.TimeOfDay.ToString();
|
||||
string s = dt.ToShortDateString() + " " + td;
|
||||
return s;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Advs the DT string date only.
|
||||
/// </summary>
|
||||
/// <param name="dt">The dt.</param>
|
||||
/// <returns></returns>
|
||||
static public string AdvDTStringDateOnly(DateTime dt)
|
||||
{
|
||||
if (dt == new DateTime())
|
||||
{
|
||||
return new DateTime(0, 0, 0).ToShortDateString() + " 00:00:00";
|
||||
}
|
||||
else
|
||||
{
|
||||
return new DateTime(dt.Year, dt.Month, dt.Day).ToShortDateString() + " 00:00:00";
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Firsts the day of last month.
|
||||
/// </summary>
|
||||
/// <param name="date">The date.</param>
|
||||
/// <returns>the first day of the last month of the month given. eg. date is in december, will return november 1</returns>
|
||||
public static DateTime FirstDayOfLastMonth(DateTime date)
|
||||
{
|
||||
if ((date.Month - 1) == 0)
|
||||
{
|
||||
//this is january, return last december
|
||||
return new DateTime(date.Year - 1, 12, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return new DateTime(date.Year, date.Month - 1, 1);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lasts the day of last month.
|
||||
/// </summary>
|
||||
/// <param name="date">The date.</param>
|
||||
/// <returns>the last day of the last month of the month given. eg. date is in december, will return november 31</returns>
|
||||
public static DateTime LastDayOfLastMonth(DateTime date)
|
||||
{
|
||||
return date.AddMonths(-1).AddDays(-1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Firsts the day of week.
|
||||
/// </summary>
|
||||
/// <param name="date">The date.</param>
|
||||
/// <returns>the date of the sunday before the given date</returns>
|
||||
public static DateTime FirstDayOfWeek(DateTime date)
|
||||
{
|
||||
//get the day of week for the given date, subtract it to get sunday
|
||||
int dayOfWeek = (int)date.DayOfWeek;
|
||||
return DateTime.Now.AddDays(-dayOfWeek);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lasts the day of week.
|
||||
/// </summary>
|
||||
/// <param name="date">The date.</param>
|
||||
/// <returns>the date of the saturday following the given date</returns>
|
||||
public static DateTime LastDayOfWeek(DateTime date)
|
||||
{
|
||||
//get the day of week for the given date and add 7-it to get to the next saturday
|
||||
int dayOfWeek = (int)date.DayOfWeek;
|
||||
return DateTime.Now.AddDays(7 - dayOfWeek);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Firsts the day of next month.
|
||||
/// </summary>
|
||||
/// <param name="date">The date.</param>
|
||||
/// <returns>the first day of the next month of the given date</returns>
|
||||
public static DateTime FirstDayOfNextMonth(DateTime date)
|
||||
{
|
||||
return new DateTime(date.Year, date.Month + 1, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lasts the day of next month.
|
||||
/// </summary>
|
||||
/// <param name="date">The date.</param>
|
||||
/// <returns>the last day of the next month of the given date</returns>
|
||||
public static DateTime LastDayOfNextMonth(DateTime date)
|
||||
{
|
||||
return FirstDayOfNextMonth(date).AddMonths(1).AddDays(-1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Firsts the day of current month.
|
||||
/// </summary>
|
||||
/// <param name="date">The date.</param>
|
||||
/// <returns>the first day of the current month</returns>
|
||||
public static DateTime FirstDayOfCurrentMonth(DateTime date)
|
||||
{
|
||||
return new DateTime(date.Year, date.Month, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lasts the day of current month.
|
||||
/// </summary>
|
||||
/// <param name="date">The date.</param>
|
||||
/// <returns>the last day of the current month</returns>
|
||||
public static DateTime LastDayOfCurrentMonth(DateTime date)
|
||||
{
|
||||
return FirstDayOfCurrentMonth(date).AddMonths(1).AddDays(-1);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Reference in New Issue
Block a user