KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > crypto > Test


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.crypto;
17
18 import javax.crypto.spec.PBEKeySpec;
19 import javax.crypto.spec.PBEParameterSpec;
20 import javax.crypto.SecretKeyFactory;
21 import javax.crypto.SecretKey;
22 import javax.crypto.Cipher;
23
24 /**
25  *
26  * date: Jul 22, 2004
27  * @author Rakesh Kalra
28  */

29 public class Test {
30
31
32     private static final String JavaDoc PWD = "test";
33
34     public static void main(String JavaDoc[] args)
35             throws Exception JavaDoc {
36
37         String JavaDoc plaintext = "this is plaintext";
38         byte[] cyphertext = getEncrypted(plaintext);
39         System.out.println(cyphertext);
40         String JavaDoc plaintext1 = getDecrypted(cyphertext);
41         System.out.println(plaintext1);
42     }
43
44     private static byte[] getEncrypted(String JavaDoc plaintext)
45         throws Exception JavaDoc {
46
47         PBEKeySpec pbeKeySpec;
48         PBEParameterSpec pbeParamSpec;
49         SecretKeyFactory keyFac;
50
51         // Salt
52
byte[] salt = {
53             (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
54             (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
55         };
56
57         // Iteration count
58
int count = 20;
59
60         // Create PBE parameter set
61
pbeParamSpec = new PBEParameterSpec(salt, count);
62
63         pbeKeySpec = new PBEKeySpec(PWD.toCharArray());
64         keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
65         SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
66
67         // Create PBE Cipher
68
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
69
70         // Initialize PBE Cipher with key and parameters
71
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
72
73         // Our cleartext
74
byte[] cleartext = plaintext.getBytes();
75
76         // Encrypt the cleartext
77
return pbeCipher.doFinal(cleartext);
78     }
79
80
81     private static String JavaDoc getDecrypted(byte[] cyphertext)
82         throws Exception JavaDoc {
83
84         PBEKeySpec pbeKeySpec;
85         PBEParameterSpec pbeParamSpec;
86         SecretKeyFactory keyFac;
87
88         // Salt
89
byte[] salt = {
90             (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
91             (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99
92         };
93
94         // Iteration count
95
int count = 20;
96
97         // Create PBE parameter set
98
pbeParamSpec = new PBEParameterSpec(salt, count);
99
100         pbeKeySpec = new PBEKeySpec(PWD.toCharArray());
101         keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
102         SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
103
104         // Create PBE Cipher
105
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
106
107         // Initialize PBE Cipher with key and parameters
108
pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
109
110         // Encrypt the cleartext
111
return new String JavaDoc(pbeCipher.doFinal(cyphertext));
112     }
113 }
114
Popular Tags