DES Engine : DES « Security « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Security » DESScreenshots 
DES Engine
 
/*******************************************************************************
 * Class:   : DESEngine
 * Author   : mbrinkley
 * Creation : Nov 11, 2003 2:59:23 PM
 * $Header: /cvsroot/jtds/jtds/src/main/net/sourceforge/jtds/util/DESEngine.java,v 1.3 2004/09/16 20:40:51 matt_brinkley Exp $
 *
 * Version: $Id: DESEngine.java,v 1.3 2004/09/16 20:40:51 matt_brinkley Exp $
 ******************************************************************************/


/*
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.params.KeyParameter;
*/

/**
 * a class that provides a basic DES engine.
 * Modified by Matt Brinkley (mdb) ... mainly just removed depends on external classes.
 *
 @version $Id: DESEngine.java,v 1.3 2004/09/16 20:40:51 matt_brinkley Exp $
 */
public class DESEngine
//    implements BlockCipher  //mdb
{
    protected static final int  BLOCK_SIZE = 8;

    private int[]               workingKey = null;

    /**
     * standard constructor.
     */
    public DESEngine()
    {
    }

    /**
     * mdb: convenient constructor
     */
    public DESEngineboolean encrypting, byte[] key )
    {
        initencrypting, key );
    }

    /**
     * initialise a DES cipher.
     *
     @param encrypting whether or not we are for encryption.
     @param key the parameters required to set up the cipher.
     @exception IllegalArgumentException if the params argument is
     * inappropriate.
     */
    public void init(
        boolean encrypting,
        byte[]  key//mdb: changed parameter from CipherParameters to byte[]
    {
        workingKey = generateWorkingKey(encrypting, key);

        //mdb: original:
        /*
        if (params instanceof KeyParameter)
        {
            workingKey = generateWorkingKey(encrypting,
                                  ((KeyParameter)params).getKey());

            return;
        }
        throw new IllegalArgumentException("invalid parameter passed to DES init - " + params.getClass().getName());
        */
    }

    public String getAlgorithmName()
    {
        return "DES";
    }

    public int getBlockSize()
    {
        return BLOCK_SIZE;
    }

    public int processBlock(
        byte[] in,
        int inOff,
        byte[] out,
        int outOff)
    {
        if (workingKey == null)
        {
            throw new IllegalStateException("DES engine not initialised");
        }

        if ((inOff + BLOCK_SIZE> in.length)
        {
            //mdb: used to be DataLengthException
            throw new IllegalArgumentException("input buffer too short");
        }

        if ((outOff + BLOCK_SIZE> out.length)
        {
            //mdb: used to be DataLengthException
            throw new IllegalArgumentException("output buffer too short");
        }

        desFunc(workingKey, in, inOff, out, outOff);

        return BLOCK_SIZE;
    }

    public void reset()
    {
    }

    /**
     * what follows is mainly taken from "Applied Cryptography", by
     * Bruce Schneier, however it also bears great resemblance to Richard
     * Outerbridge's D3DES...
     */

    static short[]    Df_Key =
        {
            0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
            0xfe,0xdc,0xba,0x98,0x76,0x54,0x32,0x10,
            0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
        };

    static short[]    bytebit =
        {
            02000100040020010040201
        };

    static int[]    bigbyte =
        {
            0x8000000x4000000x2000000x100000,
            0x80000,  0x40000,  0x20000,  0x10000,
            0x8000,      0x4000,   0x2000,   0x1000,
            0x800,    0x400,    0x200,    0x100,
            0x80,      0x40,        0x20,     0x10,
            0x8,      0x4,      0x2,      0x1
        };

    /*
     * Use the key schedule specified in the Standard (ANSI X3.92-1981).
     */

    static byte[]    pc1 =
        {
            564840322416,  8,   0574941332517,
             9,  15850423426,  1810,  259514335,
            62544638302214,   6615345372921,
            13,  56052443628,  2012,  4271911,  3
        };

    static byte[] totrot =
        {
            12468101214,
            1517192123252728
        };

    static byte[] pc2 =
        {
            13161023,  0,  4,  22714,  520,  9,
            221811,  325,  715,  6261912,  1,
            405130364654293950443247,
            434838553352454149352831
        };

    static int[] SP1 = {
        0x010104000x000000000x000100000x01010404,
        0x010100040x000104040x000000040x00010000,
        0x000004000x010104000x010104040x00000400,
        0x010004040x010100040x010000000x00000004,
        0x000004040x010004000x010004000x00010400,
        0x000104000x010100000x010100000x01000404,
        0x000100040x010000040x010000040x00010004,
        0x000000000x000004040x000104040x01000000,
        0x000100000x010104040x000000040x01010000,
        0x010104000x010000000x010000000x00000400,
        0x010100040x000100000x000104000x01000004,
        0x000004000x000000040x010004040x00010404,
        0x010104040x000100040x010100000x01000404,
        0x010000040x000004040x000104040x01010400,
        0x000004040x010004000x010004000x00000000,
        0x000100040x000104000x000000000x01010004
    };

    static int[] SP2 = {
        0x801080200x800080000x000080000x00108020,
        0x001000000x000000200x801000200x80008020,
        0x800000200x801080200x801080000x80000000,
        0x800080000x001000000x000000200x80100020,
        0x001080000x001000200x800080200x00000000,
        0x800000000x000080000x001080200x80100000,
        0x001000200x800000200x000000000x00108000,
        0x000080200x801080000x801000000x00008020,
        0x000000000x001080200x801000200x00100000,
        0x800080200x801000000x801080000x00008000,
        0x801000000x800080000x000000200x80108020,
        0x001080200x000000200x000080000x80000000,
        0x000080200x801080000x001000000x80000020,
        0x001000200x800080200x800000200x00100020,
        0x001080000x000000000x800080000x00008020,
        0x800000000x801000200x801080200x00108000
    };

    static int[] SP3 = {
        0x000002080x080202000x000000000x08020008,
        0x080002000x000000000x000202080x08000200,
        0x000200080x080000080x080000080x00020000,
        0x080202080x000200080x080200000x00000208,
        0x080000000x000000080x080202000x00000200,
        0x000202000x080200000x080200080x00020208,
        0x080002080x000202000x000200000x08000208,
        0x000000080x080202080x000002000x08000000,
        0x080202000x080000000x000200080x00000208,
        0x000200000x080202000x080002000x00000000,
        0x000002000x000200080x080202080x08000200,
        0x080000080x000002000x000000000x08020008,
        0x080002080x000200000x080000000x08020208,
        0x000000080x000202080x000202000x08000008,
        0x080200000x080002080x000002080x08020000,
        0x000202080x000000080x080200080x00020200
    };

    static int[] SP4 = {
        0x008020010x000020810x000020810x00000080,
        0x008020800x008000810x008000010x00002001,
        0x000000000x008020000x008020000x00802081,
        0x000000810x000000000x008000800x00800001,
        0x000000010x000020000x008000000x00802001,
        0x000000800x008000000x000020010x00002080,
        0x008000810x000000010x000020800x00800080,
        0x000020000x008020800x008020810x00000081,
        0x008000800x008000010x008020000x00802081,
        0x000000810x000000000x000000000x00802000,
        0x000020800x008000800x008000810x00000001,
        0x008020010x000020810x000020810x00000080,
        0x008020810x000000810x000000010x00002000,
        0x008000010x000020010x008020800x00800081,
        0x000020010x000020800x008000000x00802001,
        0x000000800x008000000x000020000x00802080
    };

    static int[] SP5 = {
        0x000001000x020801000x020800000x42000100,
        0x000800000x000001000x400000000x02080000,
        0x400801000x000800000x020001000x40080100,
        0x420001000x420800000x000801000x40000000,
        0x020000000x400800000x400800000x00000000,
        0x400001000x420801000x420801000x02000100,
        0x420800000x400001000x000000000x42000000,
        0x020801000x020000000x420000000x00080100,
        0x000800000x420001000x000001000x02000000,
        0x400000000x020800000x420001000x40080100,
        0x020001000x400000000x420800000x02080100,
        0x400801000x000001000x020000000x42080000,
        0x420801000x000801000x420000000x42080100,
        0x020800000x000000000x400800000x42000000,
        0x000801000x020001000x400001000x00080000,
        0x000000000x400800000x020801000x40000100
    };

    static int[] SP6 = {
        0x200000100x204000000x000040000x20404010,
        0x204000000x000000100x204040100x00400000,
        0x200040000x004040100x004000000x20000010,
        0x004000100x200040000x200000000x00004010,
        0x000000000x004000100x200040100x00004000,
        0x004040000x200040100x000000100x20400010,
        0x204000100x000000000x004040100x20404000,
        0x000040100x004040000x204040000x20000000,
        0x200040000x000000100x204000100x00404000,
        0x204040100x004000000x000040100x20000010,
        0x004000000x200040000x200000000x00004010,
        0x200000100x204040100x004040000x20400000,
        0x004040100x204040000x000000000x20400010,
        0x000000100x000040000x204000000x00404010,
        0x000040000x004000100x200040100x00000000,
        0x204040000x200000000x004000100x20004010
    };

    static int[] SP7 = {
        0x002000000x042000020x040008020x00000000,
        0x000008000x040008020x002008020x04200800,
        0x042008020x002000000x000000000x04000002,
        0x000000020x040000000x042000020x00000802,
        0x040008000x002008020x002000020x04000800,
        0x040000020x042000000x042008000x00200002,
        0x042000000x000008000x000008020x04200802,
        0x002008000x000000020x040000000x00200800,
        0x040000000x002008000x002000000x04000802,
        0x040008020x042000020x042000020x00000002,
        0x002000020x040000000x040008000x00200000,
        0x042008000x000008020x002008020x04200800,
        0x000008020x040000020x042008020x04200000,
        0x002008000x000000000x000000020x04200802,
        0x000000000x002008020x042000000x00000800,
        0x040000020x040008000x000008000x00200002
    };

    static int[] SP8 = {
        0x100010400x000010000x000400000x10041040,
        0x100000000x100010400x000000400x10000000,
        0x000400400x100400000x100410400x00041000,
        0x100410000x000410400x000010000x00000040,
        0x100400000x100000400x100010000x00001040,
        0x000410000x000400400x100400400x10041000,
        0x000010400x000000000x000000000x10040040,
        0x100000400x100010000x000410400x00040000,
        0x000410400x000400000x100410000x00001000,
        0x000000400x100400400x000010000x00041040,
        0x100010000x000000400x100000400x10040000,
        0x100400400x100000000x000400000x10001040,
        0x000000000x100410400x000400400x10000040,
        0x100400000x100010000x100010400x00000000,
        0x100410400x000410000x000410000x00001040,
        0x000010400x000400400x100000000x10041000
    };

    /**
     * generate an integer based working key based on our secret key
     * and what we processing we are planning to do.
     *
     * Acknowledgements for this routine go to James Gillogly & Phil Karn.
     *         (whoever, and wherever they are!).
     */
    protected int[] generateWorkingKey(
        boolean encrypting,
        byte[]  key)
    {
        int[]       newKey = new int[32];
        boolean[]   pc1m = new boolean[56],
                    pcr = new boolean[56];

        for (int j = 0; j < 56; j++ )
        {
            int    l = pc1[j];

            pc1m[j((key[l >>> 3& bytebit[l & 07]) != 0);
        }

        for (int i = 0; i < 16; i++)
        {
            int    l, m, n;

            if (encrypting)
            {
                m = i << 1;
            }
            else
            {
                m = (15 - i<< 1;
            }

            n = m + 1;
            newKey[m= newKey[n0;

            for (int j = 0; j < 28; j++)
            {
                l = j + totrot[i];
                if l < 28 )
                {
                    pcr[j= pc1m[l];
                }
                else
                {
                    pcr[j= pc1m[l - 28];
                }
            }

            for (int j = 28; j < 56; j++)
            {
                l = j + totrot[i];
                if (l < 56 )
                {
                    pcr[j= pc1m[l];
                }
                else
                {
                    pcr[j= pc1m[l - 28];
                }
            }

            for (int j = 0; j < 24; j++)
            {
                if (pcr[pc2[j]])
                {
                    newKey[m|= bigbyte[j];
                }

                if (pcr[pc2[j + 24]])
                {
                    newKey[n|= bigbyte[j];
                }
            }
        }

        //
        // store the processed key
        //
        for (int i = 0; i != 32; i += 2)
        {
            int    i1, i2;

            i1 = newKey[i];
            i2 = newKey[i + 1];

            newKey[i((i1 & 0x00fc0000<< 6((i1 & 0x00000fc0<< 10)
                                   ((i2 & 0x00fc0000>>> 10((i2 & 0x00000fc0>>> 6);

            newKey[i + 1((i1 & 0x0003f000<< 12((i1 & 0x0000003f<< 16)
                                   ((i2 & 0x0003f000>>> 4(i2 & 0x0000003f);
        }

        return newKey;
    }

    /**
     * the DES engine.
     */
    protected void desFunc(
        int[]   wKey,
        byte[]  in,
        int     inOff,
        byte[]  out,
        int     outOff)
    {
        int     work, right, left;

        left     = (in[inOff + 00xff<< 24;
        left    |= (in[inOff + 10xff<< 16;
        left    |= (in[inOff + 20xff<< 8;
        left    |= (in[inOff + 30xff);

        right     = (in[inOff + 40xff<< 24;
        right    |= (in[inOff + 50xff<< 16;
        right    |= (in[inOff + 60xff<< 8;
        right    |= (in[inOff + 70xff);

        work = ((left >>> 4^ right0x0f0f0f0f;
        right ^= work;
        left ^= (work << 4);
        work = ((left >>> 16^ right0x0000ffff;
        right ^= work;
        left ^= (work << 16);
        work = ((right >>> 2^ left0x33333333;
        left ^= work;
        right ^= (work << 2);
        work = ((right >>> 8^ left0x00ff00ff;
        left ^= work;
        right ^= (work << 8);
        right = ((right << 1((right >>> 311)) 0xffffffff;
        work = (left ^ right0xaaaaaaaa;
        left ^= work;
        right ^= work;
        left = ((left << 1((left >>> 311)) 0xffffffff;

        for (int round = 0; round < 8; round++)
        {
            int     fval;

            work  = (right << 28(right >>> 4);
            work ^= wKey[round * 0];
            fval  = SP7work      & 0x3f];
            fval |= SP5[(work >>>  80x3f];
            fval |= SP3[(work >>> 160x3f];
            fval |= SP1[(work >>> 240x3f];
            work  = right ^ wKey[round * 1];
            fval |= SP8work      & 0x3f];
            fval |= SP6[(work >>>  80x3f];
            fval |= SP4[(work >>> 160x3f];
            fval |= SP2[(work >>> 240x3f];
            left ^= fval;
            work  = (left << 28(left >>> 4);
            work ^= wKey[round * 2];
            fval  = SP7work      & 0x3f];
            fval |= SP5[(work >>>  80x3f];
            fval |= SP3[(work >>> 160x3f];
            fval |= SP1[(work >>> 240x3f];
            work  = left ^ wKey[round * 3];
            fval |= SP8work      & 0x3f];
            fval |= SP6[(work >>>  80x3f];
            fval |= SP4[(work >>> 160x3f];
            fval |= SP2[(work >>> 240x3f];
            right ^= fval;
        }

        right = (right << 31(right >>> 1);
        work = (left ^ right0xaaaaaaaa;
        left ^= work;
        right ^= work;
        left = (left << 31(left >>> 1);
        work = ((left >>> 8^ right0x00ff00ff;
        right ^= work;
        left ^= (work << 8);
        work = ((left >>> 2^ right0x33333333;
        right ^= work;
        left ^= (work << 2);
        work = ((right >>> 16^ left0x0000ffff;
        left ^= work;
        right ^= (work << 16);
        work = ((right >>> 4^ left0x0f0f0f0f;
        left ^= work;
        right ^= (work << 4);

        out[outOff + 0(byte)((right >>> 240xff);
        out[outOff + 1(byte)((right >>> 160xff);
        out[outOff + 2(byte)((right >>>  80xff);
        out[outOff + 3(byte)( right         & 0xff);
        out[outOff + 4(byte)((left >>> 240xff);
        out[outOff + 5(byte)((left >>> 160xff);
        out[outOff + 6(byte)((left >>>  80xff);
        out[outOff + 7(byte)( left         & 0xff);
    }
}

   
  
Related examples in the same category
1. DES Crypter and Decrypter
2. Decrypt an object with DES
3. Encrypt an object with DES
4. Encrypting a String with DES
5. Encrypting an Object with DES
6. Encrypting a File or Stream with DES
7. Triple DES
8. Encrypting with DES Using a Pass Phrase
9. DES algorithm
10. Des Encrypter
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.