1 /* 2 * This file is part of "The Java Telnet Application". 3 * 4 * (c) Matthias L. Jugel, Marcus Meißner 1996-2002. All Rights Reserved. 5 * The file was changed by Radek Polak to work as midlet in MIDP 1.0 6 * 7 * Please visit http://javatelnet.org/ for updates and contact. 8 * 9 * --LICENSE NOTICE-- 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program; if not, write to the Free Software 22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 23 * --LICENSE NOTICE-- 24 * 25 * Additional NOTICE: This file uses DES (see DES.java for copyright 26 * information!) 27 */ 28 package ssh; 29 30 31 public final class DES3 extends Cipher { 32 DES des1 = new DES(); 33 DES des2 = new DES(); 34 DES des3 = new DES(); 35 36 public synchronized void encrypt(byte[] src, int srcOff, byte[] dest, int destOff, int len) { 37 des1.encrypt(src, srcOff, dest, destOff, len); 38 des2.decrypt(dest, destOff, dest, destOff, len); 39 des3.encrypt(dest, destOff, dest, destOff, len); 40 } 41 42 public synchronized void decrypt(byte[] src, int srcOff, byte[] dest, int destOff, int len) { 43 des3.decrypt(src, srcOff, dest, destOff, len); 44 des2.encrypt(dest, destOff, dest, destOff, len); 45 des1.decrypt(dest, destOff, dest, destOff, len); 46 } 47 48 public void setKey(byte[] key) { 49 byte[] subKey = new byte[8]; 50 des1.setKey(key); 51 System.arraycopy(key, 8, subKey, 0, 8); 52 des2.setKey(subKey); 53 System.arraycopy(key, 16, subKey, 0, 8); 54 des3.setKey(subKey); 55 } 56 57 /* !!! DEBUG 58 public static void main(String[] argv) { 59 byte[] key = { 60 (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, 61 (byte)0x87, (byte)0x65, (byte)0x43, (byte)0x21, 62 (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77, 63 (byte)0x87, (byte)0x65, (byte)0x43, (byte)0x21, 64 (byte)0x87, (byte)0x65, (byte)0x43, (byte)0x21, 65 (byte)0x12, (byte)0x34, (byte)0x56, (byte)0x78, 66 }; 67 68 byte[] txt = { 69 (byte)0x00, (byte)0x11, (byte)0x22, (byte)0x33, 70 (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77, 71 (byte)0x00, (byte)0x11, (byte)0x22, (byte)0x33, 72 (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77, 73 (byte)0x00, (byte)0x11, (byte)0x22, (byte)0x33, 74 (byte)0x44, (byte)0x55, (byte)0x66, (byte)0x77 75 }; 76 77 byte[] enc; 78 byte[] dec; 79 80 System.out.println("key: " + printHex(key)); 81 System.out.println("txt: " + printHex(txt)); 82 83 DES3 cipher = new DES3(); 84 cipher.setKey(key); 85 86 enc = cipher.encrypt(txt); 87 System.out.println("enc: " + printHex(enc)); 88 89 cipher = new DES3(); 90 cipher.setKey(key); 91 92 dec = cipher.decrypt(enc); 93 94 System.out.println("dec: " + printHex(dec)); 95 } 96 97 static String printHex(byte[] buf) { 98 byte[] out = new byte[buf.length + 1]; 99 out[0] = 0; 100 System.arraycopy(buf, 0, out, 1, buf.length); 101 BigInteger big = new BigInteger(out); 102 return big.toString(16); 103 } 104 static String printHex(int i) { 105 BigInteger b = BigInteger.valueOf((long)i + 0x100000000L); 106 BigInteger c = BigInteger.valueOf(0x100000000L); 107 if(b.compareTo(c) != -1) 108 b = b.subtract(c); 109 return b.toString(16); 110 } 111 112 */ 113 114 } |