throw new IllegalStateException("provider resolves to null");
}
Key key = null;
pOut = new BCPGOutputStream(out);
if (methods.size() == 1)
{
if (methods.get(0) instanceof PBEMethod)
{
PBEMethod m = (PBEMethod)methods.get(0);
key = m.getKey();
}
else
{
key = PGPUtil.makeRandomKey(defAlgorithm, rand);
byte[] sessionInfo = createSessionInfo(defAlgorithm, key);
PubMethod m = (PubMethod)methods.get(0);
try
{
m.addSessionInfo(sessionInfo);
}
catch (Exception e)
{
throw new PGPException("exception encrypting session key", e);
}
}
pOut.writePacket((ContainedPacket)methods.get(0));
}
else // multiple methods
{
key = PGPUtil.makeRandomKey(defAlgorithm, rand);
byte[] sessionInfo = createSessionInfo(defAlgorithm, key);
for (int i = 0; i != methods.size(); i++)
{
EncMethod m = (EncMethod)methods.get(i);
try
{
m.addSessionInfo(sessionInfo);
}
catch (Exception e)
{
throw new PGPException("exception encrypting session key", e);
}
pOut.writePacket(m);
}
}
String cName = PGPUtil.getSymmetricCipherName(defAlgorithm);
if (cName == null)
{
throw new PGPException("null cipher specified");
}
try
{
if (withIntegrityPacket)
{
c = Cipher.getInstance(cName + "/CFB/NoPadding", defProvider);
}
else
{
c = Cipher.getInstance(cName + "/OpenPGPCFB/NoPadding", defProvider);
}
byte[] iv = new byte[c.getBlockSize()];
c.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(iv), rand);
if (buffer == null)
{
//
// we have to add block size + 2 for the generated IV and + 1 + 22 if integrity protected
//
if (withIntegrityPacket)
{
pOut = new BCPGOutputStream(out, PacketTags.SYM_ENC_INTEGRITY_PRO, length + c.getBlockSize() + 2 + 1 + 22);
pOut.write(1); // version number
}
else
{
pOut = new BCPGOutputStream(out, PacketTags.SYMMETRIC_KEY_ENC, length + c.getBlockSize() + 2, oldFormat);
}
}
else
{
if (withIntegrityPacket)
{
pOut = new BCPGOutputStream(out, PacketTags.SYM_ENC_INTEGRITY_PRO, buffer);
pOut.write(1); // version number
}
else
{
pOut = new BCPGOutputStream(out, PacketTags.SYMMETRIC_KEY_ENC, buffer);
}
}
OutputStream genOut = cOut = new CipherOutputStream(pOut, c);