01: package org.bouncycastle.bcpg;
02:
03: import java.io.*;
04: import java.math.BigInteger;
05:
06: /**
07: * basic packet for a PGP public key
08: */
09: public class PublicKeyEncSessionPacket extends ContainedPacket
10: implements PublicKeyAlgorithmTags {
11: private int version;
12: private long keyID;
13: private int algorithm;
14: private BigInteger[] data;
15:
16: PublicKeyEncSessionPacket(BCPGInputStream in) throws IOException {
17: version = in.read();
18:
19: keyID |= (long) in.read() << 56;
20: keyID |= (long) in.read() << 48;
21: keyID |= (long) in.read() << 40;
22: keyID |= (long) in.read() << 32;
23: keyID |= (long) in.read() << 24;
24: keyID |= (long) in.read() << 16;
25: keyID |= (long) in.read() << 8;
26: keyID |= in.read();
27:
28: algorithm = in.read();
29:
30: switch (algorithm) {
31: case RSA_ENCRYPT:
32: case RSA_GENERAL:
33: data = new BigInteger[1];
34:
35: data[0] = new MPInteger(in).getValue();
36: break;
37: case ELGAMAL_ENCRYPT:
38: case ELGAMAL_GENERAL:
39: data = new BigInteger[2];
40:
41: data[0] = new MPInteger(in).getValue();
42: data[1] = new MPInteger(in).getValue();
43: break;
44: default:
45: throw new IOException(
46: "unknown PGP public key algorithm encountered");
47: }
48: }
49:
50: public PublicKeyEncSessionPacket(long keyID, int algorithm,
51: BigInteger[] data) {
52: this .version = 3;
53: this .keyID = keyID;
54: this .algorithm = algorithm;
55: this .data = data;
56: }
57:
58: public int getVersion() {
59: return version;
60: }
61:
62: public long getKeyID() {
63: return keyID;
64: }
65:
66: public int getAlgorithm() {
67: return algorithm;
68: }
69:
70: public BigInteger[] getEncSessionKey() {
71: return data;
72: }
73:
74: public void encode(BCPGOutputStream out) throws IOException {
75: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
76: BCPGOutputStream pOut = new BCPGOutputStream(bOut);
77:
78: pOut.write(version);
79:
80: pOut.write((byte) (keyID >> 56));
81: pOut.write((byte) (keyID >> 48));
82: pOut.write((byte) (keyID >> 40));
83: pOut.write((byte) (keyID >> 32));
84: pOut.write((byte) (keyID >> 24));
85: pOut.write((byte) (keyID >> 16));
86: pOut.write((byte) (keyID >> 8));
87: pOut.write((byte) (keyID));
88:
89: pOut.write(algorithm);
90:
91: for (int i = 0; i != data.length; i++) {
92: pOut.writeObject(new MPInteger(data[i]));
93: }
94:
95: out.writePacket(PUBLIC_KEY_ENC_SESSION, bOut.toByteArray(),
96: true);
97: }
98: }
|