This project has retired. For details please refer to its
Attic page.
DES xref
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juddi.samples;
18
19 import java.security.InvalidAlgorithmParameterException;
20 import java.security.InvalidKeyException;
21 import java.security.NoSuchAlgorithmException;
22 import java.security.spec.KeySpec;
23 import javax.crypto.BadPaddingException;
24 import javax.crypto.Cipher;
25 import javax.crypto.IllegalBlockSizeException;
26 import javax.crypto.KeyGenerator;
27 import javax.crypto.NoSuchPaddingException;
28 import javax.crypto.SecretKey;
29 import javax.crypto.SecretKeyFactory;
30 import javax.crypto.spec.DESedeKeySpec;
31 import org.apache.commons.codec.binary.Base64;
32
33
34
35
36
37
38
39 public class DES {
40
41 private static final String UNICODE_FORMAT = "UTF8";
42 public static final String DESEDE_ENCRYPTION_SCHEME = "DESede";
43 static private KeySpec ks;
44 static private SecretKeyFactory skf;
45 static private Cipher cipher;
46 static byte[] arrayBytes;
47 static private String myEncryptionKey;
48 static private String myEncryptionScheme;
49 static SecretKey key;
50
51 public DES() throws Exception {
52 }
53
54 public String encrypt(String clear) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
55 String encryptedString = null;
56 try {
57 cipher.init(Cipher.ENCRYPT_MODE, key);
58 byte[] plainText = clear.getBytes(UNICODE_FORMAT);
59 byte[] encryptedText = cipher.doFinal(plainText);
60 encryptedString = new String(Base64.encodeBase64(encryptedText), "UTF-8");
61 } catch (Exception e) {
62 e.printStackTrace();
63 }
64 return encryptedString;
65 }
66
67 public String decrypt(String str) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
68 String encryptedString = null;
69 try {
70 cipher.init(Cipher.DECRYPT_MODE, key);
71 byte[] encryptedText = Base64.decodeBase64(str.getBytes("UTF-8"));
72 byte[] clear = cipher.doFinal(encryptedText);
73 encryptedString = new String(Base64.encodeBase64(clear), "UTF-8");
74 } catch (Exception e) {
75 e.printStackTrace();
76 }
77 return encryptedString;
78 }
79
80 public static void main(String[] args) throws Exception {
81 DES des = new DES();
82 KeyGenerator kgen;
83 try {
84 kgen = KeyGenerator.getInstance(DESEDE_ENCRYPTION_SCHEME);
85 kgen.init(168);
86 SecretKey skey = kgen.generateKey();
87 byte[] raw = skey.getEncoded();
88 myEncryptionKey = new String(Base64.encodeBase64(raw), "UTF-8");
89 myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
90 arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
91 ks = new DESedeKeySpec(arrayBytes);
92 skf = SecretKeyFactory.getInstance(myEncryptionScheme);
93 cipher = Cipher.getInstance(myEncryptionScheme);
94 key = skf.generateSecret(ks);
95
96 System.out.println(new String(Base64.encodeBase64(raw), "UTF-8"));
97 System.out.println(des.encrypt("password"));
98 System.out.println(des.decrypt(des.encrypt("password")));
99 } catch (Exception ex) {
100 ex.printStackTrace();;
101 }
102
103 }
104 }