StreamBlockCipher.cs :  » PDF » iTextSharp » Org » BouncyCastle » Crypto » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Crypto » StreamBlockCipher.cs
using System;

using Org.BouncyCastle.Crypto.Parameters;

namespace Org.BouncyCastle.Crypto{
  /**
   * a wrapper for block ciphers with a single byte block size, so that they
   * can be treated like stream ciphers.
   */
  public class StreamBlockCipher
    : IStreamCipher
  {
    private readonly IBlockCipher cipher;
    private readonly byte[] oneByte = new byte[1];

    /**
     * basic constructor.
     *
     * @param cipher the block cipher to be wrapped.
     * @exception ArgumentException if the cipher has a block size other than
     * one.
     */
    public StreamBlockCipher(
      IBlockCipher cipher)
    {
      if (cipher == null)
        throw new ArgumentNullException("cipher");
      if (cipher.GetBlockSize() != 1)
        throw new ArgumentException("block cipher block size != 1.", "cipher");

      this.cipher = cipher;
    }

    /**
     * initialise the underlying cipher.
     *
     * @param forEncryption true if we are setting up for encryption, false otherwise.
     * @param param the necessary parameters for the underlying cipher to be initialised.
     */
    public void Init(
      bool        forEncryption,
      ICipherParameters  parameters)
    {
      cipher.Init(forEncryption, parameters);
    }

    /**
    * return the name of the algorithm we are wrapping.
    *
    * @return the name of the algorithm we are wrapping.
    */
    public string AlgorithmName
    {
      get { return cipher.AlgorithmName; }
    }

    /**
    * encrypt/decrypt a single byte returning the result.
    *
    * @param in the byte to be processed.
    * @return the result of processing the input byte.
    */
    public byte ReturnByte(
      byte input)
    {
      oneByte[0] = input;

      cipher.ProcessBlock(oneByte, 0, oneByte, 0);

      return oneByte[0];
    }

    /**
    * process a block of bytes from in putting the result into out.
    *
    * @param in the input byte array.
    * @param inOff the offset into the in array where the data to be processed starts.
    * @param len the number of bytes to be processed.
    * @param out the output buffer the processed bytes go into.
    * @param outOff the offset into the output byte array the processed data stars at.
    * @exception DataLengthException if the output buffer is too small.
    */
    public void ProcessBytes(
      byte[]  input,
      int    inOff,
      int    length,
      byte[]  output,
      int    outOff)
    {
      if (outOff + length > output.Length)
        throw new DataLengthException("output buffer too small in ProcessBytes()");

      for (int i = 0; i != length; i++)
      {
        cipher.ProcessBlock(input, inOff + i, output, outOff + i);
      }
    }

    /**
    * reset the underlying cipher. This leaves it in the same state
    * it was at after the last init (if there was one).
    */
    public void Reset()
    {
      cipher.Reset();
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.