Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: cryptix/jce/provider/rsa/RSAPublicKeyX509.java


1   /* $Id: RSAPublicKeyX509.java,v 1.3 2001/07/10 18:54:37 edwin Exp $
2    *
3    * Copyright (C) 2000 The Cryptix Foundation Limited.
4    * All rights reserved.
5    *
6    * Use, modification, copying and distribution of this software is subject
7    * the terms and conditions of the Cryptix General Licence. You should have
8    * received a copy of the Cryptix General Licence along with this library;
9    * if not, you can download a copy from http://www.cryptix.org/ .
10   */
11  package cryptix.jce.provider.rsa;
12  
13  
14  import cryptix.jce.provider.asn.AsnBitString;
15  import cryptix.jce.provider.asn.AsnInteger;
16  import cryptix.jce.provider.asn.AsnObject;
17  import cryptix.jce.provider.asn.AsnObjectId;
18  import cryptix.jce.provider.asn.AsnOutputStream;
19  import cryptix.jce.provider.asn.AsnSequence;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  import java.math.BigInteger;
24  import java.security.interfaces.RSAPublicKey;
25  
26  
27  /**
28   * @version $Revision: 1.3 $
29   * @author  Jeroen C. van Gelderen (gelderen@cryptix.org)
30   */
31  /*package*/ final class RSAPublicKeyX509
32  implements RSAPublicKey
33  {
34      private final BigInteger n, e;
35  
36  
37      /*package*/ RSAPublicKeyX509(BigInteger n, BigInteger e)
38      {
39          this.n = n;
40          this.e = e;
41      }
42  
43  
44      public BigInteger getModulus()
45      {
46          return this.n;
47      }
48  
49  
50      public BigInteger getPublicExponent()
51      {
52          return this.e;
53      }
54  
55  
56  // Implementation of Key interface
57  //...........................................................................
58  
59      public String getAlgorithm()
60      {
61          return "RSA";
62      }
63  
64  
65      public String getFormat()
66      {
67          return "X.509";
68      }
69  
70  
71      public byte[] getEncoded()
72      {
73          /*
74           * SubjectPublicKeyInfo ::= SEQUENCE {
75           *     algorithm AlgorithmIdentifier,
76           *     subjectPublicKey BIT STRING
77           * }
78           *
79           * AlgorithmIdentifier ::= SEQUENCE {
80           *     algorithm OBJECT IDENTIFIER,
81           *     parameters ANY DEFINED BY algorithm OPTIONAL
82           * }
83           *
84           * PKCS#1 defines;
85           *
86           * rsaEncryption OBJECT IDENTIFIER ::=  { 1 2 840 113549 1 1 1 }
87           *
88           * RSAPublicKey ::= SEQUENCE {
89           *  modulus INTEGER, -- n
90           *  publicExponent INTEGER -- e
91           * }
92           */
93  
94          AsnObject[] spkData = { new AsnInteger(this.n),new AsnInteger(this.e) };
95      byte[] spkBytes;
96      
97      try
98      {
99              ByteArrayOutputStream baos = new ByteArrayOutputStream();
100             AsnOutputStream       dos  = new AsnOutputStream(baos);
101             dos.write( new AsnSequence(spkData) );
102             dos.flush();
103             dos.close();
104             spkBytes = baos.toByteArray();
105     }    
106         catch(IOException e)
107         {
108             throw new RuntimeException("PANIC");
109         }
110     
111         AsnBitString subjectPublicKey = new AsnBitString( spkBytes );
112 
113         AsnObject[] algData = { AsnObjectId.OID_rsaEncryption };
114         AsnSequence algorithm = new AsnSequence(algData);
115 
116         AsnObject[] spkiData = { algorithm, subjectPublicKey };
117         AsnSequence subjectPublicKeyInfo = new AsnSequence( spkiData );
118 
119         try
120         {
121             ByteArrayOutputStream baos = new ByteArrayOutputStream();
122             AsnOutputStream       dos  = new AsnOutputStream(baos);
123             dos.write(subjectPublicKeyInfo);
124             dos.flush();
125             dos.close();
126             return baos.toByteArray();
127         }
128         catch(IOException e)
129         {
130             throw new RuntimeException("PANIC");
131         }
132     }
133 }