Click here to Skip to main content
16,007,885 members
Home / Discussions / C#
   

C#

 
GeneralRe: Importent questions that I need very fast Pin
Judah Gabriel Himango12-May-06 10:48
sponsorJudah Gabriel Himango12-May-06 10:48 
GeneralRe: Importent questions that I need very fast Pin
led mike12-May-06 10:52
led mike12-May-06 10:52 
AnswerRe: Importent questions that I need very fast Pin
Ravi Bhavnani12-May-06 6:52
professionalRavi Bhavnani12-May-06 6:52 
GeneralRe: Importent questions that I need very fast Pin
NaNg1524112-May-06 7:06
NaNg1524112-May-06 7:06 
GeneralRe: Importent questions that I need very fast Pin
leppie12-May-06 7:19
leppie12-May-06 7:19 
GeneralRe: Importent questions that I need very fast Pin
NaNg1524112-May-06 7:25
NaNg1524112-May-06 7:25 
GeneralRe: Importent questions that I need very fast Pin
leppie12-May-06 11:50
leppie12-May-06 11:50 
AnswerRe: Importent questions that I need very fast Pin
leppie12-May-06 12:38
leppie12-May-06 12:38 
Here's something that should help:
<FONT color=Blue>public abstract class</FONT> GenericTransform : SymmetricAlgorithm, ICryptoTransform
{
  <FONT color=Blue>static readonly</FONT> RandomNumberGenerator  RANDOM  = RandomNumberGenerator<FONT color=DarkBlue>.</FONT>Create<FONT color=DarkBlue>()</FONT>;
  <FONT color=Blue>static readonly</FONT> HashAlgorithm          HASH    = HashAlgorithm<FONT color=DarkBlue>.</FONT>Create<FONT color=DarkBlue>()</FONT>;

  TransformMode curmode = TransformMode<FONT color=DarkBlue>.</FONT>None;
  <FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> initial_iv;

  <FONT color=Blue>protected enum</FONT> TransformMode
  {
    None, Encrypt, Decrypt
  }

  <FONT color=Blue>protected</FONT> TransformMode CurrentMode
  {
    <FONT color=Blue>get</FONT> {<FONT color=Blue>return</FONT> curmode;}
    <FONT color=Blue>set</FONT> {curmode = <FONT color=Blue>value</FONT>;}
  }

  <FONT color=Blue>protected</FONT> GenericTransform<FONT color=DarkBlue>()</FONT>
  {
    <FONT color=DarkGreen>// in bits</FONT>
    BlockSizeValue = <FONT color=Red>64</FONT>;
    KeySizeValue = <FONT color=Red>128</FONT>;
    PaddingValue = PaddingMode<FONT color=DarkBlue>.</FONT>PKCS7;

    LegalKeySizesValue = <FONT color=Blue>new</FONT> KeySizes<FONT color=DarkBlue>[]</FONT>{ <FONT color=Blue>new</FONT> KeySizes<FONT color=DarkBlue>(</FONT>KeySizeValue,KeySizeValue,<FONT color=Red>0</FONT><FONT color=DarkBlue>)</FONT> };
    LegalBlockSizesValue = <FONT color=Blue>new</FONT> KeySizes<FONT color=DarkBlue>[]</FONT>{ <FONT color=Blue>new</FONT> KeySizes<FONT color=DarkBlue>(</FONT>BlockSizeValue,BlockSizeValue,<FONT color=Red>0</FONT><FONT color=DarkBlue>)</FONT> };
  }

  <FONT color=DarkGreen>// in bytes</FONT>
  <FONT color=Blue>int</FONT> ICryptoTransform<FONT color=DarkBlue>.</FONT>InputBlockSize
  {
    <FONT color=Blue>get</FONT> { <FONT color=Blue>return</FONT> BlockSizeValue/<FONT color=Red>8</FONT>; }
  }

  <FONT color=DarkGreen>// in bytes</FONT>
  <FONT color=Blue>public abstract int</FONT> OutputBlockSize {<FONT color=Blue>get</FONT>;}

  <FONT color=Blue>public abstract int</FONT> TransformBlock<FONT color=DarkBlue>(</FONT><FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> inputBuffer, <FONT color=Blue>int</FONT> inputOffset, <FONT color=Blue>int</FONT> inputCount, <FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> outputBuffer, <FONT color=Blue>int</FONT> outputOffset<FONT color=DarkBlue>)</FONT>;

  <FONT color=Blue>public byte</FONT><FONT color=DarkBlue>[]</FONT> TransformFinalBlock<FONT color=DarkBlue>(</FONT><FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> inputBuffer, <FONT color=Blue>int</FONT> inputOffset, <FONT color=Blue>int</FONT> inputCount<FONT color=DarkBlue>)</FONT>
  {
    <FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> outputBuffer = <FONT color=Blue>null</FONT>;
    <FONT color=Blue>bool</FONT> enc = curmode <FONT color=DarkBlue>==</FONT> TransformMode<FONT color=DarkBlue>.</FONT>Encrypt;

    <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>PaddingValue <FONT color=DarkBlue>==</FONT> PaddingMode<FONT color=DarkBlue>.</FONT>None<FONT color=DarkBlue>)</FONT>
    {
      outputBuffer = <FONT color=Blue>new byte</FONT>[inputCount];
    }
    <FONT color=Blue>else</FONT>
    {
      outputBuffer = <FONT color=Blue>new byte</FONT>[OutputBlockSize];

      <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>enc<FONT color=DarkBlue>)</FONT>
      {
        <FONT color=DarkGreen>// clear any garbage given to us by Cryptostream, do padding</FONT>
        <FONT color=Blue>byte</FONT> pad = <FONT color=DarkBlue>(</FONT><FONT color=Blue>byte</FONT><FONT color=DarkBlue>)(</FONT>OutputBlockSize - inputCount<FONT color=DarkBlue>)</FONT>;

        <FONT color=Blue>for</FONT> <FONT color=DarkBlue>(</FONT><FONT color=Blue>int</FONT> i = inputCount; i < OutputBlockSize; i<FONT color=DarkBlue>++)</FONT>
        {
          inputBuffer[i] = <FONT color=DarkBlue>(</FONT>PaddingValue <FONT color=DarkBlue>==</FONT> PaddingMode<FONT color=DarkBlue>.</FONT>PKCS7 ? pad : <FONT color=DarkBlue>(</FONT><FONT color=Blue>byte</FONT><FONT color=DarkBlue>)</FONT><FONT color=Red>0</FONT><FONT color=DarkBlue>)</FONT>;
        }
      }
    }

    <FONT color=Blue>int</FONT> c = TransformBlock<FONT color=DarkBlue>(</FONT>inputBuffer, inputOffset, outputBuffer<FONT color=DarkBlue>.</FONT>Length, outputBuffer, <FONT color=Red>0</FONT><FONT color=DarkBlue>)</FONT>;

    <FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> final = <FONT color=Blue>null</FONT>;

    <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>PaddingValue <FONT color=DarkBlue>==</FONT> PaddingMode<FONT color=DarkBlue>.</FONT>None<FONT color=DarkBlue>)</FONT>
    {
      final = <FONT color=Blue>new byte</FONT>[inputCount];
    }
    <FONT color=Blue>else</FONT>
    {
      <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>enc<FONT color=DarkBlue>)</FONT>
      {
        final = <FONT color=Blue>new byte</FONT>[OutputBlockSize];
      }
      <FONT color=Blue>else</FONT>
      {
        <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>inputCount <FONT color=DarkBlue>==</FONT> <FONT color=Red>0</FONT><FONT color=DarkBlue>)</FONT>
        {
          final = <FONT color=Blue>new byte</FONT>[<FONT color=Red>0</FONT>];
        }
        <FONT color=Blue>else</FONT>
        {
          <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>PaddingValue <FONT color=DarkBlue>==</FONT> PaddingMode<FONT color=DarkBlue>.</FONT>Zeros<FONT color=DarkBlue>)</FONT>
          {
            <FONT color=Blue>for</FONT> <FONT color=DarkBlue>(</FONT><FONT color=Blue>int</FONT> i = outputBuffer<FONT color=DarkBlue>.</FONT>Length - <FONT color=Red>1</FONT>; i <FONT color=DarkBlue>>=</FONT> <FONT color=Red>0</FONT>; i<FONT color=DarkBlue>--)</FONT>
            {
              <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>outputBuffer[i] <FONT color=DarkBlue>!=</FONT> <FONT color=Red>0</FONT><FONT color=DarkBlue>)</FONT>
              {
                final = <FONT color=Blue>new byte</FONT>[i + <FONT color=Red>1</FONT>];
                <FONT color=Blue>break</FONT>;
              }
            }
          }
          <FONT color=Blue>else</FONT>
          {
            <FONT color=Blue>byte</FONT> pk = outputBuffer[outputBuffer<FONT color=DarkBlue>.</FONT>Length - <FONT color=Red>1</FONT>];

            <FONT color=Blue>for</FONT> <FONT color=DarkBlue>(</FONT><FONT color=Blue>int</FONT> i = outputBuffer<FONT color=DarkBlue>.</FONT>Length - <FONT color=Red>1</FONT>; <FONT color=DarkBlue>(</FONT>i <FONT color=DarkBlue>>=</FONT>  outputBuffer<FONT color=DarkBlue>.</FONT>Length - <FONT color=Red>1</FONT> - pk<FONT color=DarkBlue>)</FONT>; i<FONT color=DarkBlue>--)</FONT>
            {
              <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>outputBuffer[i] <FONT color=DarkBlue>!=</FONT> pk<FONT color=DarkBlue>)</FONT>
              {
                final = <FONT color=Blue>new byte</FONT>[i + <FONT color=Red>1</FONT>];
                <FONT color=Blue>break</FONT>;
              }
            }
          }
        }
      }
    }

    <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>final <FONT color=DarkBlue>==</FONT> <FONT color=Blue>null</FONT><FONT color=DarkBlue>)</FONT>
    {
      final = <FONT color=Blue>new byte</FONT>[outputBuffer<FONT color=DarkBlue>.</FONT>Length];
    }

    Buffer<FONT color=DarkBlue>.</FONT>BlockCopy<FONT color=DarkBlue>(</FONT>outputBuffer, inputOffset, final, <FONT color=Red>0</FONT>, final<FONT color=DarkBlue>.</FONT>Length<FONT color=DarkBlue>)</FONT>;
    IVValue = initial_iv;
    <FONT color=Blue>return</FONT> final;
  }

  <FONT color=Blue>public sealed override</FONT> ICryptoTransform CreateDecryptor<FONT color=DarkBlue>(</FONT><FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> rgbKey, <FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> rgbIV<FONT color=DarkBlue>)</FONT>
  {
    <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>rgbKey <FONT color=DarkBlue>!=</FONT> <FONT color=Blue>null</FONT><FONT color=DarkBlue>)</FONT>
    {
      Key = rgbKey;
    }
    <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>rgbIV <FONT color=DarkBlue>!=</FONT> <FONT color=Blue>null</FONT><FONT color=DarkBlue>)</FONT>
    {
      IV = rgbIV;
    }
    curmode = TransformMode<FONT color=DarkBlue>.</FONT>Decrypt;
    <FONT color=Blue>return this</FONT>;
  }

  <FONT color=Blue>public sealed override</FONT> ICryptoTransform CreateEncryptor<FONT color=DarkBlue>(</FONT><FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> rgbKey, <FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> rgbIV<FONT color=DarkBlue>)</FONT>
  {
    <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>rgbKey <FONT color=DarkBlue>!=</FONT> <FONT color=Blue>null</FONT><FONT color=DarkBlue>)</FONT>
    {
      Key = rgbKey;
    }
    <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>rgbIV <FONT color=DarkBlue>!=</FONT> <FONT color=Blue>null</FONT><FONT color=DarkBlue>)</FONT>
    {
      IV = rgbIV;
    }
    curmode = TransformMode<FONT color=DarkBlue>.</FONT>Encrypt;
    <FONT color=Blue>return this</FONT>;
  }

  <FONT color=Blue>public sealed override void</FONT> GenerateIV<FONT color=DarkBlue>()</FONT>
  {
    IVValue = <FONT color=Blue>new byte</FONT>[BlockSizeValue/<FONT color=Red>8</FONT>];
    <FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> h = HASH<FONT color=DarkBlue>.</FONT>ComputeHash<FONT color=DarkBlue>(</FONT>Key<FONT color=DarkBlue>)</FONT>;
    Buffer<FONT color=DarkBlue>.</FONT>BlockCopy<FONT color=DarkBlue>(</FONT>h,<FONT color=Red>0</FONT>,IVValue,<FONT color=Red>0</FONT>, IVValue<FONT color=DarkBlue>.</FONT>Length<FONT color=DarkBlue>)</FONT>;
  }

  <FONT color=Blue>public sealed override void</FONT> GenerateKey<FONT color=DarkBlue>()</FONT>
  {
    KeyValue = <FONT color=Blue>new byte</FONT>[KeySizeValue/<FONT color=Red>8</FONT>];
    RANDOM<FONT color=DarkBlue>.</FONT>GetNonZeroBytes<FONT color=DarkBlue>(</FONT>KeyValue<FONT color=DarkBlue>)</FONT>;
  }

  <FONT color=Blue>public override byte</FONT><FONT color=DarkBlue>[]</FONT> IV
  {
    <FONT color=Blue>get</FONT>
    {
      <FONT color=Blue>return base</FONT><FONT color=DarkBlue>.</FONT>IV;
    }
    <FONT color=Blue>set</FONT>
    {
      <FONT color=Blue>base</FONT><FONT color=DarkBlue>.</FONT>IV = <FONT color=Blue>value</FONT>;
      initial_iv = IVValue<FONT color=DarkBlue>.</FONT>Clone<FONT color=DarkBlue>()</FONT> <FONT color=Blue>as byte</FONT><FONT color=DarkBlue>[]</FONT>;
    }
  }


  <FONT color=Blue>public virtual bool</FONT> CanReuseTransform
  {
    <FONT color=Blue>get</FONT> {<FONT color=Blue>return false</FONT>;}
  }

  <FONT color=Blue>public virtual bool</FONT> CanTransformMultipleBlocks
  {
    <FONT color=Blue>get</FONT> { <FONT color=Blue>return false</FONT>; }
  }


  <FONT color=Blue>static string</FONT> ByteAsString<FONT color=DarkBlue>(</FONT><FONT color=Blue>byte</FONT><FONT color=DarkBlue>[]</FONT> input<FONT color=DarkBlue>)</FONT>
  {
    <FONT color=Blue>string</FONT> output = <FONT color=Maroon>""</FONT>;
    <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>input <FONT color=DarkBlue>!=</FONT> <FONT color=Blue>null</FONT><FONT color=DarkBlue>)</FONT>
    {
      <FONT color=Blue>foreach</FONT> <FONT color=DarkBlue>(</FONT><FONT color=Blue>byte</FONT> b <FONT color=Blue>in</FONT> input<FONT color=DarkBlue>)</FONT>
      {
        output <FONT color=DarkBlue>+=</FONT> b<FONT color=DarkBlue>.</FONT>ToString<FONT color=DarkBlue>(</FONT><FONT color=Maroon>"X2"</FONT><FONT color=DarkBlue>)</FONT>;
      }
    }
    <FONT color=Blue>else</FONT>
    {
      output = <FONT color=Maroon>"NULL"</FONT>;
    }
    <FONT color=Blue>return</FONT> output;
  }

  <FONT color=Blue>public sealed override string</FONT> ToString<FONT color=DarkBlue>()</FONT>
  {
    <FONT color=Blue>if</FONT> <FONT color=DarkBlue>(</FONT>Debugger<FONT color=DarkBlue>.</FONT>IsAttached<FONT color=DarkBlue>)</FONT>
    {
      <FONT color=Blue>return string</FONT><FONT color=DarkBlue>.</FONT>Format<FONT color=DarkBlue>(</FONT><FONT color=Maroon>"{0}(KEY({3}) = {1},\nIV({4}) = {2})"</FONT>, GetType<FONT color=DarkBlue>().</FONT>Name,
        ByteAsString<FONT color=DarkBlue>(</FONT>KeyValue<FONT color=DarkBlue>)</FONT>, ByteAsString<FONT color=DarkBlue>(</FONT>IVValue<FONT color=DarkBlue>)</FONT>, KeySize, BlockSize<FONT color=DarkBlue>)</FONT>;
    }
    <FONT color=Blue>else</FONT>
    {
      <FONT color=Blue>return base</FONT><FONT color=DarkBlue>.</FONT>ToString<FONT color=DarkBlue>()</FONT>;
    }
  }
}



