Source Code Cross Referenced for RSAKey.java in  » Portal » Open-Portal » com » sun » portal » ksecurity » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Portal » Open Portal » com.sun.portal.ksecurity 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)RSAKey.java	1.7 02/07/24 @(#)
003:         *
004:         * Copyright (c) 2000-2001 Sun Microsystems, Inc.  All rights reserved.
005:         * PROPRIETARY/CONFIDENTIAL
006:         * Use is subject to license terms.
007:         */
008:
009:        package com.sun.portal.ksecurity;
010:
011:        /**
012:         * Implements RSAKey with methods to set and get RSA exponent
013:         * and modulus.
014:         */
015:        final class RSAKey implements  RSAPublicKey, RSAPrivateKey {
016:            /** Type of key, e.g. DES, RSA etc. */
017:            byte kind;
018:            /** Key size in bits, e.g. for RSA, this is modulus size. */
019:            short bitsize;
020:            /** Flag to indicate the key has been initialized. */
021:            boolean initOk;
022:            /** Local variable to hold the exponent. */
023:            byte[] exp = null;
024:            /**
025:             * The number of bytes allocated to the modulus
026:             * is always a multiple of 8.
027:             */
028:            byte[] mod = null;
029:
030:            /**
031:             * Constructor for RSA public or private key.
032:             * @param type of key to process
033:             * @param len length of the key
034:             */
035:            RSAKey(byte type, short len) {
036:                kind = type;
037:                bitsize = len;
038:                initOk = false;
039:            }
040:
041:            /**
042:             * Clear the current key.
043:             */
044:            public void clearKey() {
045:                initOk = false;
046:                bitsize = 0;
047:            }
048:
049:            /**
050:             * Compare the current key as a RSA private key. 
051:             * @param k the key to compare
052:             * @return true if the kesy are the same.
053:             */
054:            public boolean equals(RSAPrivateKey k) {
055:                return equals((RSAKey) k);
056:            }
057:
058:            /**
059:             * Compare the current key as a RSA public key. 
060:             * @param k the key to compare
061:             * @return true if the keys are the same.
062:             */
063:            public boolean equals(RSAPublicKey k) {
064:                return equals((RSAKey) k);
065:            }
066:
067:            /**
068:             * Compare the current key as a generic RSA key. 
069:             * @param k the key to compare
070:             * @return true if the keys are the same.
071:             */
072:            public boolean equals(RSAKey k) {
073:                byte[] kexp = new byte[exp.length];
074:                byte[] kmod = new byte[mod.length];
075:
076:                if (kind != k.getType())
077:                    return false;
078:                if (k.getExponent(kexp, (short) 0) != exp.length)
079:                    return false;
080:                if (k.getModulus(kmod, (short) 0) != mod.length)
081:                    return false;
082:                for (int i = 0; i < exp.length; i++)
083:                    if (kexp[i] != exp[i])
084:                        return false;
085:                for (int i = 0; i < mod.length; i++)
086:                    if (kmod[i] != mod[i])
087:                        return false;
088:                return true;
089:            }
090:
091:            /** 
092:             * Get the size of the current key.
093:             * @return length of the key
094:             */
095:            public short getSize() {
096:                // round up to closest multiple of 64
097:                return (short) (((bitsize + 63) >>> 6) << 6);
098:                // return bitsize;
099:            }
100:
101:            /**
102:             * Get the type of the current key.
103:             * @return the type of the key
104:             */
105:            public byte getType() {
106:                return kind;
107:            }
108:
109:            /** 
110:             * Check if the key has been initialized.
111:             * @return true if the key has been initialized.
112:             */
113:            public boolean isInitialized() {
114:                return initOk;
115:            }
116:
117:            // The next four are for RSA public/private keys
118:            /**
119:             * Get the exponent for the key.
120:             * @param buf output buffer to hold the exponent
121:             * @param off offset in the buffer to copy the exponent
122:             * @return length of the data copied
123:             * @see #setExponent
124:             */
125:            public short getExponent(byte[] buf, short off) {
126:                if (((kind != KeyBuilder.TYPE_RSA_PRIVATE) && (kind != KeyBuilder.TYPE_RSA_PUBLIC))
127:                        || !initOk)
128:                    return ((short) 0);
129:
130:                if (off + exp.length > buf.length)
131:                    return 0;
132:                System.arraycopy(exp, 0, buf, off, exp.length);
133:                return ((short) exp.length);
134:            }
135:
136:            /**
137:             * Get the modulus for the key.
138:             * @param buf output buffer to hold the modulus
139:             * @param off offset in the buffer to copy the modulus
140:             * @return length of the data copied
141:             * @see #setModulus
142:             */
143:            public short getModulus(byte[] buf, short off) {
144:                if (((kind != KeyBuilder.TYPE_RSA_PRIVATE) && (kind != KeyBuilder.TYPE_RSA_PUBLIC))
145:                        || !initOk)
146:                    return ((short) 0);
147:
148:                if (off + mod.length > buf.length)
149:                    return ((short) 0);
150:                System.arraycopy(mod, 0, buf, off, mod.length);
151:                return ((short) mod.length);
152:            }
153:
154:            /**
155:             * Set the exponent for the key.
156:             * @param buf input buffer which hold the exponent
157:             * @param off offset in the buffer
158:             * @param len length of the data to be copied
159:             * @see #getExponent
160:             */
161:            public void setExponent(byte[] buf, short off, short len)
162:                    throws CryptoException {
163:                if ((kind != KeyBuilder.TYPE_RSA_PRIVATE)
164:                        && (kind != KeyBuilder.TYPE_RSA_PUBLIC))
165:                    return;
166:
167:                int cnt = getSize() >>> 3;
168:                if (len > cnt)
169:                    throw new CryptoException(CryptoException.ILLEGAL_VALUE);
170:
171:                exp = new byte[len];
172:                System.arraycopy(buf, off, exp, 0, len);
173:                if (mod != null)
174:                    initOk = true;
175:            }
176:
177:            /**
178:             * Set the modulus for the key.
179:             * @param buf input buffer which hold the modulus
180:             * @param off offset in the buffer
181:             * @param len length of the data to be copied
182:             * @see #getModulus
183:             */
184:            public void setModulus(byte[] buf, short off, short len)
185:                    throws CryptoException {
186:                if ((kind != KeyBuilder.TYPE_RSA_PRIVATE)
187:                        && (kind != KeyBuilder.TYPE_RSA_PUBLIC))
188:                    return;
189:
190:                int cnt = getSize() >>> 3;
191:                // System.out.println("bitsize is " + bitsize + " cnt is " + cnt);
192:                if (len > cnt)
193:                    throw new CryptoException(CryptoException.ILLEGAL_VALUE);
194:
195:                mod = new byte[cnt];
196:                System.arraycopy(buf, off, mod, (cnt - len), len);
197:                if (exp != null)
198:                    initOk = true;
199:            }
200:
201:            /**
202:             * Convert the key to a readable string.
203:             * @return string representation of key
204:             */
205:            public String toString() {
206:                return ("["
207:                        + getSize()
208:                        + "-bit RSA "
209:                        + ((kind == KeyBuilder.TYPE_RSA_PUBLIC) ? "Public"
210:                                : "Private") + " key, Exponent: 0x"
211:                        + KeyBuilder.hexEncode(exp) + ",  Modulus: 0x"
212:                        + KeyBuilder.hexEncode(mod) + "]");
213:            }
214:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.