Decoding encrypted and signed messages

See below for decoding an encrypted and signed PKCS #7 message.

Logging the user

Instantiate a user, set the connection to the Directory (if the user provides the IP address), and log in.

FileInputStream credentials = new FileInputStream (<credentials_location>);
SecureStringBuffer password = new SecureStringBuffer(new StringBuffer(<user_password>));
User user = new User();
if (<IP address> != null)
{
JNDIDirectory dir = new JNDIDirectory (<ip>, <port>);
user.setConnections(dir, null);
}
CredentialReader credReader = new StreamProfileReader(credentials);
user.login(credReader, password>);

Creating the decoder

Create a PKCS7DecodeStream object.

PKCS7DecodeStream decoder = new PKCS7DecodeStream(user,new FileInputStream(<input_file>));

Where <input file> is the source of the encoded data.

Decoding the data

Use the decoder to decrypt the encrypted and signed data.

byte[] b = new byte[128];
int i = decoder.read(b);
while (i >= 0)
{
i = decoder.read(b);
}

Optionally, obtain information about the encode operation or digest algorithm used by the message's sender.

int no_of_signatures = decoder.getNumberOfSignatures();
for(int n = 0; n &<; no_of_signatures; ++n;)
{
System.out.println("Signature: " + n);
System.out.println("Digest algorithm: " + decoder.getDigestAlgorithm(n));
System.out.println("Signer certificate: " + decoder.getSignerCertificate(n).getSubjectDN().getName());
System.out.println(decoder.getSignerInfo(n).toString());
}

Close the input stream to the encrypted data and the output stream to the newly decoded data.

decoder.close();
output_data.close();