1 19 20 package com.maverick.crypto.engines; 21 22 import java.io.IOException ; 23 24 27 public class DESedeEngine 28 extends DESEngine { 29 protected static final int BLOCK_SIZE = 8; 30 31 private int[] workingKey1 = null; 32 private int[] workingKey2 = null; 33 private int[] workingKey3 = null; 34 35 private boolean forEncryption; 36 37 40 public DESedeEngine() { 41 } 42 43 51 public void init( 52 boolean encrypting, 53 byte[] key) { 54 55 byte[] keyMaster = key; 56 byte[] key1 = new byte[8], key2 = new byte[8], key3 = new byte[8]; 57 58 this.forEncryption = encrypting; 59 60 if (keyMaster.length == 24) { 61 System.arraycopy(keyMaster, 0, key1, 0, key1.length); 62 System.arraycopy(keyMaster, 8, key2, 0, key2.length); 63 System.arraycopy(keyMaster, 16, key3, 0, key3.length); 64 65 workingKey1 = generateWorkingKey(encrypting, key1); 66 workingKey2 = generateWorkingKey(!encrypting, key2); 67 workingKey3 = generateWorkingKey(encrypting, key3); 68 } 69 else { System.arraycopy(keyMaster, 0, key1, 0, key1.length); 71 System.arraycopy(keyMaster, 8, key2, 0, key2.length); 72 73 workingKey1 = generateWorkingKey(encrypting, key1); 74 workingKey2 = generateWorkingKey(!encrypting, key2); 75 workingKey3 = workingKey1; 76 } 77 } 78 79 public String getAlgorithmName() { 80 return "DESede"; 81 } 82 83 public int getBlockSize() { 84 return BLOCK_SIZE; 85 } 86 87 public int processBlock( 88 byte[] in, 89 int inOff, 90 byte[] out, 91 int outOff) throws IOException { 92 if (workingKey1 == null) { 93 throw new IllegalStateException ("DESede engine not initialised"); 94 } 95 96 if ( (inOff + BLOCK_SIZE) > in.length) { 97 throw new IOException ("input buffer too short"); 98 } 99 100 if ( (outOff + BLOCK_SIZE) > out.length) { 101 throw new IOException ("output buffer too short"); 102 } 103 104 if (forEncryption) { 105 desFunc(workingKey1, in, inOff, out, outOff); 106 desFunc(workingKey2, out, outOff, out, outOff); 107 desFunc(workingKey3, out, outOff, out, outOff); 108 } 109 else { 110 desFunc(workingKey3, in, inOff, out, outOff); 111 desFunc(workingKey2, out, outOff, out, outOff); 112 desFunc(workingKey1, out, outOff, out, outOff); 113 } 114 115 return BLOCK_SIZE; 116 } 117 118 public void reset() { 119 } 120 } 121 | Popular Tags |