[Csharp-classlib-cvs] demos/XOR/XOR/Classes clsXOREncryption.cs,NONE,1.1
Status: Inactive
Brought to you by:
generalpd
From: Marcel K. <gen...@us...> - 2005-05-05 01:21:55
|
Update of /cvsroot/csharp-classlib/demos/XOR/XOR/Classes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21020/XOR/XOR/Classes Added Files: clsXOREncryption.cs Log Message: - added demo for XOR encryption class --- NEW FILE: clsXOREncryption.cs --- // C# ClassLib // http://csharp-classlib.sourceforge.net // // // Copyright (C) 2005 Marcel Joachim Kloubert // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // using System; using System.Text; namespace CSharp_ClassLib.Cryptography { /*! \brief XOR encryption class * * Class that contains routines to handle XOR encryption * * \author generalpd * * \version $Revision: 1.1 $ * * \date 2005-04-18 * * \bug none * * \todo none * * \remarks Last changed by: $Author: generalpd $ in revision $Revision: 1.1 $ on $Date: 2005/05/05 01:21:46 $ * * \note * $Log: clsXOREncryption.cs,v $ * Revision 1.1 2005/05/05 01:21:46 generalpd * - added demo for XOR encryption class * * */ public class XOR : System.Object { /// Expression private byte[] m_bExpression; /// Key private byte[] m_bKey; /// ASCII Encoding private System.Text.ASCIIEncoding m_oASCII; /*! \fn XOR () * \brief Class constructor that inits all nesessary stuff * * \code * * // Code example * * CSharp_ClassLib.Cryptography.XOR m_oXOR = new CSharp_ClassLib.Cryptography.XOR(); * * \endcode * */ public XOR () { m_oASCII = new System.Text.ASCIIEncoding(); KeyBytes = new byte[0]; ExpressionBytes = new byte[0]; return; } /*! \fn XOR (byte[] expression, byte[] key) * \brief Class constructor that inits all nesessary stuff * * \param expression The expression * \param key The key * * \code * * // Code example * * bExpression = m_oAscii.GetBytes("I hate windows!"); * bKey = m_oAscii.GetBytes("But I love .NET :D!"); * * CSharp_ClassLib.Cryptography.XOR m_oXOR = new CSharp_ClassLib.Cryptography.XOR(bExpression, bKey); * * \endcode * */ public XOR (byte[] expression, byte[] key) { this.m_oASCII = new ASCIIEncoding(); this.KeyBytes = key; ExpressionBytes = expression; } /*! \fn XOR (byte[] expression, byte[] key) * \brief Class constructor that inits all nesessary stuff * * \param expression The expression * \param key The key * * \code * * // Code example * * bExpression = m_oAscii.GetBytes("I hate windows!"); * CSharp_ClassLib.Cryptography.XOR m_oXOR = new CSharp_ClassLib.Cryptography.XOR(bExpression); * * \endcode * */ public XOR (byte[] expression) { m_oASCII = new ASCIIEncoding(); KeyBytes = new byte[0]; ExpressionBytes = expression; } /*! \property byte[] KeyBytes * \brief Returns or sets the key * * \param value The new key * * \return The key * * \code * * // Code example * * m_oXOR.KeyBytes = m_oAscii.GetBytes("This is a key!"); * Console.WriteLine("The key is: " + m_oAscii.GetString(m_oXOR.KeyBytes)); * * \endcode * */ public byte[] KeyBytes { get { return this.m_bKey; } set { this.m_bKey = value; } } /*! \property string KeyString * \brief Returns or sets the key * * \param value The new key * * \return The key * * \code * * // Code example * * m_oXOR.KeyString = "This is a key!"; * Console.WriteLine("The key is: " + m_oXOR.KeyString); * * \endcode * */ public string KeyString { get { return this.m_oASCII.GetString(KeyBytes); } set { this.KeyBytes = this.m_oASCII.GetBytes(value); } } /*! \property string KeyHey * \brief Returns or sets the key * * \param value The new key * * \return The key * * \code * * // Code example * * m_oXOR.KeyHex = "0123456789ABCDEF"; * Console.WriteLine("The key is: " + m_oXOR.KeyHex); * * \endcode * */ public string KeyHex { get { return this.ToHexString(KeyBytes); } set { this.KeyBytes = this.ToByteArray(value); } } /*! \property byte[] ExpressionBytes * \brief Returns or sets the expression * * \param value The new expression * * \return The expression * * \code * * // Code example * * m_oXOR.ExpressionBytes = m_oAscii.GetBytes("This is an expression."); * Console.WriteLine("The expression is: " + m_oAscii.GetString(m_oXOR.ExpressionBytes)); * * \endcode * */ public byte[] ExpressionBytes { get { return this.m_bExpression; } set { this.m_bExpression = value; } } /*! \property string ExpressionString * \brief Returns or sets the expression * * \param value The new expression * * \return The expression * * \code * * // Code example * * m_oXOR.ExpressionString = "This is an expression."; * Console.WriteLine("The expression is: " + m_oXOR.ExpressionString); * * \endcode * */ public string ExpressionString { get { return this.m_oASCII.GetString(this.ExpressionBytes); } set { this.ExpressionBytes = this.m_oASCII.GetBytes(value); } } /*! \property string ExpressionHex * \brief Returns or sets the expression * * \param value The new expression * * \return The expression * * \code * * // Code example * * m_oXOR.ExpressionHex = "0123456789ABCDEF"; * Console.WriteLine("The expression is: " + m_oXOR.ExpressionHex); * * \endcode * */ public string ExpressionHex { get { return this.ToHexString(this.ExpressionBytes); } set { this.ExpressionBytes = this.ToByteArray(value); } } /*! \property byte[] ExpressionEncryptedBytes * \brief Returns or sets the encrypted expression * * \param value The new (encrypted) expression * * \return The encrypted expression * * \code * * // Code example * * m_oXOR.ExpressionEncrypted = "jhdsakjsadj3287"; * Console.WriteLine("The encrypted expression is: " + m_oASCII.GetString(m_oXOR.ExpressionEncrypted)); * * \endcode * */ public byte[] ExpressionEncryptedBytes { get { return this.EncryptXOR(this.ExpressionBytes, this.KeyBytes); } set { this.ExpressionBytes = this.EncryptXOR(value, this.KeyBytes); } } /*! \property string ExpressionEncryptedHex * \brief Returns or sets the encrypted expression * * \param value The new (encrypted) expression * * \return The encrypted expression * * \code * * // Code example * * m_oXOR.ExpressionEncryptedHex = "0123456789abcdef"; * Console.WriteLine("The encrypted expression is: " + m_oASCII.GetString(m_oXOR.ExpressionEncryptedHex)); * * \endcode * */ public string ExpressionEncryptedHex { get { return this.ToHexString(this.ExpressionEncryptedBytes); } set { this.ExpressionEncryptedBytes = this.ToByteArray(value); } } /*! \fn byte[] EncryptXOR (byte[] Expression, byte[] Key) * \brief Internal routine that encrypt an expression by a key * * \param Expression The expression to encrypt * \param Key The encryption key * * \return The encrypted expression * */ private byte[] EncryptXOR (byte[] Expression, byte[] Key) { byte[] bResult = new byte[0]; if (null == Expression) // empty expression return bResult; if (Expression.GetLength(0) < 1) // empty expression return bResult; if (null == Key) // key is null => no encryption return Expression; if (Key.GetLength(0) < 1) // key is empty => no encryption return Expression; int i = 0; bResult = new byte[Expression.GetLength(0)]; // encrypt the expression for (i = 0; i < Expression.GetLength(0); i++) bResult[i] = (byte)(Expression[i] ^ Key[(int)(i % Key.GetLength(0))]); return bResult; } /*! \fn string ToHexString (byte[] Expression) * \brief Internal routine that converts an expression to a hex string * * \param Expression The expression to convert * * \return The expression as hex string * */ private string ToHexString (byte[] Expression) { if (null == Expression) return ""; int i = 0; byte bHigh = 0; byte bLow = 0; string strBuffer = ""; string strHexString = "0123456789ABCDEF"; for (i = 0; i < Expression.GetLength(0); i++) { // calc the high & the low byte bHigh = (byte)System.Math.Floor((double)Expression[i] / 16.0); bLow = (byte)(Expression[i] % 16); // add the character as 2-character-hex-expression strBuffer += strHexString.Substring(bHigh, 1) + strHexString.Substring(bLow, 1); } return strBuffer; } /*! \fn byte[] ToByteArray (string Expression) * \brief Internal routine that converts an hex expression to a byte array * * \param Expression The expression to convert * * \return The expression as byte array * * \exception System.ArgumentException Invalid format of the expression * */ private byte[] ToByteArray (string Expression) { byte[] bResult; if ("" == Expression) { // empty expression bResult = new byte[0]; return bResult; } if (0 != (Expression.Length % 2)) // invalid format throw (new System.ArgumentException("Wrong expression size!", "Expression")); int i = 0; string strHexString = "0123456789ABCDEF"; string strString = ""; bResult = new byte[(int)System.Math.Floor(Expression.Length / 2.0)]; for (i = 0; i < bResult.GetLength(0); i++) { bResult[i] = 0; // high byte strString = Expression.Substring(i * 2, 1).ToUpper(); if (strHexString.IndexOf(strString) > -1) // is a hex character => set the high byte bResult[i] += (byte)(16 * strHexString.IndexOf(strString)); else // no hex char => exception throw (new System.ArgumentException("No hex string!", "Expression")); // low byte strString = Expression.Substring(i * 2 + 1, 1).ToUpper(); if (strHexString.IndexOf(strString) > -1) // is a hex character => set the low byte bResult[i] += (byte)(strHexString.IndexOf(strString)); else // no hex char => exception throw (new System.ArgumentException("No hex string!", "Expression")); } return bResult; } } } |