Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A CBC Stream Cipher in C# (With wrappers for two open source AES implementations in C# and C)

, 4 Jul 2004
An article on .NET cryptography
//
using System;

public class GMul
{

	public delegate byte gfmuldel(byte b);
	
	public static void Main()
	{
		Console.WriteLine("#region gfmultby\r\n");
		
		// not needed
		//GenFunction("gfmultby01", new gfmuldel(gfmultby01));
		
		GenFunction("gfmultby02", new gfmuldel(gfmultby02));
		GenFunction("gfmultby03", new gfmuldel(gfmultby03));
		
		GenFunction("gfmultby09", new gfmuldel(gfmultby09));
		GenFunction("gfmultby0b", new gfmuldel(gfmultby0b));
		GenFunction("gfmultby0d", new gfmuldel(gfmultby0d));
		GenFunction("gfmultby0e", new gfmuldel(gfmultby0e));
		
		Console.WriteLine("#endregion gfmultby");
	}

    
    
    private static void GenFunction(string name, gfmuldel f)
    {
    	Console.Write("private static readonly byte[] " + name + " = new byte[256] {\r\n\t");
    	for(int b = 0; b < 256; b++)
    	{
    		byte r = f((byte)b);
    		string rr = r.ToString("x");
    		Console.Write("0x" + (rr.Length < 2 ? "0" : string.Empty) + rr);
    		if(b != 255) Console.Write(", ");
    		if(((b + 1) % 16 == 0)) Console.Write("\r\n\t");
    	}
    	Console.Write("};\r\n\r\n");
    }
    
    private static byte gfmultby01(byte b)
    {
          return b;
    }
    
    private static byte gfmultby02(byte b)
    {
      if (b < 0x80)
        return (byte)(int)(b << 1);
      else
        return (byte)( (int)(b << 1) ^ (int)(0x1b) );
    }

    private static byte gfmultby03(byte b)
    {
      return (byte) ( (int)gfmultby02(b) ^ (int)b );
    }

    private static byte gfmultby09(byte b)
    {
      return (byte)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
                     (int)b );
    }

    private static byte gfmultby0b(byte b)
    {
      return (byte)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
                     (int)gfmultby02(b) ^
                     (int)b );
    }

    private static byte gfmultby0d(byte b)
    {
      return (byte)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
                     (int)gfmultby02(gfmultby02(b)) ^
                     (int)(b) );
    }

    private static byte gfmultby0e(byte b)
    {
      return (byte)( (int)gfmultby02(gfmultby02(gfmultby02(b))) ^
                     (int)gfmultby02(gfmultby02(b)) ^
                     (int)gfmultby02(b) );
    }

}//EOC

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

vcepa

Germany Germany
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150123.1 | Last Updated 5 Jul 2004
Article Copyright 2004 by vcepa
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid