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