This project has retired. For details please refer to its
Attic page.
DefaultCryptor xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juddi.v3.client.cryptor;
18
19 import java.io.UnsupportedEncodingException;
20 import java.security.InvalidAlgorithmParameterException;
21 import java.security.InvalidKeyException;
22 import java.security.NoSuchAlgorithmException;
23 import java.security.spec.InvalidKeySpecException;
24
25 import javax.crypto.BadPaddingException;
26 import javax.crypto.Cipher;
27 import javax.crypto.IllegalBlockSizeException;
28 import javax.crypto.NoSuchPaddingException;
29 import javax.crypto.SecretKey;
30 import javax.crypto.SecretKeyFactory;
31 import javax.crypto.spec.PBEKeySpec;
32 import javax.crypto.spec.PBEParameterSpec;
33
34 import org.apache.commons.codec.binary.Base64;
35
36
37
38
39 public class DefaultCryptor implements Cryptor {
40
41 private PBEKeySpec pbeKeySpec = null;
42 private PBEParameterSpec pbeParamSpec = null;
43 private SecretKeyFactory keyFac = null;
44 private SecretKey pbeKey = null;
45
46
47 private byte[] salt = {
48 (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
49 (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
50 };
51
52
53 private int count = 20;
54
55
56
57
58 public DefaultCryptor()
59 throws NoSuchAlgorithmException, InvalidKeySpecException {
60
61 pbeParamSpec = new PBEParameterSpec(salt, count);
62 pbeKeySpec = new PBEKeySpec("saagar".toCharArray());
63 keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
64 pbeKey = keyFac.generateSecret(pbeKeySpec);
65 }
66
67
68
69
70 private byte[] crypt(int cipherMode, byte[] text)
71 throws NoSuchPaddingException,
72 NoSuchAlgorithmException,
73 InvalidAlgorithmParameterException,
74 InvalidKeyException,
75 IllegalBlockSizeException,
76 BadPaddingException {
77
78 Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
79
80
81 pbeCipher.init(cipherMode, pbeKey, pbeParamSpec);
82
83
84
85 byte[] cryptext = pbeCipher.doFinal(text);
86
87 return cryptext;
88 }
89
90
91
92
93 public String encrypt(String str)
94 throws NoSuchPaddingException,
95 NoSuchAlgorithmException,
96 InvalidAlgorithmParameterException,
97 InvalidKeyException,
98 IllegalBlockSizeException,
99 BadPaddingException,
100 UnsupportedEncodingException {
101 byte[] encs = crypt(Cipher.ENCRYPT_MODE, str.getBytes());
102 encs = Base64.encodeBase64(encs);
103 return new String(encs, "UTF-8");
104 }
105
106 public String decrypt(String str) throws NoSuchPaddingException,
107 NoSuchAlgorithmException,
108 InvalidAlgorithmParameterException,
109 InvalidKeyException,
110 IllegalBlockSizeException,
111 BadPaddingException,
112 UnsupportedEncodingException {
113 byte[] encs = crypt(Cipher.DECRYPT_MODE, Base64.decodeBase64(str.getBytes("UTF-8")));
114 return new String(encs, "UTF-8");
115 }
116
117 @Override
118 public String newKey() {
119 throw new UnsupportedOperationException("Not supported yet.");
120 }
121
122 }