00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00329 return bResult;
00330 if (Expression.GetLength(0) < 1)
00331
00332 return bResult;
00333
00334 if (null == Key)
00335
00336 return Expression;
00337 if (Key.GetLength(0) < 1)
00338
00339 return Expression;
00340
00341 int i = 0;
00342 bResult = new byte[Expression.GetLength(0)];
00343
00344
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
00373 bHigh = (byte)System.Math.Floor((double)Expression[i] / 16.0);
00374 bLow = (byte)(Expression[i] % 16);
00375
00376
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
00400
00401 bResult = new byte[0];
00402 return bResult;
00403 }
00404
00405 if (0 != (Expression.Length % 2))
00406
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
00419 strString = Expression.Substring(i * 2, 1).ToUpper();
00420 if (strHexString.IndexOf(strString) > -1)
00421
00422 bResult[i] += (byte)(16 * strHexString.IndexOf(strString));
00423 else
00424
00425 throw (new System.ArgumentException("No hex string!", "Expression"));
00426
00427
00428 strString = Expression.Substring(i * 2 + 1, 1).ToUpper();
00429 if (strHexString.IndexOf(strString) > -1)
00430
00431 bResult[i] += (byte)(strHexString.IndexOf(strString));
00432 else
00433
00434 throw (new System.ArgumentException("No hex string!", "Expression"));
00435 }
00436
00437 return bResult;
00438 }
00439 }
00440 }