GeneralRe: Importent questions that I need very fast Pin
J4amieC12-May-06 23:35
J4amieC12-May-06 23:35 
AnswerRe: Importent questions that I need very fast Pin
V.12-May-06 9:51
professionalV.12-May-06 9:51 
GeneralRe: Importent questions that I need very fast Pin
NaNg1524112-May-06 10:28
NaNg1524112-May-06 10:28 
AnswerRe: Importent questions that I need very fast Pin
J4amieC12-May-06 10:11
J4amieC12-May-06 10:11 
NewsRe: Importent questions that I need very fast Pin
NaNg1524112-May-06 10:33
NaNg1524112-May-06 10:33 
GeneralRe: Importent questions that I need very fast Pin
J4amieC12-May-06 10:43
J4amieC12-May-06 10:43 
GeneralRe: Importent questions that I need very fast Pin
led mike12-May-06 11:30
led mike12-May-06 11:30 
Questionobject-access question Pin
spin vector12-May-06 6:12
spin vector12-May-06 6:12 
AnswerRe: object-access question Pin
NaNg1524112-May-06 6:15
NaNg1524112-May-06 6:15 
AnswerRe: object-access question Pin
Stefan Troschuetz12-May-06 6:32
Stefan Troschuetz12-May-06 6:32 
AnswerRe: object-access question Pin
spin vector12-May-06 6:40
spin vector12-May-06 6:40 
QuestionIE Component Pin
mehrdadc4812-May-06 5:43
mehrdadc4812-May-06 5:43 
AnswerRe: IE Component Pin
led mike12-May-06 5:51
led mike12-May-06 5:51 
Questionrecording sound stream Pin
Naveed Kamboh12-May-06 5:17
Naveed Kamboh12-May-06 5:17 
QuestionHow to add Virtual Printer (vs 2003) Pin
Rizwan Bashir12-May-06 5:05
Rizwan Bashir12-May-06 5:05 
Questioncombining subexpressions into 1 Regex expression Pin
theFrenchHornet12-May-06 5:04
theFrenchHornet12-May-06 5:04 
Questiona question about contextmenustrip ? Pin
cmpeng3412-May-06 4:56
cmpeng3412-May-06 4:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.