1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998,99 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | *****************************************************************************
|
---|
17 | * $Author: mats $
|
---|
18 | * $Date: 1999/08/16 18:49:30 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.security;
|
---|
22 |
|
---|
23 | public abstract class Cipher {
|
---|
24 |
|
---|
25 | public static Cipher getInstance(String algorithm) {
|
---|
26 | Class c;
|
---|
27 | try {
|
---|
28 | c = Class.forName("mindbright.security." + algorithm);
|
---|
29 | return (Cipher)c.newInstance();
|
---|
30 | } catch(Throwable t) {
|
---|
31 | return null;
|
---|
32 | }
|
---|
33 | }
|
---|
34 |
|
---|
35 | public byte[] encrypt(byte[] src) { byte[] dest = new byte[src.length];
|
---|
36 | encrypt(src, 0, dest, 0, src.length);
|
---|
37 | return dest; }
|
---|
38 | public abstract void encrypt(byte[] src, int srcOff, byte[] dest, int destOff, int len);
|
---|
39 | public byte[] decrypt(byte[] src) { byte[] dest = new byte[src.length];
|
---|
40 | decrypt(src, 0, dest, 0, src.length);
|
---|
41 | return dest; }
|
---|
42 | public abstract void decrypt(byte[] src, int srcOff, byte[] dest, int destOff, int len);
|
---|
43 | public abstract void setKey(byte[] key);
|
---|
44 |
|
---|
45 | public void setKey(String key) {
|
---|
46 | MessageDigest md5;
|
---|
47 | byte[] mdKey = new byte[32];
|
---|
48 | try {
|
---|
49 | md5 = MessageDigest.getInstance("MD5");
|
---|
50 | md5.update(key.getBytes());
|
---|
51 | byte[] digest = md5.digest();
|
---|
52 | System.arraycopy(digest, 0, mdKey, 0, 16);
|
---|
53 | System.arraycopy(digest, 0, mdKey, 16, 16);
|
---|
54 | } catch(Exception e) {
|
---|
55 | // !!!
|
---|
56 | System.out.println("MD5 not implemented, can't generate key out of string!");
|
---|
57 | System.exit(1);
|
---|
58 | }
|
---|
59 | setKey(mdKey);
|
---|
60 | }
|
---|
61 |
|
---|
62 | }
|
---|