clsXOREncryption.cs

00001 // C# ClassLib
00002 // http://csharp-classlib.sourceforge.net
00003 //
00004 //
00005 // Copyright (C) 2005 Marcel Joachim Kloubert
00006 // 
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 // 
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with this program; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 //
00021 
00022 using System;
00023 using System.Text;
00024 
00025 namespace CSharp_ClassLib.Cryptography
00026 {
00049         public class XOR : System.Object
00050         {
00052                 private byte[] m_bExpression;
00054                 private byte[] m_bKey;
00056                 private System.Text.ASCIIEncoding m_oASCII;
00057 
00070                 public XOR ()
00071                 {
00072                         m_oASCII = new System.Text.ASCIIEncoding();
00073                         
00074                         KeyBytes = new byte[0];
00075                         ExpressionBytes = new byte[0];
00076                         
00077                         return;
00078                 }
00079         
00098                 public XOR (byte[] expression, byte[] key)
00099                 {
00100                         this.m_oASCII = new ASCIIEncoding();
00101                         
00102                         this.KeyBytes = key;
00103                         ExpressionBytes = expression;
00104                 }
00105                 
00122                 public XOR (byte[] expression)
00123                 {
00124                         m_oASCII = new ASCIIEncoding();
00125                         
00126                         KeyBytes = new byte[0];
00127                         ExpressionBytes = expression;
00128                 }
00129                 
00147                 public byte[] KeyBytes
00148                 {
00149                         get { return this.m_bKey; }
00150                         set { this.m_bKey = value; }
00151                 }
00152                 
00170                 public string KeyString
00171                 {
00172                         get { return this.m_oASCII.GetString(KeyBytes); }
00173                         set { this.KeyBytes = this.m_oASCII.GetBytes(value); }
00174                 }
00175                 
00193                 public string KeyHex
00194                 {
00195                         get { return this.ToHexString(KeyBytes); }
00196                         set { this.KeyBytes = this.ToByteArray(value); }
00197                 }
00198 
00216                 public byte[] ExpressionBytes
00217                 {
00218                         get { return this.m_bExpression; }
00219                         set { this.m_bExpression = value; }
00220                 }
00221                 
00239                 public string ExpressionString
00240                 {
00241                         get { return this.m_oASCII.GetString(this.ExpressionBytes); }
00242                         set { this.ExpressionBytes = this.m_oASCII.GetBytes(value); }
00243                 }               
00244                 
00262                 public string ExpressionHex
00263                 {
00264                         get { return this.ToHexString(this.ExpressionBytes); }
00265                         set { this.ExpressionBytes = this.ToByteArray(value); }
00266                 }               
00267                 
00285                 public byte[] ExpressionEncryptedBytes
00286                 {
00287                         get { return this.EncryptXOR(this.ExpressionBytes, this.KeyBytes); }
00288                         set { this.ExpressionBytes = this.EncryptXOR(value, this.KeyBytes); }
00289                 }               
00290                 
00308                 public string ExpressionEncryptedHex
00309                 {
00310                         get { return this.ToHexString(this.ExpressionEncryptedBytes); }
00311                         set { this.ExpressionEncryptedBytes = this.ToByteArray(value); }
00312                 }               
00313                 
00323                 private byte[] EncryptXOR (byte[] Expression, byte[] Key)
00324                 {
00325                         byte[] bResult = new byte[0];
00326                 
00327                         if (null == Expression)
00328                                 // empty expression
00329                                 return bResult;
00330                         if (Expression.GetLength(0) < 1)
00331                                 // empty expression
00332                                 return bResult;
00333                 
00334                         if (null == Key)
00335                                 // key is null => no encryption
00336                                 return Expression;
00337                         if (Key.GetLength(0) < 1)
00338                                 // key is empty => no encryption
00339                                 return Expression;
00340 
00341                         int i = 0;
00342                         bResult = new byte[Expression.GetLength(0)];
00343 
00344                         // encrypt the expression
00345                         for (i = 0; i < Expression.GetLength(0); i++)
00346                                 bResult[i] = (byte)(Expression[i] ^ Key[(int)(i % Key.GetLength(0))]);
00347         
00348                         return bResult;
00349                 }
00350                 
00359                 private string ToHexString (byte[] Expression)
00360                 {
00361                         if (null == Expression)
00362                                 return "";
00363                                 
00364                         int i = 0;
00365                         byte bHigh = 0;
00366                         byte bLow = 0;
00367                         string strBuffer = "";
00368                         string strHexString = "0123456789ABCDEF";
00369 
00370                         for (i = 0; i < Expression.GetLength(0); i++)
00371                         {
00372                                 // calc the high & the low byte
00373                                 bHigh = (byte)System.Math.Floor((double)Expression[i] / 16.0);
00374                                 bLow = (byte)(Expression[i] % 16);
00375         
00376                                 // add the character as 2-character-hex-expression
00377                                 strBuffer += strHexString.Substring(bHigh, 1) + strHexString.Substring(bLow, 1);
00378                         }
00379                 
00380                         return strBuffer;
00381                 }
00382                 
00393                 private byte[] ToByteArray (string Expression)
00394                 {
00395                         byte[] bResult;
00396                 
00397                         if ("" == Expression)
00398                         {
00399                                 // empty expression
00400                         
00401                                 bResult = new byte[0];
00402                                 return bResult;
00403                         }
00404 
00405                         if (0 != (Expression.Length % 2))
00406                                 // invalid format                       
00407                                 throw (new System.ArgumentException("Wrong expression size!", "Expression"));
00408 
00409                         int i = 0;
00410                         string strHexString = "0123456789ABCDEF";
00411                         string strString = "";
00412                         bResult = new byte[(int)System.Math.Floor(Expression.Length / 2.0)];
00413 
00414                         for (i = 0; i < bResult.GetLength(0); i++)
00415                         {
00416                                 bResult[i] = 0;
00417                         
00418                                 // high byte
00419                                 strString = Expression.Substring(i * 2, 1).ToUpper();
00420                                 if (strHexString.IndexOf(strString) > -1)
00421                                         // is a hex character => set the high byte
00422                                         bResult[i] += (byte)(16 * strHexString.IndexOf(strString));
00423                                 else
00424                                         // no hex char => exception                             
00425                                         throw (new System.ArgumentException("No hex string!", "Expression"));
00426                         
00427                                 // low byte
00428                                 strString = Expression.Substring(i * 2 + 1, 1).ToUpper();
00429                                 if (strHexString.IndexOf(strString) > -1)
00430                                         // is a hex character => set the low byte
00431                                         bResult[i] += (byte)(strHexString.IndexOf(strString));
00432                                 else
00433                                         // no hex char => exception
00434                                         throw (new System.ArgumentException("No hex string!", "Expression"));
00435                         }
00436 
00437                         return bResult;
00438                 }
00439         }
00440 }

Generated on Thu Jun 2 08:41:49 2005 for C# ClassLib by  doxygen 1.4.1