VMPCEngine.cs :  » PDF » iTextSharp » Org » BouncyCastle » Crypto » Engines » 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 » Engines » VMPCEngine.cs
using System;

using Org.BouncyCastle.Crypto.Parameters;

namespace Org.BouncyCastle.Crypto.Engines{
  public class VmpcEngine
    : IStreamCipher
  {
    /*
    * variables to hold the state of the VMPC engine during encryption and
    * decryption
    */
    protected byte n = 0;
    protected byte[] P = null;
    protected byte s = 0;

    protected byte[] workingIV;
    protected byte[] workingKey;

    public virtual string AlgorithmName
    {
      get { return "VMPC"; }
    }

    /**
    * initialise a VMPC cipher.
    * 
    * @param forEncryption
    *    whether or not we are for encryption.
    * @param params
    *    the parameters required to set up the cipher.
    * @exception ArgumentException
    *    if the params argument is inappropriate.
    */
    public virtual void Init(
      bool        forEncryption,
      ICipherParameters  parameters)
    {
      if (!(parameters is ParametersWithIV))
        throw new ArgumentException("VMPC Init parameters must include an IV");

      ParametersWithIV ivParams = (ParametersWithIV) parameters;
      KeyParameter key = (KeyParameter) ivParams.Parameters;

      if (!(ivParams.Parameters is KeyParameter))
        throw new ArgumentException("VMPC Init parameters must include a key");

      this.workingIV = ivParams.GetIV();

      if (workingIV == null || workingIV.Length < 1 || workingIV.Length > 768)
        throw new ArgumentException("VMPC requires 1 to 768 bytes of IV");

      this.workingKey = key.GetKey();

      InitKey(this.workingKey, this.workingIV);
    }

    protected virtual void InitKey(
      byte[]  keyBytes,
      byte[]  ivBytes)
    {
      s = 0;
      P = new byte[256];
      for (int i = 0; i < 256; i++)
      {
        P[i] = (byte) i;
      }

      for (int m = 0; m < 768; m++)
      {
        s = P[(s + P[m & 0xff] + keyBytes[m % keyBytes.Length]) & 0xff];
        byte temp = P[m & 0xff];
        P[m & 0xff] = P[s & 0xff];
        P[s & 0xff] = temp;
      }
      for (int m = 0; m < 768; m++)
      {
        s = P[(s + P[m & 0xff] + ivBytes[m % ivBytes.Length]) & 0xff];
        byte temp = P[m & 0xff];
        P[m & 0xff] = P[s & 0xff];
        P[s & 0xff] = temp;
      }
      n = 0;
    }

    public virtual void ProcessBytes(
      byte[]  input,
      int    inOff,
      int    len,
      byte[]  output,
      int    outOff)
    {
      if ((inOff + len) > input.Length)
      {
        throw new DataLengthException("input buffer too short");
      }

      if ((outOff + len) > output.Length)
      {
        throw new DataLengthException("output buffer too short");
      }

      for (int i = 0; i < len; i++)
      {
        s = P[(s + P[n & 0xff]) & 0xff];
        byte z = P[(P[(P[s & 0xff]) & 0xff] + 1) & 0xff];
        // encryption
        byte temp = P[n & 0xff];
        P[n & 0xff] = P[s & 0xff];
        P[s & 0xff] = temp;
        n = (byte) ((n + 1) & 0xff);

        // xor
        output[i + outOff] = (byte) (input[i + inOff] ^ z);
      }
    }

    public virtual void Reset()
    {
      InitKey(this.workingKey, this.workingIV);
    }

    public virtual byte ReturnByte(
      byte input)
    {
      s = P[(s + P[n & 0xff]) & 0xff];
      byte z = P[(P[(P[s & 0xff]) & 0xff] + 1) & 0xff];
      // encryption
      byte temp = P[n & 0xff];
      P[n & 0xff] = P[s & 0xff];
      P[s & 0xff] = temp;
      n = (byte) ((n + 1) & 0xff);

      // xor
      return (byte) (input ^ z);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.