jcifs.util.DES Maven / Gradle / Ivy
Show all versions of jcifs-krb5-jdk7 Show documentation
// DesCipher - the DES encryption method
//
// The meat of this code is by Dave Zimmerman , and is:
//
// Copyright (c) 1996 Widget Workshop, Inc. All Rights Reserved.
//
// Permission to use, copy, modify, and distribute this software
// and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
// without fee is hereby granted, provided that this copyright notice is kept
// intact.
//
// WIDGET WORKSHOP MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY
// OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE, OR NON-INFRINGEMENT. WIDGET WORKSHOP SHALL NOT BE LIABLE
// FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
// DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
//
// THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
// CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
// PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
// NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
// SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
// SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
// PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). WIDGET WORKSHOP
// SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
// HIGH RISK ACTIVITIES.
//
//
// The rest is:
//
// Copyright (C) 1996 by Jef Poskanzer . All rights reserved.
//
// Copyright (C) 1996 by Wolfgang Platzer
// email: [email protected]
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
// SUCH DAMAGE.
//
package jcifs.util;
import java.io.*;
/**
* This code is derived from the above source
* JCIFS API
* Norbert Hranitzky
*
* and modified again by Michael B. Allen
*/
public class DES {
private int[] encryptKeys = new int[32];
private int[] decryptKeys = new int[32];
private int[] tempInts = new int[2];
public DES( ) {
}
// Constructor, byte-array key.
public DES( byte[] key ) {
if( key.length == 7 ) {
byte[] key8 = new byte[8];
makeSMBKey( key, key8 );
setKey( key8 );
} else {
setKey( key );
}
}
public static void makeSMBKey(byte[] key7, byte[] key8) {
int i;
key8[0] = (byte) ( ( key7[0] >> 1) & 0xff);
key8[1] = (byte)(( ((key7[0] & 0x01) << 6) | (((key7[1] & 0xff)>>2) & 0xff)) & 0xff );
key8[2] = (byte)(( ((key7[1] & 0x03) << 5) | (((key7[2] & 0xff)>>3) & 0xff)) & 0xff );
key8[3] = (byte)(( ((key7[2] & 0x07) << 4) | (((key7[3] & 0xff)>>4) & 0xff)) & 0xff );
key8[4] = (byte)(( ((key7[3] & 0x0F) << 3) | (((key7[4] & 0xff)>>5) & 0xff)) & 0xff );
key8[5] = (byte)(( ((key7[4] & 0x1F) << 2) | (((key7[5] & 0xff)>>6) & 0xff)) & 0xff );
key8[6] = (byte)(( ((key7[5] & 0x3F) << 1) | (((key7[6] & 0xff)>>7) & 0xff)) & 0xff );
key8[7] = (byte)(key7[6] & 0x7F);
for (i=0;i<8;i++) {
key8[i] = (byte)( key8[i] << 1);
}
}
/// Set the key.
public void setKey( byte[] key ) {
// CHECK PAROTY TBD
deskey( key, true, encryptKeys );
deskey( key, false, decryptKeys );
}
// Turn an 8-byte key into internal keys.
private void deskey( byte[] keyBlock, boolean encrypting, int[] KnL ) {
int i, j, l, m, n;
int[] pc1m = new int[56];
int[] pcr = new int[56];
int[] kn = new int[32];
for ( j = 0; j < 56; ++j ) {
l = pc1[j];
m = l & 07;
pc1m[j] = ( (keyBlock[l >>> 3] & bytebit[m]) != 0 )? 1: 0;
}
for ( i = 0; i < 16; ++i ) {
if ( encrypting )
m = i << 1;
else
m = (15-i) << 1;
n = m+1;
kn[m] = kn[n] = 0;
for ( j = 0; j < 28; ++j ) {
l = j+totrot[i];
if ( l < 28 )
pcr[j] = pc1m[l];
else
pcr[j] = pc1m[l-28];
}
for ( j=28; j < 56; ++j ) {
l = j+totrot[i];
if ( l < 56 )
pcr[j] = pc1m[l];
else
pcr[j] = pc1m[l-28];
}
for ( j = 0; j < 24; ++j ) {
if ( pcr[pc2[j]] != 0 )
kn[m] |= bigbyte[j];
if ( pcr[pc2[j+24]] != 0 )
kn[n] |= bigbyte[j];
}
}
cookey( kn, KnL );
}
private void cookey( int[] raw, int KnL[] ) {
int raw0, raw1;
int rawi, KnLi;
int i;
for ( i = 0, rawi = 0, KnLi = 0; i < 16; ++i ) {
raw0 = raw[rawi++];
raw1 = raw[rawi++];
KnL[KnLi] = (raw0 & 0x00fc0000) << 6;
KnL[KnLi] |= (raw0 & 0x00000fc0) << 10;
KnL[KnLi] |= (raw1 & 0x00fc0000) >>> 10;
KnL[KnLi] |= (raw1 & 0x00000fc0) >>> 6;
++KnLi;
KnL[KnLi] = (raw0 & 0x0003f000) << 12;
KnL[KnLi] |= (raw0 & 0x0000003f) << 16;
KnL[KnLi] |= (raw1 & 0x0003f000) >>> 4;
KnL[KnLi] |= (raw1 & 0x0000003f);
++KnLi;
}
}
/// Encrypt a block of eight bytes.
private void encrypt( byte[] clearText, int clearOff, byte[] cipherText, int cipherOff ) {
squashBytesToInts( clearText, clearOff, tempInts, 0, 2 );
des( tempInts, tempInts, encryptKeys );
spreadIntsToBytes( tempInts, 0, cipherText, cipherOff, 2 );
}
/// Decrypt a block of eight bytes.
private void decrypt( byte[] cipherText, int cipherOff, byte[] clearText, int clearOff ) {
squashBytesToInts( cipherText, cipherOff, tempInts, 0, 2 );
des( tempInts, tempInts, decryptKeys );
spreadIntsToBytes( tempInts, 0, clearText, clearOff, 2 );
}
// The DES function.
private void des( int[] inInts, int[] outInts, int[] keys ) {
int fval, work, right, leftt;
int round;
int keysi = 0;
leftt = inInts[0];
right = inInts[1];
work = ((leftt >>> 4) ^ right) & 0x0f0f0f0f;
right ^= work;
leftt ^= (work << 4);
work = ((leftt >>> 16) ^ right) & 0x0000ffff;
right ^= work;
leftt ^= (work << 16);
work = ((right >>> 2) ^ leftt) & 0x33333333;
leftt ^= work;
right ^= (work << 2);
work = ((right >>> 8) ^ leftt) & 0x00ff00ff;
leftt ^= work;
right ^= (work << 8);
right = (right << 1) | ((right >>> 31) & 1);
work = (leftt ^ right) & 0xaaaaaaaa;
leftt ^= work;
right ^= work;
leftt = (leftt << 1) | ((leftt >>> 31) & 1);
for ( round = 0; round < 8; ++round ) {
work = (right << 28) | (right >>> 4);
work ^= keys[keysi++];
fval = SP7[ work & 0x0000003f ];
fval |= SP5[(work >>> 8) & 0x0000003f ];
fval |= SP3[(work >>> 16) & 0x0000003f ];
fval |= SP1[(work >>> 24) & 0x0000003f ];
work = right ^ keys[keysi++];
fval |= SP8[ work & 0x0000003f ];
fval |= SP6[(work >>> 8) & 0x0000003f ];
fval |= SP4[(work >>> 16) & 0x0000003f ];
fval |= SP2[(work >>> 24) & 0x0000003f ];
leftt ^= fval;
work = (leftt << 28) | (leftt >>> 4);
work ^= keys[keysi++];
fval = SP7[ work & 0x0000003f ];
fval |= SP5[(work >>> 8) & 0x0000003f ];
fval |= SP3[(work >>> 16) & 0x0000003f ];
fval |= SP1[(work >>> 24) & 0x0000003f ];
work = leftt ^ keys[keysi++];
fval |= SP8[ work & 0x0000003f ];
fval |= SP6[(work >>> 8) & 0x0000003f ];
fval |= SP4[(work >>> 16) & 0x0000003f ];
fval |= SP2[(work >>> 24) & 0x0000003f ];
right ^= fval;
}
right = (right << 31) | (right >>> 1);
work = (leftt ^ right) & 0xaaaaaaaa;
leftt ^= work;
right ^= work;
leftt = (leftt << 31) | (leftt >>> 1);
work = ((leftt >>> 8) ^ right) & 0x00ff00ff;
right ^= work;
leftt ^= (work << 8);
work = ((leftt >>> 2) ^ right) & 0x33333333;
right ^= work;
leftt ^= (work << 2);
work = ((right >>> 16) ^ leftt) & 0x0000ffff;
leftt ^= work;
right ^= (work << 16);
work = ((right >>> 4) ^ leftt) & 0x0f0f0f0f;
leftt ^= work;
right ^= (work << 4);
outInts[0] = right;
outInts[1] = leftt;
}
/// Encrypt a block of bytes.
public void encrypt( byte[] clearText, byte[] cipherText ) {
encrypt( clearText, 0, cipherText, 0 );
}
/// Decrypt a block of bytes.
public void decrypt( byte[] cipherText, byte[] clearText ) {
decrypt( cipherText, 0, clearText, 0 );
}
/**
* encrypts an array where the length must be a multiple of 8
*/
public byte[] encrypt(byte[] clearText) {
int length = clearText.length;
if (length % 8 != 0) {
System.out.println("Array must be a multiple of 8");
return null;
}
byte[] cipherText = new byte[length];
int count = length / 8;
for (int i=0; i>> 24 );
outBytes[outOff + i * 4 + 1] = (byte) ( inInts[inOff + i] >>> 16 );
outBytes[outOff + i * 4 + 2] = (byte) ( inInts[inOff + i] >>> 8 );
outBytes[outOff + i * 4 + 3] = (byte) inInts[inOff + i];
}
}
}