Unix Crypt 2 : Unix « Security « Java

Home
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 » Security » UnixScreenshots 
Unix Crypt 2
     

//package com.peterfranza.svnadmin.server.acldb.utils;

public class UnixCrypt {

  //
  // Null constructor - can't instantiate class
  private UnixCrypt() {
  }

  private static final char[] saltChars = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
      .toCharArray());

  private static final int ITERATIONS = 16;

  private static final int con_salt[] 0x000x000x000x000x000x00,
      0x000x000x000x000x000x000x000x000x000x000x00,
      0x000x000x000x000x000x000x000x000x000x000x00,
      0x000x000x000x000x000x000x000x000x000x000x00,
      0x000x000x000x000x000x000x000x000x010x020x03,
      0x040x050x060x070x080x090x0A0x0B0x050x060x07,
      0x080x090x0A0x0B0x0C0x0D0x0E0x0F0x100x110x12,
      0x130x140x150x160x170x180x190x1A0x1B0x1C0x1D,
      0x1E0x1F0x200x210x220x230x240x250x200x210x22,
      0x230x240x250x260x270x280x290x2A0x2B0x2C0x2D,
      0x2E0x2F0x300x310x320x330x340x350x360x370x38,
      0x390x3A0x3B0x3C0x3D0x3E0x3F0x000x000x000x00,
      0x00};

  private static final boolean shifts2[] false, false, true, true, true,
      true, true, true, false, true, true, true, true, true, true, false };

  private static final int skb[][] {
      {
      /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
      0x000000000x000000100x200000000x200000100x00010000,
          0x000100100x200100000x200100100x000008000x00000810,
          0x200008000x200008100x000108000x000108100x20010800,
          0x200108100x000000200x000000300x200000200x20000030,
          0x000100200x000100300x200100200x200100300x00000820,
          0x000008300x200008200x200008300x000108200x00010830,
          0x200108200x200108300x000800000x000800100x20080000,
          0x200800100x000900000x000900100x200900000x20090010,
          0x000808000x000808100x200808000x200808100x00090800,
          0x000908100x200908000x200908100x000800200x00080030,
          0x200800200x200800300x000900200x000900300x20090020,
          0x200900300x000808200x000808300x200808200x20080830,
          0x000908200x000908300x200908200x20090830},
      {
      /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */
      0x000000000x020000000x000020000x020020000x00200000,
          0x022000000x002020000x022020000x000000040x02000004,
          0x000020040x020020040x002000040x022000040x00202004,
          0x022020040x000004000x020004000x000024000x02002400,
          0x002004000x022004000x002024000x022024000x00000404,
          0x020004040x000024040x020024040x002004040x02200404,
          0x002024040x022024040x100000000x120000000x10002000,
          0x120020000x102000000x122000000x102020000x12202000,
          0x100000040x120000040x100020040x120020040x10200004,
          0x122000040x102020040x122020040x100004000x12000400,
          0x100024000x120024000x102004000x122004000x10202400,
          0x122024000x100004040x120004040x100024040x12002404,
          0x102004040x122004040x102024040x12202404},
      {
      /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */
      0x000000000x000000010x000400000x000400010x01000000,
          0x010000010x010400000x010400010x000000020x00000003,
          0x000400020x000400030x010000020x010000030x01040002,
          0x010400030x000002000x000002010x000402000x00040201,
          0x010002000x010002010x010402000x010402010x00000202,
          0x000002030x000402020x000402030x010002020x01000203,
          0x010402020x010402030x080000000x080000010x08040000,
          0x080400010x090000000x090000010x090400000x09040001,
          0x080000020x080000030x080400020x080400030x09000002,
          0x090000030x090400020x090400030x080002000x08000201,
          0x080402000x080402010x090002000x090002010x09040200,
          0x090402010x080002020x080002030x080402020x08040203,
          0x090002020x090002030x090402020x09040203},
      {
      /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */
      0x000000000x001000000x000001000x001001000x00000008,
          0x001000080x000001080x001001080x000010000x00101000,
          0x000011000x001011000x000010080x001010080x00001108,
          0x001011080x040000000x041000000x040001000x04100100,
          0x040000080x041000080x040001080x041001080x04001000,
          0x041010000x040011000x041011000x040010080x04101008,
          0x040011080x041011080x000200000x001200000x00020100,
          0x001201000x000200080x001200080x000201080x00120108,
          0x000210000x001210000x000211000x001211000x00021008,
          0x001210080x000211080x001211080x040200000x04120000,
          0x040201000x041201000x040200080x041200080x04020108,
          0x041201080x040210000x041210000x040211000x04121100,
          0x040210080x041210080x040211080x04121108},
      {
      /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
      0x000000000x100000000x000100000x100100000x00000004,
          0x100000040x000100040x100100040x200000000x30000000,
          0x200100000x300100000x200000040x300000040x20010004,
          0x300100040x001000000x101000000x001100000x10110000,
          0x001000040x101000040x001100040x101100040x20100000,
          0x301000000x201100000x301100000x201000040x30100004,
          0x201100040x301100040x000010000x100010000x00011000,
          0x100110000x000010040x100010040x000110040x10011004,
          0x200010000x300010000x200110000x300110000x20001004,
          0x300010040x200110040x300110040x001010000x10101000,
          0x001110000x101110000x001010040x101010040x00111004,
          0x101110040x201010000x301010000x201110000x30111000,
          0x201010040x301010040x201110040x30111004},
      {
      /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */
      0x000000000x080000000x000000080x080000080x00000400,
          0x080004000x000004080x080004080x000200000x08020000,
          0x000200080x080200080x000204000x080204000x00020408,
          0x080204080x000000010x080000010x000000090x08000009,
          0x000004010x080004010x000004090x080004090x00020001,
          0x080200010x000200090x080200090x000204010x08020401,
          0x000204090x080204090x020000000x0A0000000x02000008,
          0x0A0000080x020004000x0A0004000x020004080x0A000408,
          0x020200000x0A0200000x020200080x0A0200080x02020400,
          0x0A0204000x020204080x0A0204080x020000010x0A000001,
          0x020000090x0A0000090x020004010x0A0004010x02000409,
          0x0A0004090x020200010x0A0200010x020200090x0A020009,
          0x020204010x0A0204010x020204090x0A020409},
      {
      /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */
      0x000000000x000001000x000800000x000801000x01000000,
          0x010001000x010800000x010801000x000000100x00000110,
          0x000800100x000801100x010000100x010001100x01080010,
          0x010801100x002000000x002001000x002800000x00280100,
          0x012000000x012001000x012800000x012801000x00200010,
          0x002001100x002800100x002801100x012000100x01200110,
          0x012800100x012801100x000002000x000003000x00080200,
          0x000803000x010002000x010003000x010802000x01080300,
          0x000002100x000003100x000802100x000803100x01000210,
          0x010003100x010802100x010803100x002002000x00200300,
          0x002802000x002803000x012002000x012003000x01280200,
          0x012803000x002002100x002003100x002802100x00280310,
          0x012002100x012003100x012802100x01280310},
      {
      /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
      0x000000000x040000000x000400000x040400000x00000002,
          0x040000020x000400020x040400020x000020000x04002000,
          0x000420000x040420000x000020020x040020020x00042002,
          0x040420020x000000200x040000200x000400200x04040020,
          0x000000220x040000220x000400220x040400220x00002020,
          0x040020200x000420200x040420200x000020220x04002022,
          0x000420220x040420220x000008000x040008000x00040800,
          0x040408000x000008020x040008020x000408020x04040802,
          0x000028000x040028000x000428000x040428000x00002802,
          0x040028020x000428020x040428020x000008200x04000820,
          0x000408200x040408200x000008220x040008220x00040822,
          0x040408220x000028200x040028200x000428200x04042820,
          0x000028220x040028220x000428220x04042822}};

  private static final int SPtrans[][] {
      {
      /* nibble 0 */
      0x008202000x000200000x808000000x808202000x00800000,
          0x800202000x800200000x808000000x800202000x00820200,
          0x008200000x800002000x808002000x008000000x00000000,
          0x800200000x000200000x800000000x008002000x00020200,
          0x808202000x008200000x800002000x008002000x80000000,
          0x000002000x000202000x808200000x000002000x80800200,
          0x808200000x000000000x000000000x808202000x00800200,
          0x800200000x008202000x000200000x800002000x00800200,
          0x808200000x000002000x000202000x808000000x80020200,
          0x800000000x808000000x008200000x808202000x00020200,
          0x008200000x808002000x008000000x800002000x80020000,
          0x000000000x000200000x008000000x808002000x00820200,
          0x800000000x808200000x000002000x80020200},
      {
      /* nibble 1 */
      0x100420040x000000000x000420000x100400000x10000004,
          0x000020040x100020000x000420000x000020000x10040004,
          0x000000040x100020000x000400040x100420000x10040000,
          0x000000040x000400000x100020040x100400040x00002000,
          0x000420040x100000000x000000000x000400040x10002004,
          0x000420040x100420000x100000040x100000000x00040000,
          0x000020040x100420040x000400040x100420000x10002000,
          0x000420040x100420040x000400040x100000040x00000000,
          0x100000000x000020040x000400000x100400040x00002000,
          0x100000000x000420040x100020040x100420000x00002000,
          0x000000000x100000040x000000040x100420040x00042000,
          0x100400000x100400040x000400000x000020040x10002000,
          0x100020040x000000040x100400000x00042000},
      {
      /* nibble 2 */
      0x410000000x010100400x000000400x410000400x40010000,
          0x010000000x410000400x000100400x010000400x00010000,
          0x010100000x400000000x410100400x400000400x40000000,
          0x410100000x000000000x400100000x010100400x00000040,
          0x400000400x410100400x000100000x410000000x41010000,
          0x010000400x400100400x010100000x000100400x00000000,
          0x010000000x400100400x010100400x000000400x40000000,
          0x000100000x400000400x400100000x010100000x41000040,
          0x000000000x010100400x000100400x410100000x40010000,
          0x010000000x410100400x400000000x400100400x41000000,
          0x010000000x410100400x000100000x010000400x41000040,
          0x000100400x010000400x000000000x410100000x40000040,
          0x410000000x400100400x000000400x01010000},
      {
      /* nibble 3 */
      0x001004020x040004000x000000020x041004020x00000000,
          0x041000000x040004020x001000020x041004000x04000002,
          0x040000000x000004020x040000020x001004020x00100000,
          0x040000000x041000020x001004000x000004000x00000002,
          0x001004000x040004020x041000000x000004000x00000402,
          0x000000000x001000020x041004000x040004000x04100002,
          0x041004020x001000000x041000020x000004020x00100000,
          0x040000020x001004000x040004000x000000020x04100000,
          0x040004020x000000000x000004000x001000020x00000000,
          0x041000020x041004000x000004000x040000000x04100402,
          0x001004020x001000000x041004020x000000020x04000400,
          0x001004020x001000020x001004000x041000000x04000402,
          0x000004020x040000000x040000020x04100400},
      {
      /* nibble 4 */
      0x020000000x000040000x000001000x020041080x02004008,
          0x020001000x000041080x020040000x000040000x00000008,
          0x020000080x000041000x020001080x020040080x02004100,
          0x000000000x000041000x020000000x000040080x00000108,
          0x020001000x000041080x000000000x020000080x00000008,
          0x020001080x020041080x000040080x020040000x00000100,
          0x000001080x020041000x020041000x020001080x00004008,
          0x020040000x000040000x000000080x020000080x02000100,
          0x020000000x000041000x020041080x000000000x00004108,
          0x020000000x000001000x000040080x020001080x00000100,
          0x000000000x020041080x020040080x020041000x00000108,
          0x000040000x000041000x020040080x020001000x00000108,
          0x000000080x000041080x020040000x02000008},
      {
      /* nibble 5 */
      0x200000100x000800100x000000000x200808000x00080010,
          0x000008000x200008100x000800000x000008100x20080810,
          0x000808000x200000000x200008000x200000100x20080000,
          0x000808100x000800000x200008100x200800100x00000000,
          0x000008000x000000100x200808000x200800100x20080810,
          0x200800000x200000000x000008100x000000100x00080800,
          0x000808100x200008000x000008100x200000000x20000800,
          0x000808100x200808000x000800100x000000000x20000800,
          0x200000000x000008000x200800100x000800000x00080010,
          0x200808100x000808000x000000100x200808100x00080800,
          0x000800000x200008100x200000100x200800000x00080810,
          0x000000000x000008000x200000100x200008100x20080800,
          0x200800000x000008100x000000100x20080010},
      {
      /* nibble 6 */
      0x000010000x000000800x004000800x004000010x00401081,
          0x000010010x000010800x000000000x004000000x00400081,
          0x000000810x004010000x000000010x004010800x00401000,
          0x000000810x004000810x000010000x000010010x00401081,
          0x000000000x004000800x004000010x000010800x00401001,
          0x000010810x004010800x000000010x000010810x00401001,
          0x000000800x004000000x000010810x004010000x00401001,
          0x000000810x000010000x000000800x004000000x00401001,
          0x004000810x000010810x000010800x000000000x00000080,
          0x004000010x000000010x004000800x000000000x00400081,
          0x004000800x000010800x000000810x000010000x00401081,
          0x004000000x004010800x000000010x000010010x00401081,
          0x004000010x004010800x004010000x00001001},
      {
      /* nibble 7 */
      0x082000200x082080000x000080200x000000000x08008000,
          0x002000200x082000000x082080200x000000200x08000000,
          0x002080000x000080200x002080200x080080200x08000020,
          0x082000000x000080000x002080200x002000200x08008000,
          0x082080200x080000200x000000000x002080000x08000000,
          0x002000000x080080200x082000200x002000000x00008000,
          0x082080000x000000200x002000000x000080000x08000020,
          0x082080200x000080200x080000000x000000000x00208000,
          0x082000200x080080200x080080000x002000200x08208000,
          0x000000200x002000200x080080000x082080200x00200000,
          0x082000000x080000200x002080000x000080200x08008020,
          0x082000000x000000200x082080000x002080200x00000000,
          0x080000000x082000200x000080000x00208020 } };

  private static final int cov_2char[] 0x2E0x2F0x300x310x32,
      0x330x340x350x360x370x380x390x410x420x430x44,
      0x450x460x470x480x490x4A0x4B0x4C0x4D0x4E0x4F,
      0x500x510x520x530x540x550x560x570x580x590x5A,
      0x610x620x630x640x650x660x670x680x690x6A0x6B,
      0x6C0x6D0x6E0x6F0x700x710x720x730x740x750x76,
      0x770x780x790x7A };

  private static final int byteToUnsigned(byte b) {
    int value = (intb;

    return (value >= ? value : value + 256);
  }

  private static int fourBytesToInt(byte b[]int offset) {
    int value;

    value = byteToUnsigned(b[offset++]);
    value |= (byteToUnsigned(b[offset++]) << 8);
    value |= (byteToUnsigned(b[offset++]) << 16);
    value |= (byteToUnsigned(b[offset++]) << 24);

    return (value);
  }

  private static final void intToFourBytes(int iValue, byte b[]int offset) {
    b[offset++(byte) ((iValue0xff);
    b[offset++(byte) ((iValue >>> 80xff);
    b[offset++(byte) ((iValue >>> 160xff);
    b[offset++(byte) ((iValue >>> 240xff);
  }

  private static final void PERM_OP(int a, int b, int n, int m, int results[]) {
    int t;

    t = ((a >>> n^ b& m;
    a ^= t << n;
    b ^= t;

    results[0= a;
    results[1= b;
  }

  private static final int HPERM_OP(int a, int n, int m) {
    int t;

    t = ((a << (16 - n)) ^ a& m;
    a = a ^ t ^ (t >>> (16 - n));

    return (a);
  }

  private static int[] des_set_key(byte key[]) {
    int schedule[] new int[ITERATIONS * 2];

    int c = fourBytesToInt(key, 0);
    int d = fourBytesToInt(key, 4);

    int results[] new int[2];

    PERM_OP(d, c, 40x0f0f0f0f, results);
    d = results[0];
    c = results[1];

    c = HPERM_OP(c, -20xcccc0000);
    d = HPERM_OP(d, -20xcccc0000);

    PERM_OP(d, c, 10x55555555, results);
    d = results[0];
    c = results[1];

    PERM_OP(c, d, 80x00ff00ff, results);
    c = results[0];
    d = results[1];

    PERM_OP(d, c, 10x55555555, results);
    d = results[0];
    c = results[1];

    d = (((d & 0x000000ff<< 16(d & 0x0000ff00)
        ((d & 0x00ff0000>>> 16((c & 0xf0000000>>> 4));
    c &= 0x0fffffff;

    int s, t;
    int j = 0;

    for (int i = 0; i < ITERATIONS; i++) {
      if (shifts2[i]) {
        c = (c >>> 2(c << 26);
        d = (d >>> 2(d << 26);
      else {
        c = (c >>> 1(c << 27);
        d = (d >>> 1(d << 27);
      }

      c &= 0x0fffffff;
      d &= 0x0fffffff;

      s = skb[0][(c0x3f]
          | skb[1][((c >>> 60x03((c >>> 70x3c)]
          | skb[2][((c >>> 130x0f((c >>> 140x30)]
          | skb[3][((c >>> 200x01((c >>> 210x06)
              ((c >>> 220x38)];

      t = skb[4][(d0x3f]
          | skb[5][((d >>> 70x03((d >>> 80x3c)]
          | skb[6][(d >>> 150x3f]
          | skb[7][((d >>> 210x0f((d >>> 220x30)];

      schedule[j++((t << 16(s & 0x0000ffff)) 0xffffffff;
      s = ((s >>> 16(t & 0xffff0000));

      s = (s << 4(s >>> 28);
      schedule[j++= s & 0xffffffff;
    }
    return (schedule);
  }

  private static final int D_ENCRYPT(int L, int R, int S, int E0, int E1,
      int s[]) {
    int t, u, v;

    v = R ^ (R >>> 16);
    u = v & E0;
    v = v & E1;
    u = (u ^ (u << 16)) ^ R ^ s[S];
    t = (v ^ (v << 16)) ^ R ^ s[S + 1];
    t = (t >>> 4(t << 28);

    L ^= SPtrans[1][(t0x3f| SPtrans[3][(t >>> 80x3f]
        | SPtrans[5][(t >>> 160x3f| SPtrans[7][(t >>> 240x3f]
        | SPtrans[0][(u0x3f| SPtrans[2][(u >>> 80x3f]
        | SPtrans[4][(u >>> 160x3f| SPtrans[6][(u >>> 240x3f];

    return (L);
  }

  private static final int[] body(int schedule[]int Eswap0, int Eswap1) {
    int left = 0;
    int right = 0;
    int t = 0;

    for (int j = 0; j < 25; j++) {
      for (int i = 0; i < ITERATIONS * 2; i += 4) {
        left = D_ENCRYPT(left, right, i, Eswap0, Eswap1, schedule);
        right = D_ENCRYPT(right, left, i + 2, Eswap0, Eswap1, schedule);
      }
      t = left;
      left = right;
      right = t;
    }

    t = right;

    right = (left >>> 1(left << 31);
    left = (t >>> 1(t << 31);

    left &= 0xffffffff;
    right &= 0xffffffff;

    int results[] new int[2];

    PERM_OP(right, left, 10x55555555, results);
    right = results[0];
    left = results[1];

    PERM_OP(left, right, 80x00ff00ff, results);
    left = results[0];
    right = results[1];

    PERM_OP(right, left, 20x33333333, results);
    right = results[0];
    left = results[1];

    PERM_OP(left, right, 160x0000ffff, results);
    left = results[0];
    right = results[1];

    PERM_OP(right, left, 40x0f0f0f0f, results);
    right = results[0];
    left = results[1];

    int out[] new int[2];

    out[0= left;
    out[1= right;

    return (out);
  }

  /**
   * <P>
   * Encrypt a password given the cleartext password and a "salt".
   * </P>
   
   @param salt
   *            A two-character string representing the salt used to iterate
   *            the encryption engine in lots of different ways. If you are
   *            generating a new encryption then this value should be
   *            randomised.
   @param original
   *            The password to be encrypted.
   @return A string consisting of the 2-character salt followed by the
   *         encrypted password.
   */
  public static final String crypt(String salt, String original) {
    while (salt.length() 2)
      salt += "A";

    StringBuffer buffer = new StringBuffer("             ");

    char charZero = salt.charAt(0);
    char charOne = salt.charAt(1);

    buffer.setCharAt(0, charZero);
    buffer.setCharAt(1, charOne);

    int Eswap0 = con_salt[(intcharZero];
    int Eswap1 = con_salt[(intcharOne<< 4;

    byte key[] new byte[8];

    for (int i = 0; i < key.length; i++)
      key[i(byte0;

    for (int i = 0; i < key.length && i < original.length(); i++) {
      int iChar = (intoriginal.charAt(i);

      key[i(byte) (iChar << 1);
    }

    int schedule[] = des_set_key(key);
    int out[] body(schedule, Eswap0, Eswap1);

    byte b[] new byte[9];

    intToFourBytes(out[0], b, 0);
    intToFourBytes(out[1], b, 4);
    b[80;

    for (int i = 2, y = 0, u = 0x80; i < 13; i++) {
      for (int j = 0, c = 0; j < 6; j++) {
        c <<= 1;

        if (((intb[y& u!= 0)
          c |= 1;

        u >>>= 1;

        if (u == 0) {
          y++;
          u = 0x80;
        }
        buffer.setCharAt(i, (charcov_2char[c]);
      }
    }
    return (buffer.toString());
  }

  /**
   * <P>
   * Encrypt a password given the cleartext password. This method generates a
   * random salt using the 'java.util.Random' class.
   * </P>
   
   @param original
   *            The password to be encrypted.
   @return A string consisting of the 2-character salt followed by the
   *         encrypted password.
   */
  public static final String crypt(String original) {
    java.util.Random randomGenerator = new java.util.Random();
    int numSaltChars = saltChars.length;
    String salt;

    salt = (new StringBuffer())
        .append(saltChars[Math.abs(randomGenerator.nextInt())
            % numSaltChars])
        .append(saltChars[Math.abs(randomGenerator.nextInt())
            % numSaltChars]).toString();

    return crypt(salt, original);
  }

  /**
   * <P>
   * Check that <I>enteredPassword</I> encrypts to <I>encryptedPassword</I>.
   * </P>
   
   @param encryptedPassword
   *            The <I>encryptedPassword</I>. The first two characters are
   *            assumed to be the salt. This string would be the same as one
   *            found in a Unix <U>/etc/passwd</U> file.
   @param enteredPassword
   *            The password as entered by the user (or otherwise aquired).
   @return <B>true</B> if the password should be considered correct.
   */
  public final static boolean matches(String encryptedPassword,
      String enteredPassword) {
    String salt = encryptedPassword.substring(03);
    String newCrypt = crypt(salt, enteredPassword);

    return newCrypt.equals(encryptedPassword);
  }
}

   
    
    
    
    
  
Related examples in the same category
1.Unix Crypt
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.