1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
53: public class DSSKeyPairRawCodec
54: implements IKeyPairCodec
55: {
56:
57:
58: public int getFormatID()
59: {
60: return RAW_FORMAT;
61: }
62:
63:
102: public byte[] encodePublicKey(PublicKey key)
103: {
104: if (! (key instanceof DSSPublicKey))
105: throw new IllegalArgumentException("key");
106:
107: DSSPublicKey dssKey = (DSSPublicKey) key;
108: ByteArrayOutputStream baos = new ByteArrayOutputStream();
109:
110: baos.write(Registry.MAGIC_RAW_DSS_PUBLIC_KEY[0]);
111: baos.write(Registry.MAGIC_RAW_DSS_PUBLIC_KEY[1]);
112: baos.write(Registry.MAGIC_RAW_DSS_PUBLIC_KEY[2]);
113: baos.write(Registry.MAGIC_RAW_DSS_PUBLIC_KEY[3]);
114:
115: baos.write(0x01);
116:
117: byte[] buffer = dssKey.getParams().getP().toByteArray();
118: int length = buffer.length;
119: baos.write(length >>> 24);
120: baos.write((length >>> 16) & 0xFF);
121: baos.write((length >>> 8) & 0xFF);
122: baos.write(length & 0xFF);
123: baos.write(buffer, 0, length);
124:
125: buffer = dssKey.getParams().getQ().toByteArray();
126: length = buffer.length;
127: baos.write(length >>> 24);
128: baos.write((length >>> 16) & 0xFF);
129: baos.write((length >>> 8) & 0xFF);
130: baos.write(length & 0xFF);
131: baos.write(buffer, 0, length);
132:
133: buffer = dssKey.getParams().getG().toByteArray();
134: length = buffer.length;
135: baos.write(length >>> 24);
136: baos.write((length >>> 16) & 0xFF);
137: baos.write((length >>> 8) & 0xFF);
138: baos.write(length & 0xFF);
139: baos.write(buffer, 0, length);
140:
141: buffer = dssKey.getY().toByteArray();
142: length = buffer.length;
143: baos.write(length >>> 24);
144: baos.write((length >>> 16) & 0xFF);
145: baos.write((length >>> 8) & 0xFF);
146: baos.write(length & 0xFF);
147: baos.write(buffer, 0, length);
148: return baos.toByteArray();
149: }
150:
151: public PublicKey decodePublicKey(byte[] k)
152: {
153:
154: if (k[0] != Registry.MAGIC_RAW_DSS_PUBLIC_KEY[0]
155: || k[1] != Registry.MAGIC_RAW_DSS_PUBLIC_KEY[1]
156: || k[2] != Registry.MAGIC_RAW_DSS_PUBLIC_KEY[2]
157: || k[3] != Registry.MAGIC_RAW_DSS_PUBLIC_KEY[3])
158: throw new IllegalArgumentException("magic");
159:
160:
161: if (k[4] != 0x01)
162: throw new IllegalArgumentException("version");
163:
164: int i = 5;
165: int l;
166: byte[] buffer;
167:
168: l = k[i++] << 24
169: | (k[i++] & 0xFF) << 16
170: | (k[i++] & 0xFF) << 8
171: | (k[i++] & 0xFF);
172: buffer = new byte[l];
173: System.arraycopy(k, i, buffer, 0, l);
174: i += l;
175: BigInteger p = new BigInteger(1, buffer);
176:
177: l = k[i++] << 24
178: | (k[i++] & 0xFF) << 16
179: | (k[i++] & 0xFF) << 8
180: | (k[i++] & 0xFF);
181: buffer = new byte[l];
182: System.arraycopy(k, i, buffer, 0, l);
183: i += l;
184: BigInteger q = new BigInteger(1, buffer);
185:
186: l = k[i++] << 24
187: | (k[i++] & 0xFF) << 16
188: | (k[i++] & 0xFF) << 8
189: | (k[i++] & 0xFF);
190: buffer = new byte[l];
191: System.arraycopy(k, i, buffer, 0, l);
192: i += l;
193: BigInteger g = new BigInteger(1, buffer);
194:
195: l = k[i++] << 24
196: | (k[i++] & 0xFF) << 16
197: | (k[i++] & 0xFF) << 8
198: | (k[i++] & 0xFF);
199: buffer = new byte[l];
200: System.arraycopy(k, i, buffer, 0, l);
201: i += l;
202: BigInteger y = new BigInteger(1, buffer);
203: return new DSSPublicKey(p, q, g, y);
204: }
205:
206:
244: public byte[] encodePrivateKey(PrivateKey key)
245: {
246: if (! (key instanceof DSSPrivateKey))
247: throw new IllegalArgumentException("key");
248:
249: DSSPrivateKey dssKey = (DSSPrivateKey) key;
250: ByteArrayOutputStream baos = new ByteArrayOutputStream();
251:
252: baos.write(Registry.MAGIC_RAW_DSS_PRIVATE_KEY[0]);
253: baos.write(Registry.MAGIC_RAW_DSS_PRIVATE_KEY[1]);
254: baos.write(Registry.MAGIC_RAW_DSS_PRIVATE_KEY[2]);
255: baos.write(Registry.MAGIC_RAW_DSS_PRIVATE_KEY[3]);
256:
257: baos.write(0x01);
258:
259: byte[] buffer = dssKey.getParams().getP().toByteArray();
260: int length = buffer.length;
261: baos.write(length >>> 24);
262: baos.write((length >>> 16) & 0xFF);
263: baos.write((length >>> 8) & 0xFF);
264: baos.write(length & 0xFF);
265: baos.write(buffer, 0, length);
266:
267: buffer = dssKey.getParams().getQ().toByteArray();
268: length = buffer.length;
269: baos.write(length >>> 24);
270: baos.write((length >>> 16) & 0xFF);
271: baos.write((length >>> 8) & 0xFF);
272: baos.write(length & 0xFF);
273: baos.write(buffer, 0, length);
274:
275: buffer = dssKey.getParams().getG().toByteArray();
276: length = buffer.length;
277: baos.write(length >>> 24);
278: baos.write((length >>> 16) & 0xFF);
279: baos.write((length >>> 8) & 0xFF);
280: baos.write(length & 0xFF);
281: baos.write(buffer, 0, length);
282:
283: buffer = dssKey.getX().toByteArray();
284: length = buffer.length;
285: baos.write(length >>> 24);
286: baos.write((length >>> 16) & 0xFF);
287: baos.write((length >>> 8) & 0xFF);
288: baos.write(length & 0xFF);
289: baos.write(buffer, 0, length);
290: return baos.toByteArray();
291: }
292:
293: public PrivateKey decodePrivateKey(byte[] k)
294: {
295:
296: if (k[0] != Registry.MAGIC_RAW_DSS_PRIVATE_KEY[0]
297: || k[1] != Registry.MAGIC_RAW_DSS_PRIVATE_KEY[1]
298: || k[2] != Registry.MAGIC_RAW_DSS_PRIVATE_KEY[2]
299: || k[3] != Registry.MAGIC_RAW_DSS_PRIVATE_KEY[3])
300: throw new IllegalArgumentException("magic");
301:
302:
303: if (k[4] != 0x01)
304: throw new IllegalArgumentException("version");
305:
306: int i = 5;
307: int l;
308: byte[] buffer;
309:
310: l = k[i++] << 24
311: | (k[i++] & 0xFF) << 16
312: | (k[i++] & 0xFF) << 8
313: | (k[i++] & 0xFF);
314: buffer = new byte[l];
315: System.arraycopy(k, i, buffer, 0, l);
316: i += l;
317: BigInteger p = new BigInteger(1, buffer);
318:
319: l = k[i++] << 24
320: | (k[i++] & 0xFF) << 16
321: | (k[i++] & 0xFF) << 8
322: | (k[i++] & 0xFF);
323: buffer = new byte[l];
324: System.arraycopy(k, i, buffer, 0, l);
325: i += l;
326: BigInteger q = new BigInteger(1, buffer);
327:
328: l = k[i++] << 24
329: | (k[i++] & 0xFF) << 16
330: | (k[i++] & 0xFF) << 8
331: | (k[i++] & 0xFF);
332: buffer = new byte[l];
333: System.arraycopy(k, i, buffer, 0, l);
334: i += l;
335: BigInteger g = new BigInteger(1, buffer);
336:
337: l = k[i++] << 24
338: | (k[i++] & 0xFF) << 16
339: | (k[i++] & 0xFF) << 8
340: | (k[i++] & 0xFF);
341: buffer = new byte[l];
342: System.arraycopy(k, i, buffer, 0, l);
343: i += l;
344: BigInteger x = new BigInteger(1, buffer);
345: return new DSSPrivateKey(p, q, g, x);
346: }
347: }