Unix Crypt : 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.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Security » Unix 




Unix Crypt
     
/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 */
//package org.beangle.security.codec;

import java.util.Random;

public class UnixCrypt {

  private UnixCrypt() {
  }

  private static final int byteToUnsigned(byte b) {
    int value = b;
    return value < ? value + 256 : value;
  }

  private static int fourBytesToInt(byte b[]int offset) {
    int 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) (iValue & 0xff);
    b[offset++(byte) (iValue >>> 0xff);
    b[offset++(byte) (iValue >>> 16 0xff);
    b[offset++(byte) (iValue >>> 24 0xff);
  }

  private static final void PERM_OP(int a, int b, int n, int m, int results[]) {
    int 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 = (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[32];
    int c = fourBytesToInt(key, 0);
    int d = fourBytesToInt(key, 4);
    int results[] new int[2];
    PERM_OP(d, c, 40xf0f0f0f, 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, 80xff00ff, results);
    c = results[0];
    d = results[1];
    PERM_OP(d, c, 10x55555555, results);
    d = results[0];
    c = results[1];
    d = (d & 0xff<< 16 | d & 0xff00 (d & 0xff0000>>> 16
        (c & 0xf0000000>>> 4;
    c &= 0xfffffff;
    int j = 0;
    for (int i = 0; i < 16; i++) {
      if (shifts2[i]) {
        c = c >>> | c << 26;
        d = d >>> | d << 26;
      else {
        c = c >>> | c << 27;
        d = d >>> | d << 27;
      }
      c &= 0xfffffff;
      d &= 0xfffffff;
      int s = skb[0][c & 0x3f| skb[1][c >>> | c >>> 0x3c]
          | skb[2][c >>> 13 0xf | c >>> 14 0x30]
          | skb[3][c >>> 20 | c >>> 21 | c >>> 22 0x38];
      int t = skb[4][d & 0x3f| skb[5][d >>> | d >>> 0x3c]
          | skb[6][d >>> 15 0x3f]
          | skb[7][d >>> 21 0xf | d >>> 22 0x30];
      schedule[j++(t << 16 | s & 0xffff& -1;
      s = s >>> 16 | t & 0xffff0000;
      s = s << | s >>> 28;
      schedule[j++= s & -1;
    }

    return schedule;
  }

  private static final int D_ENCRYPT(int L, int R, int S, int E0, int E1,
      int s[]) {
    int v = R ^ R >>> 16;
    int u = v & E0;
    v &= E1;
    u = u ^ u << 16 ^ R ^ s[S];
    int t = v ^ v << 16 ^ R ^ s[S + 1];
    t = t >>> | t << 28;
    L ^= SPtrans[1][t & 0x3f| SPtrans[3][t >>> 0x3f]
        | SPtrans[5][t >>> 16 0x3f| SPtrans[7][t >>> 24 0x3f]
        | SPtrans[0][u & 0x3f| SPtrans[2][u >>> 0x3f]
        | SPtrans[4][u >>> 16 0x3f| SPtrans[6][u >>> 24 0x3f];
    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 < 32; 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 >>> | left << 31;
    left = t >>> | t << 31;
    left &= -1;
    right &= -1;
    int results[] new int[2];
    PERM_OP(right, left, 10x55555555, results);
    right = results[0];
    left = results[1];
    PERM_OP(left, right, 80xff00ff, results);
    left = results[0];
    right = results[1];
    PERM_OP(right, left, 20x33333333, results);
    right = results[0];
    left = results[1];
    PERM_OP(left, right, 1665535, results);
    left = results[0];
    right = results[1];
    PERM_OP(right, left, 40xf0f0f0f, results);
    right = results[0];
    left = results[1];
    int out[] new int[2];
    out[0= left;
    out[1= right;
    return out;
  }

  public static final String crypt(String salt, String original) {
    for (; salt.length() 2; salt = salt + "A")
      ;
    StringBuilder buffer = new StringBuilder("             ");
    char charZero = salt.charAt(0);
    char charOne = salt.charAt(1);
    buffer.setCharAt(0, charZero);
    buffer.setCharAt(1, charOne);
    int Eswap0 = con_salt[charZero];
    int Eswap1 = con_salt[charOne<< 4;
    byte key[] new byte[8];
    for (int i = 0; i < key.length; i++)
      key[i0;

    for (int i = 0; i < key.length && i < original.length(); i++) {
      int iChar = original.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;
    int i = 2;
    int y = 0;
    int u = 128;
    for (; i < 13; i++) {
      int j = 0;
      int c = 0;
      for (; j < 6; j++) {
        c <<= 1;
        if ((b[y& u!= 0)
          c |= 1;
        u >>>= 1;
        if (u == 0) {
          y++;
          u = 128;
        }
        buffer.setCharAt(i, (charcov_2char[c]);
      }

    }

    return buffer.toString();
  }

  public static final String crypt(String original) {
    Random randomGenerator = new Random();
    int numSaltChars = saltChars.length;
    String salt = ""
        + saltChars[Math.abs(randomGenerator.nextInt()) % numSaltChars]
        + saltChars[Math.abs(randomGenerator.nextInt()) % numSaltChars];
    return crypt(salt, original);
  }

  public static final boolean matches(String encryptedPassword,
      String enteredPassword) {
    String salt = encryptedPassword.substring(03);
    String newCrypt = crypt(salt, enteredPassword);
    return newCrypt.equals(encryptedPassword);
  }

  private static final char saltChars[] "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./"
      .toCharArray();
  // private static final int ITERATIONS = 16;
  private static final int con_salt[] 000000000000,
      0000000000000000000000,
      0000000000000123456789,
      1011567891011121314151617181920,
      2122232425262728293031323334353637,
      3233343536373839404142434445464748,
      49505152535455565758596061626300,
      00};
  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[][] {
      0160x200000000x200000100x100000x100100x20010000,
          0x20010010204820640x200008000x200008100x10800,
          0x108100x200108000x2001081032480x20000020,
          0x200000300x100200x100300x200100200x200100302080,
          20960x200008200x200008300x108200x108300x20010820,
          0x200108300x800000x800100x200800000x20080010,
          0x900000x900100x200900000x200900100x808000x80810,
          0x200808000x200808100x908000x908100x20090800,
          0x200908100x800200x800300x200800200x20080030,
          0x900200x900300x200900200x200900300x808200x80830,
          0x200808200x200808300x908200x908300x20090820,
          0x20090830 },
      00x200000081920x20020000x2000000x22000000x202000,
          0x220200040x200000481960x20020040x200004,
          0x22000040x2020040x220200410240x20004009216,
          0x20024000x2004000x22004000x2024000x22024001028,
          0x200040492200x20024040x2004040x22004040x202404,
          0x22024040x100000000x120000000x100020000x12002000,
          0x102000000x122000000x102020000x122020000x10000004,
          0x120000040x100020040x120020040x102000040x12200004,
          0x102020040x122020040x100004000x120004000x10002400,
          0x120024000x102004000x122004000x102024000x12202400,
          0x100004040x120004040x100024040x120024040x10200404,
          0x122004040x102024040x12202404 },
      010x400000x400010x10000000x10000010x1040000,
          0x1040001230x400020x400030x10000020x1000003,
          0x10400020x10400035125130x402000x40201,
          0x10002000x10002010x10402000x1040201514515,
          0x402020x402030x10002020x10002030x1040202,
          0x10402030x80000000x80000010x80400000x8040001,
          0x90000000x90000010x90400000x90400010x8000002,
          0x80000030x80400020x80400030x90000020x9000003,
          0x90400020x90400030x80002000x80002010x8040200,
          0x80402010x90002000x90002010x90402000x9040201,
          0x80002020x80002030x80402020x80402030x9000202,
          0x90002030x90402020x9040203 },
      00x1000002560x10010080x1000082640x1001084096,
          0x10100043520x10110041040x10100843600x101108,
          0x40000000x41000000x40001000x41001000x4000008,
          0x41000080x40001080x41001080x40010000x4101000,
          0x40011000x41011000x40010080x41010080x4001108,
          0x41011080x200000x1200000x201000x1201000x20008,
          0x1200080x201080x1201080x210000x1210000x21100,
          0x1211000x210080x1210080x211080x1211080x4020000,
          0x41200000x40201000x41201000x40200080x4120008,
          0x40201080x41201080x40210000x41210000x4021100,
          0x41211000x40210080x41210080x40211080x4121108 },
      00x100000000x100000x1001000040x100000040x10004,
          0x100100040x200000000x300000000x200100000x30010000,
          0x200000040x300000040x200100040x300100040x100000,
          0x101000000x1100000x101100000x1000040x10100004,
          0x1100040x101100040x201000000x301000000x20110000,
          0x301100000x201000040x301000040x201100040x30110004,
          40960x100010000x110000x1001100041000x10001004,
          0x110040x100110040x200010000x300010000x20011000,
          0x300110000x200010040x300010040x200110040x30011004,
          0x1010000x101010000x1110000x101110000x101004,
          0x101010040x1110040x101110040x201010000x30101000,
          0x201110000x301110000x201010040x301010040x20111004,
          0x30111004 },
      00x800000080x800000810240x800040010320x8000408,
          0x200000x80200000x200080x80200080x204000x8020400,
          0x204080x802040810x800000190x80000091025,
          0x800040110330x80004090x200010x80200010x20009,
          0x80200090x204010x80204010x204090x8020409,
          0x20000000xa0000000x20000080xa0000080x2000400,
          0xa0004000x20004080xa0004080x20200000xa020000,
          0x20200080xa0200080x20204000xa0204000x2020408,
          0xa0204080x20000010xa0000010x20000090xa000009,
          0x20004010xa0004010x20004090xa0004090x2020001,
          0xa0200010x20200090xa0200090x20204010xa020401,
          0x20204090xa020409 },
      02560x800000x801000x10000000x10001000x1080000,
          0x1080100162720x800100x801100x10000100x1000110,
          0x10800100x10801100x2000000x2001000x280000,
          0x2801000x12000000x12001000x12800000x1280100,
          0x2000100x2001100x2800100x2801100x1200010,
          0x12001100x12800100x12801105127680x80200,
          0x803000x10002000x10003000x10802000x1080300528,
          7840x802100x803100x10002100x10003100x1080210,
          0x10803100x2002000x2003000x2802000x280300,
          0x12002000x12003000x12802000x12803000x200210,
          0x2003100x2802100x2803100x12002100x1200310,
          0x12802100x1280310 },
      00x40000000x400000x404000020x40000020x40002,
          0x404000281920x40020000x420000x40420008194,
          0x40020020x420020x4042002320x40000200x40020,
          0x4040020340x40000220x400220x40400228224,
          0x40020200x420200x404202082260x40020220x42022,
          0x404202220480x40008000x408000x40408002050,
          0x40008020x408020x4040802102400x40028000x42800,
          0x4042800102420x40028020x428020x40428022080,
          0x40008200x408200x404082020820x40008220x40822,
          0x4040822102720x40028200x428200x404282010274,
          0x40028220x428220x4042822 } };
  private static final int SPtrans[][] {
      0x8202000x200000x808000000x808202000x8000000x80020200,
          0x800200000x808000000x800202000x8202000x820000,
          0x800002000x808002000x80000000x800200000x20000,
          0x800000000x8002000x202000x808202000x820000,
          0x800002000x8002000x800000005120x202000x80820000,
          5120x808002000x80820000000x808202000x800200,
          0x800200000x8202000x200000x800002000x800200,
          0x808200005120x202000x808000000x80020200,
          0x800000000x808000000x8200000x808202000x20200,
          0x8200000x808002000x8000000x800002000x800200000,
          0x200000x8000000x808002000x8202000x80000000,
          0x808200005120x80020200 },
      0x1004200400x420000x100400000x1000000481960x10002000,
          0x4200081920x1004000440x100020000x40004,
          0x100420000x1004000040x400000x100020040x10040004,
          81920x420040x1000000000x400040x100020040x42004,
          0x100420000x100000040x100000000x400008196,
          0x100420040x400040x100420000x100020000x42004,
          0x100420040x400040x1000000400x100000008196,
          0x400000x1004000481920x100000000x420040x10002004,
          0x10042000819200x1000000440x100420040x42000,
          0x100400000x100400040x4000081960x10002000,
          0x1000200440x100400000x42000 },
      0x410000000x1010040640x410000400x400100000x1000000,
          0x410000400x100400x10000400x100000x1010000,
          0x400000000x410100400x400000400x400000000x41010000,
          00x400100000x1010040640x400000400x41010040,
          0x100000x410000000x410100000x10000400x40010040,
          0x10100000x1004000x10000000x400100400x1010040,
          640x400000000x100000x400000400x400100000x1010000,
          0x4100004000x10100400x100400x410100000x40010000,
          0x10000000x410100400x400000000x400100400x41000000,
          0x10000000x410100400x100000x10000400x41000040,
          0x100400x100004000x410100000x400000400x41000000,
          0x40010040640x1010000 },
      0x1004020x400040020x410040200x41000000x4000402,
          0x1000020x41004000x40000020x400000010260x4000002,
          0x1004020x1000000x40000000x41000020x1004001024,
          20x1004000x40004020x41000001024102600x100002,
          0x41004000x40004000x41000020x41004020x100000,
          0x410000210260x1000000x40000020x1004000x4000400,
          20x41000000x4000402010240x10000200x4100002,
          0x410040010240x40000000x41004020x1004020x100000,
          0x410040220x40004000x1004020x1000020x100400,
          0x41000000x400040210260x40000000x40000020x4100400 },
      0x2000000163842560x20041080x20040080x200010016648,
          0x20040001638480x2000008166400x2000108,
          0x20040080x20041000166400x200000016392264,
          0x20001001664800x200000880x20001080x2004108,
          163920x20040002562640x20041000x2004100,
          0x2000108163920x20040001638480x2000008,
          0x20001000x2000000166400x2004108016648,
          0x2000000256163920x200010825600x2004108,
          0x20040080x200410026416384166400x2004008,
          0x20001002648166480x20040000x2000008 },
      0x200000100x8001000x200808000x8001020480x20000810,
          0x8000020640x200808100x808000x200000000x20000800,
          0x200000100x200800000x808100x800000x20000810,
          0x2008001002048160x200808000x20080010,
          0x200808100x200800000x200000002064160x80800,
          0x808100x2000080020640x200000000x200008000x80810,
          0x200808000x8001000x200008000x200000002048,
          0x200800100x800000x800100x200808100x8080016,
          0x200808100x808000x800000x200008100x20000010,
          0x200800000x80810020480x200000100x20000810,
          0x200808000x200800002064160x20080010 },
      40961280x4000800x4000010x4010814097422400x400000,
          0x4000811290x40100010x4010800x401000129,
          0x400081409640970x40108100x4000800x400001,
          42240x40100142250x401080142250x401001128,
          0x40000042250x4010000x4010011294096128,
          0x4000000x4010010x4000814225422401280x400001,
          10x40008000x4000810x40008042241294096,
          0x4010810x4000000x401080140970x4010810x400001,
          0x4010800x4010004097 },
      0x82000200x82080003280000x80080000x2000200x8200000,
          0x8208020320x80000000x208000328000x208020,
          0x80080200x80000200x8200000327680x2080200x200020,
          0x80080000x82080200x800002000x2080000x8000000,
          0x2000000x80080200x82000200x200000327680x8208000,
          320x200000327680x80000200x820802032800,
          0x800000000x2080000x82000200x80080200x8008000,
          0x2000200x8208000320x2000200x80080000x8208020,
          0x2000000x82000000x80000200x208000328000x8008020,
          0x8200000320x82080000x20802000x8000000,
          0x8200020327680x208020 } };
  private static final int cov_2char[] 4647484950515253,
      5455565765666768697071727374757677,
      78798081828384858687888990979899,
      100101102103104105106107108109110111112,
      113114115116117118119120121122 };

}

   
    
    
    
    
  














Related examples in the same category
1.Unix Crypt 2
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.