01: package org.bouncycastle.bcpg;
02:
03: import java.io.ByteArrayOutputStream;
04: import java.io.IOException;
05:
06: /**
07: * Basic type for a symmetric encrypted session key packet
08: */
09: public class SymmetricKeyEncSessionPacket extends ContainedPacket {
10: private int version;
11: private int encAlgorithm;
12: private S2K s2k;
13: private byte[] secKeyData;
14:
15: public SymmetricKeyEncSessionPacket(BCPGInputStream in)
16: throws IOException {
17: version = in.read();
18: encAlgorithm = in.read();
19:
20: s2k = new S2K(in);
21:
22: if (in.available() != 0) {
23: secKeyData = new byte[in.available()];
24: in.readFully(secKeyData, 0, secKeyData.length);
25: }
26: }
27:
28: public SymmetricKeyEncSessionPacket(int encAlgorithm, S2K s2k,
29: byte[] secKeyData) {
30: this .version = 4;
31: this .encAlgorithm = encAlgorithm;
32: this .s2k = s2k;
33: this .secKeyData = secKeyData;
34: }
35:
36: /**
37: * @return int
38: */
39: public int getEncAlgorithm() {
40: return encAlgorithm;
41: }
42:
43: /**
44: * @return S2K
45: */
46: public S2K getS2K() {
47: return s2k;
48: }
49:
50: /**
51: * @return byte[]
52: */
53: public byte[] getSecKeyData() {
54: return secKeyData;
55: }
56:
57: /**
58: * @return int
59: */
60: public int getVersion() {
61: return version;
62: }
63:
64: public void encode(BCPGOutputStream out) throws IOException {
65: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
66: BCPGOutputStream pOut = new BCPGOutputStream(bOut);
67:
68: pOut.write(version);
69: pOut.write(encAlgorithm);
70: pOut.writeObject(s2k);
71:
72: if (secKeyData != null) {
73: pOut.write(secKeyData);
74: }
75:
76: out.writePacket(SYMMETRIC_KEY_ENC_SESSION, bOut.toByteArray(),
77: true);
78: }
79: }
|