KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > pki > dsa > SshDssKeyPair


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.security.pki.dsa;
21
22 import java.security.KeyPair JavaDoc;
23 import java.security.KeyPairGenerator JavaDoc;
24 import java.security.NoSuchAlgorithmException JavaDoc;
25 import java.security.interfaces.DSAPrivateKey JavaDoc;
26 import java.security.interfaces.DSAPublicKey JavaDoc;
27
28 import com.sslexplorer.security.pki.InvalidKeyException;
29 import com.sslexplorer.security.pki.SshKeyPair;
30 import com.sslexplorer.security.pki.SshPrivateKey;
31 import com.sslexplorer.security.pki.SshPublicKey;
32 import com.sslexplorer.security.pki.Utils;
33
34
35 /**
36  *
37  *
38  * @author $author$
39  */

40 public class SshDssKeyPair extends SshKeyPair {
41     /**
42      *
43      *
44      * @param encoded
45      *
46      * @return
47      *
48      * @throws InvalidKeyException
49      */

50     public SshPrivateKey decodePrivateKey(byte[] encoded)
51         throws InvalidKeyException {
52         return new SshDssPrivateKey(encoded);
53     }
54
55     /**
56      *
57      *
58      * @param encoded
59      *
60      * @return
61      *
62      * @throws InvalidKeyException
63      */

64     public SshPublicKey decodePublicKey(byte[] encoded)
65         throws InvalidKeyException {
66         return new SshDssPublicKey(encoded);
67     }
68
69     /**
70      *
71      *
72      * @param bits
73      */

74     public void generate(int bits) {
75         try {
76             // Initialize the generator
77
KeyPairGenerator JavaDoc keyGen = KeyPairGenerator.getInstance("DSA");
78             keyGen.initialize(bits, Utils.getRND());
79
80             KeyPair JavaDoc pair = keyGen.generateKeyPair();
81
82             // Get the keys
83
DSAPrivateKey JavaDoc prvKey = (DSAPrivateKey JavaDoc) pair.getPrivate();
84             DSAPublicKey JavaDoc pubKey = (DSAPublicKey JavaDoc) pair.getPublic();
85
86             // Set the private key (the public is automatically generated)
87
setPrivateKey(new SshDssPrivateKey(prvKey));
88         } catch (NoSuchAlgorithmException JavaDoc nsae) {
89         }
90     }
91 }
92
Popular Tags