1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45: import ;
46: import ;
47: import ;
48:
49:
59: public class SRPKeyPairRawCodec
60: implements IKeyPairCodec
61: {
62:
63:
64: public int getFormatID()
65: {
66: return RAW_FORMAT;
67: }
68:
69:
100: public byte[] encodePublicKey(PublicKey key)
101: {
102: if (! (key instanceof SRPPublicKey))
103: throw new IllegalArgumentException("key");
104: SRPPublicKey srpKey = (SRPPublicKey) key;
105: ByteArrayOutputStream baos = new ByteArrayOutputStream();
106:
107: baos.write(Registry.MAGIC_RAW_SRP_PUBLIC_KEY[0]);
108: baos.write(Registry.MAGIC_RAW_SRP_PUBLIC_KEY[1]);
109: baos.write(Registry.MAGIC_RAW_SRP_PUBLIC_KEY[2]);
110: baos.write(Registry.MAGIC_RAW_SRP_PUBLIC_KEY[3]);
111:
112: baos.write(0x01);
113:
114: byte[] buffer = srpKey.getN().toByteArray();
115: int length = buffer.length;
116: baos.write( length >>> 24);
117: baos.write((length >>> 16) & 0xFF);
118: baos.write((length >>> 8) & 0xFF);
119: baos.write( length & 0xFF);
120: baos.write(buffer, 0, length);
121:
122: buffer = srpKey.getG().toByteArray();
123: length = buffer.length;
124: baos.write( length >>> 24);
125: baos.write((length >>> 16) & 0xFF);
126: baos.write((length >>> 8) & 0xFF);
127: baos.write( length & 0xFF);
128: baos.write(buffer, 0, length);
129:
130: buffer = srpKey.getY().toByteArray();
131: length = buffer.length;
132: baos.write( length >>> 24);
133: baos.write((length >>> 16) & 0xFF);
134: baos.write((length >>> 8) & 0xFF);
135: baos.write( length & 0xFF);
136: baos.write(buffer, 0, length);
137: return baos.toByteArray();
138: }
139:
140: public PublicKey decodePublicKey(byte[] k)
141: {
142:
143: if (k[0] != Registry.MAGIC_RAW_SRP_PUBLIC_KEY[0]
144: || k[1] != Registry.MAGIC_RAW_SRP_PUBLIC_KEY[1]
145: || k[2] != Registry.MAGIC_RAW_SRP_PUBLIC_KEY[2]
146: || k[3] != Registry.MAGIC_RAW_SRP_PUBLIC_KEY[3])
147: throw new IllegalArgumentException("magic");
148:
149: if (k[4] != 0x01)
150: throw new IllegalArgumentException("version");
151: int i = 5;
152: int l;
153: byte[] buffer;
154:
155: l = k[i++] << 24
156: | (k[i++] & 0xFF) << 16
157: | (k[i++] & 0xFF) << 8
158: | (k[i++] & 0xFF);
159: buffer = new byte[l];
160: System.arraycopy(k, i, buffer, 0, l);
161: i += l;
162: BigInteger N = new BigInteger(1, buffer);
163:
164: l = k[i++] << 24
165: | (k[i++] & 0xFF) << 16
166: | (k[i++] & 0xFF) << 8
167: | (k[i++] & 0xFF);
168: buffer = new byte[l];
169: System.arraycopy(k, i, buffer, 0, l);
170: i += l;
171: BigInteger g = new BigInteger(1, buffer);
172:
173: l = k[i++] << 24
174: | (k[i++] & 0xFF) << 16
175: | (k[i++] & 0xFF) << 8
176: | (k[i++] & 0xFF);
177: buffer = new byte[l];
178: System.arraycopy(k, i, buffer, 0, l);
179: i += l;
180: BigInteger y = new BigInteger(1, buffer);
181: return new SRPPublicKey(N, g, y);
182: }
183:
184:
223: public byte[] encodePrivateKey(PrivateKey key)
224: {
225: if (! (key instanceof SRPPrivateKey))
226: throw new IllegalArgumentException("key");
227: SRPPrivateKey srpKey = (SRPPrivateKey) key;
228: ByteArrayOutputStream baos = new ByteArrayOutputStream();
229:
230: baos.write(Registry.MAGIC_RAW_SRP_PRIVATE_KEY[0]);
231: baos.write(Registry.MAGIC_RAW_SRP_PRIVATE_KEY[1]);
232: baos.write(Registry.MAGIC_RAW_SRP_PRIVATE_KEY[2]);
233: baos.write(Registry.MAGIC_RAW_SRP_PRIVATE_KEY[3]);
234:
235: baos.write(0x01);
236:
237: byte[] buffer = srpKey.getN().toByteArray();
238: int length = buffer.length;
239: baos.write( length >>> 24);
240: baos.write((length >>> 16) & 0xFF);
241: baos.write((length >>> 8) & 0xFF);
242: baos.write( length & 0xFF);
243: baos.write(buffer, 0, length);
244:
245: buffer = srpKey.getG().toByteArray();
246: length = buffer.length;
247: baos.write( length >>> 24);
248: baos.write((length >>> 16) & 0xFF);
249: baos.write((length >>> 8) & 0xFF);
250: baos.write( length & 0xFF);
251: baos.write(buffer, 0, length);
252:
253: buffer = srpKey.getX().toByteArray();
254: length = buffer.length;
255: baos.write( length >>> 24);
256: baos.write((length >>> 16) & 0xFF);
257: baos.write((length >>> 8) & 0xFF);
258: baos.write( length & 0xFF);
259: baos.write(buffer, 0, length);
260:
261: if (srpKey.getV() != null)
262: {
263: baos.write(0x01);
264: buffer = srpKey.getV().toByteArray();
265: length = buffer.length;
266: baos.write( length >>> 24);
267: baos.write((length >>> 16) & 0xFF);
268: baos.write((length >>> 8) & 0xFF);
269: baos.write( length & 0xFF);
270: baos.write(buffer, 0, length);
271: }
272: else
273: baos.write(0x00);
274: return baos.toByteArray();
275: }
276:
277: public PrivateKey decodePrivateKey(byte[] k)
278: {
279:
280: if (k[0] != Registry.MAGIC_RAW_SRP_PRIVATE_KEY[0]
281: || k[1] != Registry.MAGIC_RAW_SRP_PRIVATE_KEY[1]
282: || k[2] != Registry.MAGIC_RAW_SRP_PRIVATE_KEY[2]
283: || k[3] != Registry.MAGIC_RAW_SRP_PRIVATE_KEY[3])
284: throw new IllegalArgumentException("magic");
285:
286: if (k[4] != 0x01)
287: throw new IllegalArgumentException("version");
288: int i = 5;
289: int l;
290: byte[] buffer;
291:
292: l = k[i++] << 24
293: | (k[i++] & 0xFF) << 16
294: | (k[i++] & 0xFF) << 8
295: | (k[i++] & 0xFF);
296: buffer = new byte[l];
297: System.arraycopy(k, i, buffer, 0, l);
298: i += l;
299: BigInteger N = new BigInteger(1, buffer);
300:
301: l = k[i++] << 24
302: | (k[i++] & 0xFF) << 16
303: | (k[i++] & 0xFF) << 8
304: | (k[i++] & 0xFF);
305: buffer = new byte[l];
306: System.arraycopy(k, i, buffer, 0, l);
307: i += l;
308: BigInteger g = new BigInteger(1, 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 x = new BigInteger(1, buffer);
318:
319: l = k[i++];
320: if (l == 0x01)
321: {
322: l = k[i++] << 24
323: | (k[i++] & 0xFF) << 16
324: | (k[i++] & 0xFF) << 8
325: | (k[i++] & 0xFF);
326: buffer = new byte[l];
327: System.arraycopy(k, i, buffer, 0, l);
328: i += l;
329: BigInteger v = new BigInteger(1, buffer);
330: return new SRPPrivateKey(N, g, x, v);
331: }
332: return new SRPPrivateKey(N, g, x);
333: }
334: }